userrights.class.inc.php 7.5 KB

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