userrights.class.inc.php 8.5 KB

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