PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/admin/models/User.php

http://digitalus-cms.googlecode.com/
PHP | 423 lines | 266 code | 33 blank | 124 comment | 54 complexity | da554d247ca37ed4e0901eb29578e459 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Digitalus CMS
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://digitalus-media.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@digitalus-media.com so we can send you a copy immediately.
  14. *
  15. * @category Digitalus CMS
  16. * @package Digitalus_CMS_Models
  17. * @copyright Copyright (c) 2007 - 2010, Digitalus Media USA (digitalus-media.com)
  18. * @license http://digitalus-media.com/license/new-bsd New BSD License
  19. * @version $Id: User.php Mon Dec 24 20:38:38 EST 2007 20:38:38 forrest lyman $
  20. * @link http://www.digitaluscms.com
  21. * @since Release 1.5
  22. */
  23. /**
  24. * @see Digitalus_Db_Table
  25. */
  26. require_once 'Digitalus/Db/Table.php';
  27. /**
  28. * User model
  29. *
  30. * @copyright Copyright (c) 2007 - 2010, Digitalus Media USA (digitalus-media.com)
  31. * @license http://digitalus-media.com/license/new-bsd New BSD License
  32. * @version Release: @package_version@
  33. * @link http://www.digitaluscms.com
  34. * @since Release 1.5
  35. */
  36. class Model_User extends Digitalus_Db_Table
  37. {
  38. /**
  39. * The role of the superuser
  40. */
  41. const SUPERUSER_ROLE = 'superadmin';
  42. /**
  43. * the maximum lenght for user names (must correspond to length in database)
  44. */
  45. const USERNAME_LENGTH = 30;
  46. /**
  47. * the regex that the userName will be checked against
  48. */
  49. const USERNAME_REGEX = '/^[0-9\p{L}`\'´ ]*$/u';
  50. /**
  51. * this is the error message that will be displayed if the userName doesn't match the regex
  52. */
  53. const USERNAME_REGEX_NOTMATCH = 'Please only use alphanumeric characters, `\'´ and empty space!';
  54. /**
  55. * table name
  56. *
  57. * @var string
  58. */
  59. protected $_name = 'users';
  60. protected $_primary = 'name';
  61. public function createUser($userName, $firstName, $lastName, $email, $password, $active = 0, $role = Model_Group::GUEST_ROLE)
  62. {
  63. $data = array(
  64. 'active' => $active,
  65. 'name' => $userName,
  66. 'first_name' => $firstName,
  67. 'last_name' => $lastName,
  68. 'email' => $email,
  69. 'password' => md5($password),
  70. 'role' => $role,
  71. );
  72. if (!$this->userExists($userName)) {
  73. return $this->insert($data);
  74. }
  75. return false;
  76. }
  77. public function updatePassword($userName, $password, $confirmationRequire = true, $confirmation = null)
  78. {
  79. $person = $this->find($userName)->current();
  80. if ($person) {
  81. if ($confirmationRequire == true) {
  82. if ($confirmation != $password) {
  83. return false;
  84. }
  85. }
  86. $person->password = md5($password);
  87. $result = $person->save();
  88. return $result;
  89. }
  90. return false;
  91. }
  92. public function updateAclResources($userName, $resourceArray)
  93. {
  94. $data['acl_resources'] = serialize($resourceArray);
  95. $where[] = $this->_db->quoteInto('name = ?', $userName);
  96. return $this->update($data, $where);
  97. }
  98. public function getAclResources($userRowset)
  99. {
  100. $role = Model_Group::GUEST_ROLE;
  101. if (isset($userRowset->role) && !empty($userRowset->role)) {
  102. $role = $userRowset->role;
  103. }
  104. $mdlGroup = new Model_Group();
  105. return $mdlGroup->getAclResources($role);
  106. }
  107. /**
  108. * returns the complete user row for the currently logged in user
  109. * @return Zend_Db_Row
  110. */
  111. public function getCurrentUser()
  112. {
  113. $currentUser = Digitalus_Auth::getIdentity();
  114. if (!empty($currentUser) && isset($currentUser->name) && Model_Group::GUEST_ROLE != $currentUser->name) {
  115. return $this->find($currentUser->name)->current();
  116. } else {
  117. return $currentUser;
  118. }
  119. }
  120. public function getCurrentUsersAclResources()
  121. {
  122. $currentUser = $this->getCurrentUser();
  123. if (!empty($currentUser)) {
  124. return $this->getAclResources($currentUser);
  125. }
  126. }
  127. public function getCurrentUsersModules()
  128. {
  129. return $this->getUsersModules($this->getCurrentUser());
  130. }
  131. public function getUsersModules($userRowset)
  132. {
  133. $modules = null;
  134. $user = $this->getCurrentUser();
  135. if ($user->role == Model_User::SUPERUSER_ROLE) {
  136. //the superadmin has access to all of the modules
  137. $front = Zend_Controller_Front::getInstance();
  138. $ctlPaths = $front->getControllerDirectory();
  139. foreach ($ctlPaths as $module => $path) {
  140. if (substr($module, 0, 4) == 'mod_') {
  141. $modules[] = str_replace('mod_', '', $module);
  142. }
  143. }
  144. } else {
  145. $resources = $this->getAclResources($userRowset);
  146. if (is_array($resources)) {
  147. foreach ($resources as $k => $v) {
  148. if (1 == $v ) {
  149. $parts = explode('_', $k);
  150. if ('mod' == $parts[0]) {
  151. $key = $parts[1];
  152. $modules[$key] = $key;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if (is_array($modules)) {
  159. return $modules;
  160. }
  161. }
  162. /**
  163. * this function queries a users permissions
  164. *
  165. * the resource should be in the module_controller_action format
  166. *
  167. * if strict = true then this requires an exact match
  168. * example: news_article != news_article_edit
  169. *
  170. * if strict = false then it will add wildcards
  171. * example: news_article == news_article_edit
  172. *
  173. * if user is not set then the query will be run on the current user
  174. *
  175. * @param string $resource
  176. * @param boolean $strict
  177. * @param integer $user
  178. * @return boolean
  179. */
  180. public function queryPermissions($resource, $strict = false, $userName = null)
  181. {
  182. if ($userName !== null) {
  183. $user = $this->find($userName)->current();
  184. if (!$user) {
  185. return false;
  186. }
  187. $resources = $this->getAclResources($user);
  188. } else {
  189. $resources = $this->getCurrentUsersAclResources();
  190. }
  191. if (is_array($resources)) {
  192. if ($strict) {
  193. if (array_key_exists($resource, $resources) && 1 == $resources[$resource]) {
  194. return true;
  195. }
  196. } else {
  197. $len = strlen($resource);
  198. foreach ($resources as $r => $v) {
  199. if (1 == $v && $resource == substr($r, 0, $len)) {
  200. return true;
  201. }
  202. }
  203. }
  204. }
  205. return false;
  206. }
  207. public function getUserFullNameByUsername($userName, $format = null)
  208. {
  209. $user = $this->getUserByUsername($userName);
  210. switch ((string)strtolower($format)) {
  211. case 'firstname':
  212. return $user->first_name;
  213. case 'lastname':
  214. return $user->last_name;
  215. default:
  216. return $user->first_name . ' ' . $user->last_name;
  217. }
  218. }
  219. public function getGroupByUsername($userName)
  220. {
  221. if (Model_Group::GUEST_ROLE == $userName) {
  222. return Model_Group::GUEST_ROLE;
  223. }
  224. $select = $this->select();
  225. $select->from($this->_name, array('name', 'role'))
  226. ->where($this->_db->quoteInto('name = ?', $userName));
  227. $user = $this->fetchRow($select);
  228. return $user->role;
  229. }
  230. public function getUserByUsername($userName)
  231. {
  232. $where[] = $this->_db->quoteInto('name = ?', $userName);
  233. return $this->fetchRow($where);
  234. }
  235. public function getUserByEmail($email)
  236. {
  237. $where[] = $this->_db->quoteInto('email = ?', $userName);
  238. return $this->fetchRow($where);
  239. }
  240. public function getUserByOpenId($openId)
  241. {
  242. $where[] = $this->_db->quoteInto('openid = ?', $openId);
  243. return $this->fetchRow($where);
  244. }
  245. /**
  246. * @since 0.8.7
  247. *
  248. * returns a hash of the current users
  249. * their name is the key and their first_name . ' ' . last_name is the value
  250. *
  251. */
  252. public function getUserNamesArray()
  253. {
  254. $select = $this->select();
  255. $select->from($this->_name, array('name', 'first_name', 'last_name'));
  256. $users = $this->fetchAll($select);
  257. foreach ($users as $user) {
  258. $usersArray[$user->name] = $user->first_name . ' ' . $user->last_name;
  259. }
  260. return $usersArray;
  261. }
  262. /**
  263. * @since 1.10.0
  264. *
  265. * returns the current users
  266. *
  267. */
  268. public function getUsers($selectItems = null)
  269. {
  270. $select = $this->select();
  271. $select->from($this->_name, array_map('trim', explode(',', $selectItems)))
  272. ->order('name ASC');
  273. $users = $this->fetchAll($select);
  274. return $users;
  275. }
  276. public function copyPermissions($from, $to)
  277. {
  278. $fromUser = $this->find($from)->current();
  279. $toUser = $this->find($to)->current();
  280. $toUser->acl_resources = $fromUser->acl_resources;
  281. return $toUser->save();
  282. }
  283. /**
  284. * This function checks if a user already exists
  285. *
  286. * @param string $userName The name to check for
  287. * @param string $exclude Usernames to exclude from check
  288. * @return boolean
  289. */
  290. public function userExists($userName, $exclude = null)
  291. {
  292. $userName = strtolower($userName);
  293. if (!is_array($exclude)) {
  294. $exclude = array($exclude);
  295. }
  296. $where[] = $this->_db->quoteInto('LOWER(name) = ?', $userName);
  297. foreach ($exclude as $exclusion) {
  298. $exclusion = trim($exclusion);
  299. if (isset($exclusion) && !empty($exclusion) && '' != $exclusion) {
  300. $where[] = $this->_db->quoteInto('LOWER(name) != ?', $exclusion);
  301. }
  302. }
  303. $result = $this->fetchAll($where, null, 1);
  304. if ($result->count() > 0) {
  305. return true;
  306. }
  307. return false;
  308. }
  309. /**
  310. * This function checks if a specified openId already exists
  311. *
  312. * @param string $openId The openId to check for
  313. * @return boolean
  314. */
  315. public function openIdExists($openId)
  316. {
  317. $openId = strtolower($openId);
  318. $where[] = $this->_db->quoteInto('LOWER(openid) = ?', $openId);
  319. $result = $this->fetchAll($where, null, 1);
  320. if ($result->count() > 0) {
  321. return true;
  322. }
  323. return false;
  324. }
  325. /**
  326. * This function checks if a specified email belongs to a given user name
  327. *
  328. * @param string $userName The user name
  329. * @param string $email The email address
  330. * @return boolean
  331. */
  332. public function userEmailExists($userName, $email)
  333. {
  334. $userName = strtolower($userName);
  335. $email = strtolower($email);
  336. $where[] = $this->_db->quoteInto('LOWER(name) = ?', $userName);
  337. $where[] = $this->_db->quoteInto('LOWER(email) = ?', $email);
  338. $result = $this->fetchAll($where, null, 1);
  339. if ($result->count() > 0) {
  340. return true;
  341. }
  342. return false;
  343. }
  344. /**
  345. * This function checks if a user has already been activated
  346. *
  347. * @param int $userName The name to check
  348. * @return boolean
  349. */
  350. public function isActive($userName)
  351. {
  352. $where[] = $this->_db->quoteInto('name = ?', $userName);
  353. $where[] = $this->_db->quoteInto('active = ?', 1, 'TINYINT');
  354. $result = $this->fetchAll($where, null, 1);
  355. if ($result->count() > 0) {
  356. return true;
  357. }
  358. return false;
  359. }
  360. /**
  361. * This function activates a user
  362. *
  363. * @param int $userName The name to activate
  364. * @return int Number of rows updated
  365. */
  366. public function activate($userName)
  367. {
  368. $data['active'] = 1;
  369. $where[] = $this->_db->quoteInto('name = ?', $userName);
  370. if ($this->isActive($userName) || $this->update($data, $where)) {
  371. return true;
  372. }
  373. return false;
  374. }
  375. /**
  376. * This function deactivates a user
  377. *
  378. * @param int $userName The name to deactivate
  379. * @return int Number of rows updated
  380. */
  381. public function deactivate($userName)
  382. {
  383. $data['active'] = 0;
  384. $where[] = $this->_db->quoteInto('name = ?', $userName);
  385. if (!$this->isActive($userName) || $this->update($data, $where)) {
  386. return true;
  387. }
  388. return false;
  389. }
  390. }