usercontext.class.inc.php 4.0 KB

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