PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Users/Model/User.php

https://github.com/kareypowell/croogo
PHP | 226 lines | 129 code | 16 blank | 81 comment | 9 complexity | 49aef2eab1684ca345f5d935f116754a MD5 | raw file
  1. <?php
  2. App::uses('UsersAppModel', 'Users.Model');
  3. App::uses('AuthComponent', 'Controller/Component');
  4. /**
  5. * User
  6. *
  7. * @category Model
  8. * @package Croogo.Users.Model
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class User extends UsersAppModel {
  15. /**
  16. * Model name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'User';
  22. /**
  23. * Order
  24. *
  25. * @var string
  26. * @access public
  27. */
  28. public $order = 'User.name ASC';
  29. /**
  30. * Behaviors used by the Model
  31. *
  32. * @var array
  33. * @access public
  34. */
  35. public $actsAs = array(
  36. 'Acl' => array(
  37. 'className' => 'Croogo.CroogoAcl',
  38. 'type' => 'requester',
  39. ),
  40. 'Croogo.Trackable',
  41. 'Search.Searchable',
  42. );
  43. /**
  44. * Model associations: belongsTo
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. public $belongsTo = array('Users.Role');
  50. /**
  51. * Validation
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. public $validate = array(
  57. 'username' => array(
  58. 'isUnique' => array(
  59. 'rule' => 'isUnique',
  60. 'message' => 'The username has already been taken.',
  61. 'last' => true,
  62. ),
  63. 'notEmpty' => array(
  64. 'rule' => 'notEmpty',
  65. 'message' => 'This field cannot be left blank.',
  66. 'last' => true,
  67. ),
  68. 'validAlias' => array(
  69. 'rule' => 'validAlias',
  70. 'message' => 'This field must be alphanumeric',
  71. 'last' => true,
  72. ),
  73. ),
  74. 'email' => array(
  75. 'email' => array(
  76. 'rule' => 'email',
  77. 'message' => 'Please provide a valid email address.',
  78. 'last' => true,
  79. ),
  80. 'isUnique' => array(
  81. 'rule' => 'isUnique',
  82. 'message' => 'Email address already in use.',
  83. 'last' => true,
  84. ),
  85. ),
  86. 'password' => array(
  87. 'rule' => array('minLength', 6),
  88. 'message' => 'Passwords must be at least 6 characters long.',
  89. ),
  90. 'verify_password' => array(
  91. 'rule' => 'validIdentical',
  92. ),
  93. 'name' => array(
  94. 'notEmpty' => array(
  95. 'rule' => 'notEmpty',
  96. 'message' => 'This field cannot be left blank.',
  97. 'last' => true,
  98. ),
  99. 'validName' => array(
  100. 'rule' => 'validName',
  101. 'message' => 'This field must be alphanumeric',
  102. 'last' => true,
  103. ),
  104. ),
  105. 'website' => array(
  106. 'url' => array(
  107. 'rule' => 'url',
  108. 'message' => 'This field must be a valid URL',
  109. 'allowEmpty' => true,
  110. ),
  111. ),
  112. );
  113. /**
  114. * Filter search fields
  115. *
  116. * @var array
  117. * @access public
  118. */
  119. public $filterArgs = array(
  120. 'name' => array('type' => 'like', 'field' => array('User.name', 'User.username')),
  121. 'role_id' => array('type' => 'value'),
  122. );
  123. /**
  124. * Display fields for this model
  125. *
  126. * @var array
  127. */
  128. protected $_displayFields = array(
  129. 'id',
  130. 'Role.title' => 'Role',
  131. 'username',
  132. 'name',
  133. 'status' => array('type' => 'boolean'),
  134. 'email',
  135. );
  136. /**
  137. * Edit fields for this model
  138. *
  139. * @var array
  140. */
  141. protected $_editFields = array(
  142. 'role_id',
  143. 'username',
  144. 'name',
  145. 'email',
  146. 'website',
  147. 'status',
  148. );
  149. /**
  150. * beforeDelete
  151. *
  152. * @param boolean $cascade
  153. * @return boolean
  154. */
  155. public function beforeDelete($cascade = true) {
  156. $this->Role->Behaviors->attach('Croogo.Aliasable');
  157. $adminRoleId = $this->Role->byAlias('admin');
  158. $current = AuthComponent::user();
  159. if (!empty($current['id']) && $current['id'] == $this->id) {
  160. return false;
  161. }
  162. if ($this->field('role_id') == $adminRoleId) {
  163. $count = $this->find('count', array(
  164. 'conditions' => array(
  165. $this->escapeField() . ' <>' => $this->id,
  166. $this->escapeField('role_id') => $adminRoleId,
  167. $this->escapeField('status') => true,
  168. )
  169. ));
  170. return ($count > 0);
  171. }
  172. return true;
  173. }
  174. /**
  175. * beforeSave
  176. *
  177. * @param array $options
  178. * @return boolean
  179. */
  180. public function beforeSave($options = array()) {
  181. if (!empty($this->data[$this->alias]['password'])) {
  182. $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
  183. }
  184. return true;
  185. }
  186. /**
  187. * _identical
  188. *
  189. * @param string $check
  190. * @return boolean
  191. * @deprecated Protected validation methods are no longer supported
  192. */
  193. protected function _identical($check) {
  194. return $this->validIdentical($check);
  195. }
  196. /**
  197. * validIdentical
  198. *
  199. * @param string $check
  200. * @return boolean
  201. */
  202. public function validIdentical($check) {
  203. if (isset($this->data[$this->alias]['password'])) {
  204. if ($this->data[$this->alias]['password'] != $check['verify_password']) {
  205. return __d('croogo', 'Passwords do not match. Please, try again.');
  206. }
  207. }
  208. return true;
  209. }
  210. }