1: <?php
2:
3: App::import('Model', 'LdapUser');
4:
5: class UserChangePassword extends AppModel {
6:
7: const MIN_PASSWORD_LENGTH = 6;
8: const MAX_PASSWORD_LENGTH = 30;
9:
10: var $useTable = false;
11: var $validate = array(
12: 'user_id' => array(
13: 'rule' => array('userIdValidacao'),
14: 'message' => 'Usuário e/ou requisição não existente',
15: 'required' => true,
16: ),
17: 'senha_atual' => array(
18: 'rule' => array('senhaAtualValidacao'),
19: 'message' => 'Senha informada não corresponde à atual',
20: 'required' => true,
21: ),
22: 'nova_senha' => array(
23: 'rule' => array('novaSenhaValidacao'),
24: 'message' => 'Nova senha inválida',
25: 'required' => true,
26: ),
27: 'confirmacao_senha' => array(
28: 'rule' => array('confirmacaoSenhaValidacao'),
29: 'message' => 'Confirmação não confere com nova senha',
30: 'required' => true,
31: )
32: );
33:
34: public function __construct($id = false, $table = null, $ds = null) {
35: parent::__construct($id, $table, $ds);
36: $this->UserResetPasswordRequest = ClassRegistry::init('Authentication.UserResetPasswordRequest');
37: $this->AuthenticationUser = ClassRegistry::init('Authentication.AuthenticationUser');
38: }
39:
40: public function save($data = null, $validate = true, $fieldList = array()) {
41: $this->set($data);
42: if ($this->validates()) {
43: $this->AuthenticationUser->changePassword(
44: $this->data[$this->alias]['user_id']
45: , $this->data[$this->alias]['nova_senha']
46: );
47: return true;
48: }
49: return false;
50: }
51:
52: public function userIdValidacao($check) {
53: return $this->_findUser();
54: }
55:
56: public function senhaAtualValidacao($check) {
57: $user = $this->_findUser();
58: if (empty($user)) {
59: return false;
60: } else {
61: $senhaAtualHash = Security::hash($check['senha_atual'], null, true);
62: return $senhaAtualHash == $user['AuthenticationUser']['password'];
63: }
64: }
65:
66: public function novaSenhaValidacao($check) {
67: foreach ($check as $password) {
68: $password = trim($password);
69: if (strlen($password) < self::MIN_PASSWORD_LENGTH || strlen($password) > self::MAX_PASSWORD_LENGTH) {
70: return false;
71: }
72: }
73:
74: return true;
75: }
76:
77: public function confirmacaoSenhaValidacao($check) {
78: foreach ($check as $value) {
79: if ($value != $this->data[$this->alias]['nova_senha']) {
80: return false;
81: }
82: }
83: return true;
84: }
85:
86: private function _findUser() {
87: if (empty($this->data[$this->alias]['user_id'])) {
88: throw new Exception("\$this->data[{$this->alias}]['user_id'] is empty");
89: }
90: $user = $this->AuthenticationUser->findById($this->data[$this->alias]['user_id']);
91: if (empty($user)) {
92: throw new Exception("User not found");
93: }
94: return $user;
95: }
96:
97: }
98: