PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Centurion/Contrib/auth/models/DbTable/Row/User.php

http://github.com/centurion-project/Centurion
PHP | 195 lines | 92 code | 30 blank | 73 comment | 14 complexity | b5c0361d97041f9ceec24346cb31e491 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Centurion
  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. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@centurion-project.org so we can send you a copy immediately.
  12. *
  13. * @category Centurion
  14. * @package Centurion_Contrib
  15. * @subpackage Auth
  16. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  17. * @license http://centurion-project.org/license/new-bsd New BSD License
  18. * @version $Id$
  19. */
  20. /**
  21. * @category Centurion
  22. * @package Centurion_Contrib
  23. * @subpackage Auth
  24. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  25. * @license http://centurion-project.org/license/new-bsd New BSD License
  26. * @author Florent Messa <florent.messa@gmail.com>
  27. */
  28. class Auth_Model_DbTable_Row_User extends Centurion_Db_Table_Row_Abstract
  29. {
  30. const DEFAULT_ALGORITHM = 'sha1';
  31. /**
  32. * Permissions stack.
  33. *
  34. * @var array
  35. */
  36. protected $_permissions = array();
  37. /**
  38. * Profile instance attached to the user instance.
  39. *
  40. * @var Centurion_Db_Table_Row_Abstract
  41. */
  42. protected $_profile = null;
  43. public function __toString()
  44. {
  45. return $this->username;
  46. }
  47. /**
  48. * Check if the user has the permission.
  49. *
  50. * @param string $permission Permission name
  51. * @return boolean True if the user has the permission otherwise false
  52. */
  53. public function isAllowed($permission)
  54. {
  55. if (!$this->is_active) {
  56. return false;
  57. }
  58. if ($this->is_super_admin) {
  59. return true;
  60. }
  61. if (!is_string($permission)) {
  62. $permission = (string) $permission;
  63. }
  64. if (Zend_Registry::get('Centurion_Acl')->isAllowed($this, $permission)) {
  65. return true;
  66. }
  67. if (!array_key_exists($permission, $this->_permissions)) {
  68. $result = false;
  69. foreach ($this->groups as $groupRow) {
  70. try {
  71. $result = $groupRow->isAllowed($permission);
  72. if ($result)
  73. break;
  74. } catch (Zend_Acl_Exception $e) {
  75. throw $e;
  76. $result = false;
  77. }
  78. }
  79. $this->_permissions[$permission] = $result;
  80. }
  81. return $this->_permissions[$permission];
  82. }
  83. /**
  84. * Proxy method for isAllowed([perm]).
  85. *
  86. * @param string $permission Permission name
  87. * @return boolean True if the user has the permission otherwise false
  88. */
  89. public function hasPerm($permission)
  90. {
  91. return $this->isAllowed($permission);
  92. }
  93. /**
  94. * Set the current password.
  95. *
  96. * @param string $password Password string
  97. * @return void
  98. */
  99. public function setPassword($password)
  100. {
  101. if (null === $this->salt) {
  102. $salt = md5(rand(100000, 999999). $this->username);
  103. $this->salt = $salt;
  104. }
  105. $algorithm = isset($this->algorithm) && '' != trim($this->algorithm) ? $this->algorithm : self::DEFAULT_ALGORITHM;
  106. $algorithmAsStr = is_array($algorithm) ? $algorithm[0] . '::' . $algorithm[1] : $algorithm;
  107. if (!is_callable($algorithm)) {
  108. throw new Centurion_Exception(sprintf('The algorithm callable "%s" is not callable.', $algorithmAsStr));
  109. }
  110. $this->algorithm = $algorithmAsStr;
  111. $this->_data['password'] = call_user_func_array($algorithm, array($this->salt . $password));
  112. $this->_modifiedFields['password'] = true;
  113. }
  114. /**
  115. * Returns True if the user has each of the specified permissions.
  116. * If object is passed, it checks if the user has all required perms
  117. * for this object.
  118. *
  119. * @param array $permissions Permission list
  120. * @return boolean True if the user has each of the specified permissions otherwise false
  121. */
  122. public function hasPerms($permissions)
  123. {
  124. foreach ($permissions as $permission) {
  125. if (!$this->hasPerm($permission)) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * Retrieve the profile attached to the user instance.
  133. *
  134. * @return Centurion_Db_Table_Row_Abstract
  135. */
  136. public function getProfile()
  137. {
  138. if (null === $this->_profile) {
  139. $options = Centurion_Config_Manager::get('centurion');
  140. if (!isset($options['auth_profile'])) {
  141. throw new Centurion_Exception('No site profile module available: you have to set a centurion.auth_profile in your application.ini');
  142. }
  143. if (!class_exists($options['auth_profile'])) {
  144. throw new Centurion_Exception('The class in centurion.auth_profile option does not exists.');
  145. }
  146. $this->_profile = Centurion_Db::getSingletonByClassName($options['auth_profile'])->findOneByUserId($this->pk);
  147. }
  148. return $this->_profile;
  149. }
  150. /**
  151. *
  152. * Get a random password
  153. * @param int $length the length of the password wanted
  154. * @return string a random password
  155. */
  156. public function generePassword($length = 10) {
  157. $list = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  158. $newstring = '';
  159. $max = strlen($list)-1;
  160. while( strlen($newstring) < $length ) {
  161. $newstring .= $list[mt_rand(0, $max)];
  162. }
  163. return $newstring;
  164. }
  165. }