userrights.class.inc.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * User rights management API
  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. class UserRightException extends CoreException
  25. {
  26. }
  27. define('UR_ALLOWED_NO', 0);
  28. define('UR_ALLOWED_YES', 1);
  29. define('UR_ALLOWED_DEPENDS', 2);
  30. define('UR_ACTION_READ', 1); // View an object
  31. define('UR_ACTION_MODIFY', 2); // Create/modify an object/attribute
  32. define('UR_ACTION_DELETE', 3); // Delete an object
  33. define('UR_ACTION_BULK_READ', 4); // Export multiple objects
  34. define('UR_ACTION_BULK_MODIFY', 5); // Create/modify multiple objects
  35. define('UR_ACTION_BULK_DELETE', 6); // Delete multiple objects
  36. define('UR_ACTION_APPLICATION_DEFINED', 10000); // Application specific actions (CSV import, View schema...)
  37. /**
  38. * User management module API
  39. *
  40. * @package iTopORM
  41. */
  42. abstract class UserRightsAddOnAPI
  43. {
  44. abstract public function Setup(); // initial installation
  45. abstract public function CreateAdministrator($sAdminUser, $sAdminPwd); // could be used during initial installation
  46. abstract public function Init(); // loads data (possible optimizations)
  47. abstract public function CheckCredentials($sLogin, $sPassword); // returns the id of the user or false
  48. abstract public function CanChangePassword(); // Whether or not a user can change her/his own password
  49. abstract public function ChangePassword($iUserId, $sOldPassword, $sNewPassword); // Change the password of the specified user
  50. abstract public function GetUserLanguage($sLogin); // returns the language code (e.g "EN US")
  51. abstract public function GetUserId($sLogin); // returns the id of the user or false
  52. abstract public function GetContactId($sLogin); // returns the id of the "business" user or false
  53. abstract public function GetFilter($sLogin, $sClass); // returns a filter object
  54. abstract public function IsActionAllowed($iUserId, $sClass, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null);
  55. abstract public function IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, /*dbObjectSet*/ $oInstanceSet = null);
  56. abstract public function IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null);
  57. abstract public function IsAdministrator($iUserId);
  58. abstract public function FlushPrivileges();
  59. }
  60. /**
  61. * User management core API
  62. *
  63. * @package iTopORM
  64. */
  65. class UserRights
  66. {
  67. protected static $m_oAddOn;
  68. protected static $m_sUser;
  69. protected static $m_sRealUser;
  70. protected static $m_iUserId;
  71. protected static $m_iRealUserId;
  72. protected static $m_sUserLanguage;
  73. public static function SelectModule($sModuleName)
  74. {
  75. if (!class_exists($sModuleName))
  76. {
  77. throw new CoreException("Could not select this module, '$sModuleName' in not a valid class name");
  78. return;
  79. }
  80. if (!is_subclass_of($sModuleName, 'UserRightsAddOnAPI'))
  81. {
  82. throw new CoreException("Could not select this module, the class '$sModuleName' is not derived from UserRightsAddOnAPI");
  83. return;
  84. }
  85. self::$m_oAddOn = new $sModuleName;
  86. self::$m_oAddOn->Init();
  87. self::$m_sUser = '';
  88. self::$m_sRealUser = '';
  89. self::$m_iUserId = 0;
  90. self::$m_iRealUserId = 0;
  91. self::$m_sUserLanguage = 'EN US';
  92. }
  93. public static function GetModuleInstance()
  94. {
  95. return self::$m_oAddOn;
  96. }
  97. // Installation: create the very first user
  98. public static function CreateAdministrator($sAdminUser, $sAdminPwd)
  99. {
  100. return self::$m_oAddOn->CreateAdministrator($sAdminUser, $sAdminPwd);
  101. }
  102. // Installation (e.g: give default values for users)
  103. public static function Setup()
  104. {
  105. // to be discussed...
  106. return self::$m_oAddOn->Setup();
  107. }
  108. protected static function IsLoggedIn()
  109. {
  110. return (!empty(self::$m_sUser));
  111. }
  112. public static function Login($sName, $sPassword)
  113. {
  114. self::$m_iUserId = self::$m_oAddOn->CheckCredentials($sName, $sPassword);
  115. if (self::$m_iUserId !== false)
  116. {
  117. self::$m_sUser = $sName;
  118. self::$m_iRealUserId = self::$m_iUserId;
  119. self::$m_sRealUser = $sName;
  120. self::$m_sUserLanguage = self::$m_oAddOn->GetUserLanguage($sName);
  121. Dict::SetUserLanguage(self::GetUserLanguage());
  122. return true;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. public static function CanChangePassword()
  130. {
  131. if (!is_null(self::$m_iUserId))
  132. {
  133. return self::$m_oAddOn->CanChangePassword(self::$m_iUserId);
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. public static function ChangePassword($sCurrentPassword, $sNewPassword)
  141. {
  142. if (!is_null(self::$m_iUserId))
  143. {
  144. return self::$m_oAddOn->ChangePassword(self::$m_iUserId, $sCurrentPassword, $sNewPassword);
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. }
  151. public static function Impersonate($sName, $sPassword)
  152. {
  153. if (!self::CheckLogin()) return false;
  154. self::$m_iRealUserId = self::$m_oAddOn->CheckCredentials($sName, $sPassword);
  155. if (self::$m_iRealUserId !== false)
  156. {
  157. self::$m_sUser = $sName;
  158. self::$m_sUserLanguage = self::$m_oAddOn->GetUserLanguage($sName);
  159. Dict::SetUserLanguage(self::GetUserLanguage());
  160. return true;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. public static function GetUser()
  168. {
  169. return self::$m_sUser;
  170. }
  171. public static function GetUserLanguage()
  172. {
  173. return self::$m_sUserLanguage;
  174. }
  175. public static function GetUserId($sName = '')
  176. {
  177. if (empty($sName))
  178. {
  179. // return current user id
  180. return self::$m_iUserId;
  181. }
  182. else
  183. {
  184. // find the id out of the login string
  185. return self::$m_oAddOn->GetUserId($sName);
  186. }
  187. }
  188. public static function GetContactId($sName = '')
  189. {
  190. // note: returns null if the user management module is not related to the business data model
  191. if (empty($sName))
  192. {
  193. $sName = self::$m_sUser;
  194. }
  195. return self::$m_oAddOn->GetContactId($sName);
  196. }
  197. public static function GetRealUser()
  198. {
  199. return self::$m_sRealUser;
  200. }
  201. public static function GetRealUserId()
  202. {
  203. return self::$m_iRealUserId;
  204. }
  205. protected static function CheckLogin()
  206. {
  207. if (!self::IsLoggedIn())
  208. {
  209. //throw new UserRightException('No user logged in', array());
  210. return false;
  211. }
  212. return true;
  213. }
  214. public static function GetFilter($sClass)
  215. {
  216. if (!self::CheckLogin()) return false;
  217. if (self::IsAdministrator()) return new DBObjectSearch($sClass);
  218. // this module is forbidden for non admins
  219. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  220. // the rest is allowed (#@# to be improved)
  221. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return new DBObjectSearch($sClass);
  222. return self::$m_oAddOn->GetFilter(self::$m_iUserId, $sClass);
  223. }
  224. public static function IsActionAllowed($sClass, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null, $iUserId = null)
  225. {
  226. if (!self::CheckLogin()) return false;
  227. if (self::IsAdministrator($iUserId)) return true;
  228. // this module is forbidden for non admins
  229. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  230. // the rest is allowed (#@# to be improved)
  231. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  232. if (is_null($iUserId))
  233. {
  234. return self::$m_oAddOn->IsActionAllowed(self::$m_iUserId, $sClass, $iActionCode, $oInstanceSet);
  235. }
  236. else
  237. {
  238. return self::$m_oAddOn->IsActionAllowed($iUserId, $sClass, $iActionCode, $oInstanceSet);
  239. }
  240. }
  241. public static function IsStimulusAllowed($sClass, $sStimulusCode, /*dbObjectSet*/ $oInstanceSet = null, $iUserId = null)
  242. {
  243. if (!self::CheckLogin()) return false;
  244. if (self::IsAdministrator($iUserId)) return true;
  245. // this module is forbidden for non admins
  246. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  247. // the rest is allowed (#@# to be improved)
  248. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  249. if (is_null($iUserId))
  250. {
  251. return self::$m_oAddOn->IsStimulusAllowed(self::$m_iUserId, $sClass, $sStimulusCode, $oInstanceSet);
  252. }
  253. else
  254. {
  255. return self::$m_oAddOn->IsStimulusAllowed($iUserId, $sClass, $sStimulusCode, $oInstanceSet);
  256. }
  257. }
  258. public static function IsActionAllowedOnAttribute($sClass, $sAttCode, $iActionCode, /*dbObjectSet*/ $oInstanceSet = null, $iUserId = null)
  259. {
  260. if (!self::CheckLogin()) return false;
  261. if (self::IsAdministrator($iUserId)) return true;
  262. // this module is forbidden for non admins
  263. if (MetaModel::HasCategory($sClass, 'addon/userrights')) return false;
  264. // the rest is allowed (#@# to be improved)
  265. if (!MetaModel::HasCategory($sClass, 'bizmodel')) return true;
  266. if (is_null($iUserId))
  267. {
  268. return self::$m_oAddOn->IsActionAllowedOnAttribute(self::$m_iUserId, $sClass, $sAttCode, $iActionCode, $oInstanceSet);
  269. }
  270. else
  271. {
  272. return self::$m_oAddOn->IsActionAllowedOnAttribute($iUserId, $sClass, $sAttCode, $iActionCode, $oInstanceSet);
  273. }
  274. }
  275. public static function IsAdministrator($iUserId = null)
  276. {
  277. if (!self::CheckLogin()) return false;
  278. if (is_null($iUserId))
  279. {
  280. return self::$m_oAddOn->IsAdministrator(self::$m_iUserId);
  281. }
  282. else
  283. {
  284. return self::$m_oAddOn->IsAdministrator($iUserId);
  285. }
  286. }
  287. public static function FlushPrivileges()
  288. {
  289. return self::$m_oAddOn->FlushPrivileges();
  290. }
  291. }
  292. ?>