usercontext.class.inc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class UserContext... should be obsoleted later (see notes below)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../core/cmdbobject.class.inc.php');
  25. /**
  26. * Helper class to capture a user's restrictions (access rights, profiles...) as a set of limiting conditions
  27. *
  28. * **** NOW OBSOLETE *** SHOULD BE REPLACED EVERYWHERE BY UserRights *****
  29. *
  30. *
  31. *
  32. *
  33. * Usage:
  34. * 1) Build the user's context (from her rights, a lookup in the database, a cookie, whatever)
  35. * $oContext = new UserContext();
  36. * $oContext->AddCondition('SomeClass', 'someFilter', 'SomeValue', '=');
  37. * ...
  38. *
  39. * 2) Use the restrictions contained in the context when retrieving objects either when:
  40. * getting directly an instance of an object
  41. * $oObj = $oContext->GetObject('myClass', 'someKey'); // Instead of $oObj = MetaModel::GetObject('Klass', 'someKey');
  42. * or when building a new search filter
  43. * $oFilter = $oContext->NewFilter('myClass'); // Instead of $oFilter = new CMDBSearchFilter('Klass');
  44. */
  45. class UserContext
  46. {
  47. /**
  48. * Hash array to store the restricting conditions by myClass
  49. */
  50. protected $m_aConditions;
  51. /**
  52. * Constructor
  53. */
  54. public function __construct()
  55. {
  56. $this->m_aConditions = array();
  57. }
  58. /**
  59. * Create a new search filter for the given class of objects that already contains the context's restrictions
  60. */
  61. public function NewFilter($sClass)
  62. {
  63. return UserRights::GetFilter($sClass);
  64. /*
  65. $oFilter = new CMDBSearchFilter($sClass);
  66. foreach($this->m_aConditions as $sConditionClass => $aConditionList)
  67. {
  68. // Add to the filter all the conditions of the parent classes of this class
  69. if ($this->IsSubclass($sConditionClass,$sClass))
  70. {
  71. foreach($aConditionList as $sFilterCode => $aCondition)
  72. {
  73. $oFilter->AddCondition($sFilterCode, $aCondition['value'], $aCondition['operator']);
  74. }
  75. }
  76. }
  77. return $oFilter;
  78. */
  79. }
  80. /**
  81. * Retrieve an instance of an object (if allowed by the context)
  82. */
  83. public function GetObject($sClass, $sKey)
  84. {
  85. $oObject = null;
  86. $oFilter = $this->NewFilter($sClass);
  87. $oFilter->AddCondition('id', $sKey, '=');
  88. $oSet = new CMDBObjectSet($oFilter);
  89. if ($oSet->Count() > 0)
  90. {
  91. $oObject = $oSet->Fetch();
  92. }
  93. return $oObject;
  94. }
  95. /**
  96. * Add a restriction to the context for a given class of objects (and all its persistent subclasses)
  97. */
  98. public function AddCondition($sClass, $sFilterCode, $value, $sOperator)
  99. {
  100. if(!isset($this->m_aConditions[$sClass]))
  101. {
  102. $this->m_aConditions[$sClass] = array();
  103. }
  104. $this->m_aConditions[$sClass][$sFilterCode] = array('value'=>$value, 'operator'=>$sOperator);
  105. }
  106. /**
  107. * Check if a given class is a subclass of (or same as) another one
  108. */
  109. protected function IsSubclass($sParentClass, $sSubclass)
  110. {
  111. $bResult = false;
  112. if ($sParentClass == $sSubclass)
  113. {
  114. $bResult = true;
  115. }
  116. else
  117. {
  118. $aParentList = MetaModel::EnumParentClasses($sSubclass);
  119. $bResult = in_array($sParentClass, $aParentList);
  120. }
  121. return $bResult;
  122. }
  123. }
  124. ?>