PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tine20/Tinebase/Model/User.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 444 lines | 183 code | 69 blank | 192 comment | 22 complexity | c7b1137510e5978fdf9831dca8274653 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tinebase
  6. * @subpackage User
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @copyright Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Lars Kneschke <l.kneschke@metaways.de>
  10. *
  11. * @todo write more tests for functions
  12. */
  13. /**
  14. * defines the datatype for simple user object
  15. *
  16. * this user object contains only public informations
  17. * its primary usecase are user selection interfaces
  18. *
  19. * @package Tinebase
  20. * @subpackage User
  21. *
  22. * @property string accountId
  23. * @property string contact_id
  24. * @property string accountEmailAddress email address of user
  25. * @property string accountDisplayName
  26. * @property string accountLastName
  27. * @property string accountFirstName
  28. */
  29. class Tinebase_Model_User extends Tinebase_Record_Abstract
  30. {
  31. /**
  32. * const to describe current account accountId independent
  33. *
  34. * @var string
  35. */
  36. const CURRENTACCOUNT = 'currentAccount';
  37. /**
  38. * hidden from addressbook
  39. *
  40. * @var string
  41. */
  42. const VISIBILITY_HIDDEN = 'hidden';
  43. /**
  44. * visible in addressbook
  45. *
  46. * @var string
  47. */
  48. const VISIBILITY_DISPLAYED = 'displayed';
  49. /**
  50. * account is enabled
  51. *
  52. * @var string
  53. */
  54. const ACCOUNT_STATUS_ENABLED = 'enabled';
  55. /**
  56. * account is disabled
  57. *
  58. * @var string
  59. */
  60. const ACCOUNT_STATUS_DISABLED = 'disabled';
  61. /**
  62. * account is expired
  63. *
  64. * @var string
  65. */
  66. const ACCOUNT_STATUS_EXPIRED = 'expired';
  67. /**
  68. * account is blocked
  69. *
  70. * @var string
  71. */
  72. const ACCOUNT_STATUS_BLOCKED = 'blocked';
  73. /**
  74. * list of zend inputfilter
  75. *
  76. * this filter get used when validating user generated content with Zend_Input_Filter
  77. *
  78. * @var array
  79. */
  80. protected $_filters = array(
  81. 'accountId' => 'StringTrim',
  82. //'accountLoginName' => 'StringTrim',
  83. 'accountDisplayName' => 'StringTrim',
  84. 'accountLastName' => 'StringTrim',
  85. 'accountFirstName' => 'StringTrim',
  86. 'accountFullName' => 'StringTrim',
  87. );
  88. /**
  89. * list of zend validator
  90. *
  91. * this validators get used when validating user generated content with Zend_Input_Filter
  92. *
  93. * @var array
  94. */
  95. protected $_validators = array(
  96. 'accountId' => array('presence' => 'required'),
  97. //'accountLoginName' => array('presence' => 'required'),
  98. 'accountDisplayName' => array('presence' => 'required'),
  99. 'accountLastName' => array('presence' => 'required'),
  100. 'accountFirstName' => array('presence' => 'required'),
  101. 'accountFullName' => array('presence' => 'required'),
  102. 'balanceid' => array('allowEmpty' => true),
  103. 'contact_id' => array('allowEmpty' => true),
  104. );
  105. /**
  106. * user domain
  107. *
  108. * @var string
  109. */
  110. public $domain;
  111. /**
  112. * (non-PHPdoc)
  113. * @see Tinebase/Record/Tinebase_Record_Abstract#setFromArray($_data)
  114. *
  115. * @todo need to discuss if this is the right place to do this. perhaps the client should send the fullname (and displayname), too.
  116. */
  117. public function setFromArray(array $_data)
  118. {
  119. // always update accountDisplayName and accountFullName
  120. $userBackendOptions = Tinebase_User::getBackendConfigurationWithDefaults();
  121. if($userBackendOptions['Ldap']['displaynameFormat'] !== Tinebase_Config::DISPLAYNAME_FORMAT_USE_OTHER || !isset($_data['accountDisplayName']) || !isset($_data['accountFullName'])){
  122. if (isset($_data['accountLastName'])) {
  123. $_data['accountDisplayName'] = $_data['accountLastName'];
  124. if (!empty($_data['accountFirstName'])) {
  125. if($userBackendOptions['Ldap']['displaynameFormat'] === Tinebase_Config::DISPLAYNAME_FORMAT_SN_GIVENNAME) {
  126. $_data['accountDisplayName'] .= ', ' . $_data['accountFirstName'];
  127. } else {
  128. $_data['accountDisplayName'] = $_data['accountFirstName'] . ' ' . $_data['accountLastName'];
  129. }
  130. }
  131. if (! array_key_exists('accountFullName', $_data)) {
  132. $_data['accountFullName'] = $_data['accountLastName'];
  133. if (!empty($_data['accountFirstName'])) {
  134. $_data['accountFullName'] = $_data['accountFirstName'] . ' ' . $_data['accountLastName'];
  135. }
  136. }
  137. }
  138. }
  139. parent::setFromArray($_data);
  140. }
  141. /**
  142. * key in $_validators/$_properties array for the filed which
  143. * represents the identifier
  144. *
  145. * @var string
  146. */
  147. protected $_identifier = 'accountId';
  148. /**
  149. * check if current user has a given right for a given application
  150. *
  151. * @param string|Tinebase_Model_Application $_application the application (one of: app name, id or record)
  152. * @param int $_right the right to check for
  153. * @return bool
  154. */
  155. public function hasRight($_application, $_right)
  156. {
  157. $key = __METHOD__ . (string) $_application . (string) $_right . $this->accountId;
  158. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  159. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  160. }
  161. $roles = Tinebase_Acl_Roles::getInstance();
  162. $result = $roles->hasRight($_application, $this->accountId, $_right);
  163. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  164. return $result;
  165. }
  166. /**
  167. * returns a bitmask of rights for current user and given application
  168. *
  169. * @param string $_application the name of the application
  170. * @return int bitmask of rights
  171. */
  172. public function getRights($_application)
  173. {
  174. $key = __METHOD__ . $_application . $this->accountId;
  175. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  176. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  177. }
  178. $roles = Tinebase_Acl_Roles::getInstance();
  179. $result = $roles->getApplicationRights($_application, $this->accountId);
  180. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  181. return $result;
  182. }
  183. /**
  184. * return the group ids current user is member of
  185. *
  186. * @return array list of group ids
  187. */
  188. public function getGroupMemberships()
  189. {
  190. $key = __METHOD__ . $this->accountId;
  191. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  192. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  193. }
  194. $backend = Tinebase_Group::getInstance();
  195. $result = $backend->getGroupMemberships($this->accountId);
  196. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  197. return $result;
  198. }
  199. /**
  200. * update the lastlogin time of current user
  201. *
  202. * @param string $_ipAddress
  203. * @return void
  204. * @todo write test for that
  205. */
  206. public function setLoginTime($_ipAddress)
  207. {
  208. $backend = Tinebase_User::getInstance();
  209. $result = $backend->setLoginTime($this->accountId, $_ipAddress);
  210. return $result;
  211. }
  212. /**
  213. * set the password for current user
  214. *
  215. * @param string $_password
  216. * @return void
  217. * @todo write test for that
  218. */
  219. public function setPassword($_password)
  220. {
  221. $backend = Tinebase_User::getInstance();
  222. $result = $backend->setPassword($this->accountId, $_password);
  223. return $result;
  224. }
  225. /**
  226. * returns list of applications the current user is able to use
  227. *
  228. * this function takes group memberships into user. Applications the user is able to use
  229. * must have the 'run' right set
  230. *
  231. * @param boolean $_anyRight is any right enough to geht app?
  232. * @return array list of enabled applications for this user
  233. */
  234. public function getApplications($_anyRight = FALSE)
  235. {
  236. $roles = Tinebase_Acl_Roles::getInstance();
  237. $result = $roles->getApplications($this->accountId, $_anyRight);
  238. return $result;
  239. }
  240. /**
  241. * return all container, which the user has the requested right for
  242. *
  243. * used to get a list of all containers accesssible by the current user
  244. *
  245. * @param string $_application the application name
  246. * @param int $_right the required right
  247. * @param bool $_onlyIds return only ids
  248. * @return Tinebase_Record_RecordSet|array
  249. * @todo write test for that
  250. */
  251. public function getContainerByACL($_application, $_right, $_onlyIds = FALSE)
  252. {
  253. $key = __METHOD__ . $_application . (string) $_right . (string) $_onlyIds . (string) $this->accountId;
  254. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  255. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  256. }
  257. $container = Tinebase_Container::getInstance();
  258. $result = $container->getContainerByACL($this->accountId, $_application, $_right, $_onlyIds);
  259. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  260. return $result;
  261. }
  262. /**
  263. * return all personal container of the current user
  264. *
  265. * used to get a list of all personal containers accesssible by the current user
  266. *
  267. * @param string $_application the application name
  268. * @param int|Tinebase_Model_User $_owner
  269. * @param array|string $_grant
  270. * @return Tinebase_Record_RecordSet
  271. * @todo write test for that
  272. */
  273. public function getPersonalContainer($_application, $_owner, $_grant)
  274. {
  275. $key = __METHOD__ . $_application . (string) $_owner . Tinebase_Helper::arrayToCacheId($_grant) . $this->accountId;
  276. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  277. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  278. }
  279. $container = Tinebase_Container::getInstance();
  280. $result = $container->getPersonalContainer($this, $_application, $_owner, $_grant);
  281. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  282. return $result;
  283. }
  284. /**
  285. * get shared containers
  286. *
  287. * @param string|Tinebase_Model_Application $_application
  288. * @param array|string $_grant
  289. * @return Tinebase_Record_RecordSet set of Tinebase_Model_Container
  290. */
  291. public function getSharedContainer($_application, $_grant)
  292. {
  293. $key = __METHOD__ . $_application . Tinebase_Helper::arrayToCacheId($_grant) . (string) $this->accountId;
  294. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  295. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  296. }
  297. $container = Tinebase_Container::getInstance();
  298. $result = $container->getSharedContainer($this, $_application, $_grant);
  299. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  300. return $result;
  301. }
  302. /**
  303. * get containers of other users
  304. *
  305. * @param string|Tinebase_Model_Application $_application
  306. * @param array|string $_grant
  307. * @return Tinebase_Record_RecordSet set of Tinebase_Model_Container
  308. */
  309. public function getOtherUsersContainer($_application, $_grant)
  310. {
  311. $key = __METHOD__ . $_application . Tinebase_Helper::arrayToCacheId($_grant) . (string) $this->accountId;
  312. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  313. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  314. }
  315. $container = Tinebase_Container::getInstance();
  316. $result = $container->getOtherUsersContainer($this, $_application, $_grant);
  317. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  318. return $result;
  319. }
  320. /**
  321. * check if the current user has a given grant
  322. *
  323. * @param int $_containerId
  324. * @param int $_grant
  325. * @return boolean
  326. */
  327. public function hasGrant($_containerId, $_grant)
  328. {
  329. $key = __METHOD__ . (string) $_containerId . Tinebase_Helper::arrayToCacheId($_grant) . $this->accountId;
  330. if (Tinebase_Session_Storage_Acl::getInstance()->has($key)){
  331. return Tinebase_Session_Storage_Acl::getInstance()->get($key);
  332. }
  333. $container = Tinebase_Container::getInstance();
  334. $result = $container->hasGrant($this->accountId, $_containerId, $_grant);
  335. Tinebase_Session_Storage_Acl::getInstance()->set($key, $result);
  336. return $result;
  337. }
  338. /**
  339. * converts a int, string or Tinebase_Model_User to an accountid
  340. *
  341. * @param int|string|Tinebase_Model_User $_accountId the accountid to convert
  342. * @return int
  343. * @throws Tinebase_Exception_NotFound
  344. */
  345. static public function convertUserIdToInt($_accountId)
  346. {
  347. if ($_accountId instanceof Tinebase_Model_User) {
  348. if (empty($_accountId->accountId)) {
  349. throw new Tinebase_Exception_NotFound('accountId can not be empty');
  350. }
  351. $accountId = (string) $_accountId->accountId;
  352. } else {
  353. $accountId = (string) $_accountId;
  354. }
  355. if (empty($accountId)) {
  356. throw new Tinebase_Exception_NotFound('accountId can not be empty');
  357. }
  358. return $accountId;
  359. }
  360. /**
  361. * sanitizes account primary group and returns primary group id
  362. *
  363. * @return string
  364. */
  365. public function sanitizeAccountPrimaryGroup()
  366. {
  367. try {
  368. Tinebase_Group::getInstance()->getGroupById($this->accountPrimaryGroup);
  369. } catch (Tinebase_Exception_Record_NotDefined $e) {
  370. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .' Could not resolve accountPrimaryGroupgroup (' . $this->accountPrimaryGroup . '): ' . $e->getMessage() . ' => set default user group id as accountPrimaryGroup for account ' . $this->getId());
  371. $this->accountPrimaryGroup = Tinebase_Group::getInstance()->getDefaultGroup()->getId();
  372. }
  373. return $this->accountPrimaryGroup;
  374. }
  375. }