userrights.class.inc.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * UserRights
  4. * User management API
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. class UserRightException extends CoreException
  15. {
  16. }
  17. define('UR_ALLOWED_NO', 0);
  18. define('UR_ALLOWED_YES', 1);
  19. define('UR_ALLOWED_DEPENDS', 2);
  20. define('UR_ACTION_READ', 1); // View an object
  21. define('UR_ACTION_MODIFY', 2); // Create/modify an object/attribute
  22. define('UR_ACTION_DELETE', 3); // Delete an object
  23. define('UR_ACTION_BULK_READ', 4); // Export multiple objects
  24. define('UR_ACTION_BULK_MODIFY', 5); // Create/modify multiple objects
  25. define('UR_ACTION_BULK_DELETE', 6); // Delete multiple objects
  26. define('UR_ACTION_APPLICATION_DEFINED', 10000); // Application specific actions (CSV import, View schema...)
  27. /**
  28. * User management module API
  29. *
  30. * @package iTopORM
  31. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  32. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  33. * @link www.itop.com
  34. * @since 1.0
  35. * @version $itopversion$
  36. */
  37. abstract class UserRightsAddOnAPI
  38. {
  39. abstract public function Setup(); // initial installation
  40. abstract public function Init(); // loads data (possible optimizations)
  41. abstract public function CheckCredentials($sLogin, $sPassword); // returns the id of the user or false
  42. abstract public function GetUserId($sLogin); // returns the id of the user or false
  43. abstract public function GetFilter($sLogin, $sClass); // returns a filter object
  44. abstract public function IsActionAllowed($iUserId, $sClass, $iActionCode, dbObjectSet $oInstances);
  45. abstract public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, dbObjectSet $oInstances);
  46. abstract public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, dbObjectSet $oInstances);
  47. }
  48. /**
  49. * User management core API
  50. *
  51. * @package iTopORM
  52. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  53. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  54. * @link www.itop.com
  55. * @since 1.0
  56. * @version $itopversion$
  57. */
  58. class UserRights
  59. {
  60. protected static $m_oAddOn;
  61. protected static $m_sUser;
  62. protected static $m_sRealUser;
  63. protected static $m_iUserId;
  64. protected static $m_iRealUserId;
  65. public static function SelectModule($sModuleName)
  66. {
  67. if (!class_exists($sModuleName))
  68. {
  69. throw new CoreException("Could not select this module, '$sModuleName' in not a valid class name");
  70. return;
  71. }
  72. if (!is_subclass_of($sModuleName, 'UserRightsAddOnAPI'))
  73. {
  74. throw new CoreException("Could not select this module, the class '$sModuleName' is not derived from UserRightsAddOnAPI");
  75. return;
  76. }
  77. self::$m_oAddOn = new $sModuleName;
  78. self::$m_oAddOn->Init();
  79. self::$m_sUser = '';
  80. self::$m_sRealUser = '';
  81. self::$m_iUserId = 0;
  82. self::$m_iRealUserId = 0;
  83. }
  84. // Installation: create the very first user
  85. public static function CreateAdministrator($sAdminUser, $sAdminPwd)
  86. {
  87. return self::$m_oAddOn->CreateAdministrator($sAdminUser, $sAdminPwd);
  88. }
  89. // Installation (e.g: give default values for users)
  90. public static function Setup()
  91. {
  92. // to be discussed...
  93. return self::$m_oAddOn->Setup();
  94. }
  95. protected static function IsLoggedIn()
  96. {
  97. return (!empty(self::$m_sUser));
  98. }
  99. public static function Login($sName, $sPassword)
  100. {
  101. self::$m_iUserId = self::$m_oAddOn->CheckCredentials($sName, $sPassword);
  102. if ( self::$m_iUserId !== false )
  103. {
  104. self::$m_sUser = $sName;
  105. self::$m_iRealUserId = self::$m_iUserId;
  106. self::$m_sRealUser = $sName;
  107. return true;
  108. }
  109. else
  110. {
  111. return false;
  112. }
  113. }
  114. public static function Impersonate($sName, $sPassword)
  115. {
  116. if (!self::CheckLogin()) return false;
  117. self::$m_iRealUserId = self::$m_oAddOn->CheckCredentials($sName, $sPassword);
  118. if ( self::$m_iRealUserId !== false)
  119. {
  120. self::$m_sUser = $sName;
  121. return true;
  122. }
  123. else
  124. {
  125. return false;
  126. }
  127. }
  128. public static function GetUser()
  129. {
  130. return self::$m_sUser;
  131. }
  132. public static function GetUserId($sName = '')
  133. {
  134. if (empty($sName))
  135. {
  136. // return current user id
  137. return self::$m_iUserId;
  138. }
  139. else
  140. {
  141. // find the id out of the login string
  142. return self::$m_oAddOn->GetUserId($sName);
  143. }
  144. }
  145. public static function GetRealUser()
  146. {
  147. return self::$m_sRealUser;
  148. }
  149. public static function GetRealUserId()
  150. {
  151. return self::$m_iRealUserId;
  152. }
  153. protected static function CheckLogin()
  154. {
  155. if (!self::IsLoggedIn())
  156. {
  157. //throw new UserRightException('No user logged in', array());
  158. return false;
  159. }
  160. return true;
  161. }
  162. public static function GetFilter($sClass)
  163. {
  164. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return new DBObjectSearch($sClass);
  165. if (!self::CheckLogin()) return false;
  166. return self::$m_oAddOn->GetFilter(self::$m_iUserId, $sClass);
  167. }
  168. public static function IsActionAllowed($sClass, $iActionCode, dbObjectSet $oInstances, $iUserId = null)
  169. {
  170. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  171. if (!self::CheckLogin()) return false;
  172. if (is_null($iUserId))
  173. {
  174. return self::$m_oAddOn->IsActionAllowed(self::$m_iUserId, $sClass, $iActionCode, $oInstances);
  175. }
  176. else
  177. {
  178. return self::$m_oAddOn->IsActionAllowed($iUserId, $sClass, $iActionCode, $oInstances);
  179. }
  180. }
  181. public static function IsStimulusAllowed($sClass, $sStimulusCode, dbObjectSet $oInstances, $iUserId = null)
  182. {
  183. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  184. if (!self::CheckLogin()) return false;
  185. if (is_null($iUserId))
  186. {
  187. return self::$m_oAddOn->IsStimulusAllowed(self::$m_iUserId, $sClass, $sStimulusCode, $oInstances);
  188. }
  189. else
  190. {
  191. return self::$m_oAddOn->IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, $oInstances);
  192. }
  193. }
  194. public static function IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, dbObjectSet $oInstances, $iUserId = null)
  195. {
  196. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  197. if (!self::CheckLogin()) return false;
  198. if (is_null($iUserId))
  199. {
  200. return self::$m_oAddOn->IsActionAllowedOnAttribute(self::$m_iUserId, $sClass, $sAttCode, $iActionCode, $oInstances);
  201. }
  202. else
  203. {
  204. return self::$m_oAddOn->IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, $oInstances);
  205. }
  206. }
  207. }
  208. ?>