usercontext.class.inc.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. require_once('../core/cmdbobject.class.inc.php');
  3. require_once('../core/userrights.class.inc.php');
  4. /**
  5. * Helper class to capture a user's restrictions (access rights, profiles...) as a set of limiting conditions
  6. *
  7. * **** NOW OBSOLETE *** SHOULD BE REPLACED EVERYWHERE BY UserRights *****
  8. *
  9. *
  10. *
  11. *
  12. * Usage:
  13. * 1) Build the user's context (from her rights, a lookup in the database, a cookie, whatever)
  14. * $oContext = new UserContext();
  15. * $oContext->AddCondition('SomeClass', 'someFilter', 'SomeValue', '=');
  16. * ...
  17. *
  18. * 2) Use the restrictions contained in the context when retrieving objects either when:
  19. * getting directly an instance of an object
  20. * $oObj = $oContext->GetObject('myClass', 'someKey'); // Instead of $oObj = MetaModel::GetObject('Klass', 'someKey');
  21. * or when building a new search filter
  22. * $oFilter = $oContext->NewFilter('myClass'); // Instead of $oFilter = new CMDBSearchFilter('Klass');
  23. */
  24. class UserContext
  25. {
  26. /**
  27. * Hash array to store the restricting conditions by myClass
  28. */
  29. protected $m_aConditions;
  30. /**
  31. * Constructor
  32. */
  33. public function __construct()
  34. {
  35. $this->m_aConditions = array();
  36. }
  37. /**
  38. * Create a new search filter for the given class of objects that already contains the context's restrictions
  39. */
  40. public function NewFilter($sClass)
  41. {
  42. return UserRights::GetFilter($sClass);
  43. /*
  44. $oFilter = new CMDBSearchFilter($sClass);
  45. foreach($this->m_aConditions as $sConditionClass => $aConditionList)
  46. {
  47. // Add to the filter all the conditions of the parent classes of this class
  48. if ($this->IsSubclass($sConditionClass,$sClass))
  49. {
  50. foreach($aConditionList as $sFilterCode => $aCondition)
  51. {
  52. $oFilter->AddCondition($sFilterCode, $aCondition['value'], $aCondition['operator']);
  53. }
  54. }
  55. }
  56. return $oFilter;
  57. */
  58. }
  59. /**
  60. * Retrieve an instance of an object (if allowed by the context)
  61. */
  62. public function GetObject($sClass, $sKey)
  63. {
  64. $oObject = null;
  65. $oFilter = $this->NewFilter($sClass);
  66. $oFilter->AddCondition('pkey', $sKey, '=');
  67. $oSet = new CMDBObjectSet($oFilter);
  68. if ($oSet->Count() > 0)
  69. {
  70. $oObject = $oSet->Fetch();
  71. }
  72. return $oObject;
  73. }
  74. /**
  75. * Add a restriction to the context for a given class of objects (and all its persistent subclasses)
  76. */
  77. public function AddCondition($sClass, $sFilterCode, $value, $sOperator)
  78. {
  79. if(!isset($this->m_aConditions[$sClass]))
  80. {
  81. $this->m_aConditions[$sClass] = array();
  82. }
  83. $this->m_aConditions[$sClass][$sFilterCode] = array('value'=>$value, 'operator'=>$sOperator);
  84. }
  85. /**
  86. * Check if a given class is a subclass of (or same as) another one
  87. */
  88. protected function IsSubclass($sParentClass, $sSubclass)
  89. {
  90. $bResult = false;
  91. if ($sParentClass == $sSubclass)
  92. {
  93. $bResult = true;
  94. }
  95. else
  96. {
  97. $aParentList = MetaModel::EnumParentClasses($sSubclass);
  98. $bResult = in_array($sParentClass, $aParentList);
  99. }
  100. return $bResult;
  101. }
  102. }
  103. ?>