userrights.class.inc.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 CreateAdministrator($sAdminUser, $sAdminPwd); // could be used during initial installation
  41. abstract public function Init(); // loads data (possible optimizations)
  42. abstract public function CheckCredentials($sLogin, $sPassword); // returns the id of the user or false
  43. abstract public function GetUserId($sLogin); // returns the id of the user or false
  44. abstract public function GetFilter($sLogin, $sClass); // returns a filter object
  45. abstract public function IsActionAllowed($iUserId, $sClass, $iActionCode, dbObjectSet $oInstances);
  46. abstract public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, dbObjectSet $oInstances);
  47. abstract public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, dbObjectSet $oInstances);
  48. abstract public function IsAdministrator($iUserId);
  49. }
  50. /**
  51. * User management core API
  52. *
  53. * @package iTopORM
  54. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  55. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  56. * @link www.itop.com
  57. * @since 1.0
  58. * @version $itopversion$
  59. */
  60. class UserRights
  61. {
  62. protected static $m_oAddOn;
  63. protected static $m_sUser;
  64. protected static $m_sRealUser;
  65. protected static $m_iUserId;
  66. protected static $m_iRealUserId;
  67. public static function SelectModule($sModuleName)
  68. {
  69. if (!class_exists($sModuleName))
  70. {
  71. throw new CoreException("Could not select this module, '$sModuleName' in not a valid class name");
  72. return;
  73. }
  74. if (!is_subclass_of($sModuleName, 'UserRightsAddOnAPI'))
  75. {
  76. throw new CoreException("Could not select this module, the class '$sModuleName' is not derived from UserRightsAddOnAPI");
  77. return;
  78. }
  79. self::$m_oAddOn = new $sModuleName;
  80. self::$m_oAddOn->Init();
  81. self::$m_sUser = '';
  82. self::$m_sRealUser = '';
  83. self::$m_iUserId = 0;
  84. self::$m_iRealUserId = 0;
  85. }
  86. public static function GetModuleInstance()
  87. {
  88. return self::$m_oAddOn;
  89. }
  90. // Installation: create the very first user
  91. public static function CreateAdministrator($sAdminUser, $sAdminPwd)
  92. {
  93. return self::$m_oAddOn->CreateAdministrator($sAdminUser, $sAdminPwd);
  94. }
  95. // Installation (e.g: give default values for users)
  96. public static function Setup()
  97. {
  98. // to be discussed...
  99. return self::$m_oAddOn->Setup();
  100. }
  101. protected static function IsLoggedIn()
  102. {
  103. return (!empty(self::$m_sUser));
  104. }
  105. public static function Login($sName, $sPassword)
  106. {
  107. self::$m_iUserId = self::$m_oAddOn->CheckCredentials($sName, $sPassword);
  108. if ( self::$m_iUserId !== false )
  109. {
  110. self::$m_sUser = $sName;
  111. self::$m_iRealUserId = self::$m_iUserId;
  112. self::$m_sRealUser = $sName;
  113. return true;
  114. }
  115. else
  116. {
  117. return false;
  118. }
  119. }
  120. public static function Impersonate($sName, $sPassword)
  121. {
  122. if (!self::CheckLogin()) return false;
  123. self::$m_iRealUserId = self::$m_oAddOn->CheckCredentials($sName, $sPassword);
  124. if ( self::$m_iRealUserId !== false)
  125. {
  126. self::$m_sUser = $sName;
  127. return true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. public static function GetUser()
  135. {
  136. return self::$m_sUser;
  137. }
  138. public static function GetUserId($sName = '')
  139. {
  140. if (empty($sName))
  141. {
  142. // return current user id
  143. return self::$m_iUserId;
  144. }
  145. else
  146. {
  147. // find the id out of the login string
  148. return self::$m_oAddOn->GetUserId($sName);
  149. }
  150. }
  151. public static function GetRealUser()
  152. {
  153. return self::$m_sRealUser;
  154. }
  155. public static function GetRealUserId()
  156. {
  157. return self::$m_iRealUserId;
  158. }
  159. protected static function CheckLogin()
  160. {
  161. if (!self::IsLoggedIn())
  162. {
  163. //throw new UserRightException('No user logged in', array());
  164. return false;
  165. }
  166. return true;
  167. }
  168. public static function GetFilter($sClass)
  169. {
  170. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return new DBObjectSearch($sClass);
  171. if (!self::CheckLogin()) return false;
  172. return self::$m_oAddOn->GetFilter(self::$m_iUserId, $sClass);
  173. }
  174. public static function IsActionAllowed($sClass, $iActionCode, dbObjectSet $oInstances, $iUserId = null)
  175. {
  176. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  177. if (!self::CheckLogin()) return false;
  178. if (is_null($iUserId))
  179. {
  180. return self::$m_oAddOn->IsActionAllowed(self::$m_iUserId, $sClass, $iActionCode, $oInstances);
  181. }
  182. else
  183. {
  184. return self::$m_oAddOn->IsActionAllowed($iUserId, $sClass, $iActionCode, $oInstances);
  185. }
  186. }
  187. public static function IsStimulusAllowed($sClass, $sStimulusCode, dbObjectSet $oInstances, $iUserId = null)
  188. {
  189. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  190. if (!self::CheckLogin()) return false;
  191. if (is_null($iUserId))
  192. {
  193. return self::$m_oAddOn->IsStimulusAllowed(self::$m_iUserId, $sClass, $sStimulusCode, $oInstances);
  194. }
  195. else
  196. {
  197. return self::$m_oAddOn->IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, $oInstances);
  198. }
  199. }
  200. public static function IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, dbObjectSet $oInstances, $iUserId = null)
  201. {
  202. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  203. if (!self::CheckLogin()) return false;
  204. if (is_null($iUserId))
  205. {
  206. return self::$m_oAddOn->IsActionAllowedOnAttribute(self::$m_iUserId, $sClass, $sAttCode, $iActionCode, $oInstances);
  207. }
  208. else
  209. {
  210. return self::$m_oAddOn->IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, $oInstances);
  211. }
  212. }
  213. public static function IsAdministrator($iUserId = null)
  214. {
  215. if (!self::CheckLogin()) return false;
  216. if (is_null($iUserId))
  217. {
  218. return self::$m_oAddOn->IsAdministrator(self::$m_iUserId);
  219. }
  220. else
  221. {
  222. return self::$m_oAddOn->IsAdministrator($iUserId);
  223. }
  224. }
  225. }
  226. ?>