1: <?php
2:
3: App::import('Model', 'LdapUser');
4:
5: class UserResetPasswordRequestSubmission extends AppModel {
6:
7: var $useTable = false;
8: var $validate = array(
9: 'username_or_email' => array(
10: 'notempty' => array(
11: 'rule' => array('notempty'),
12: 'required' => true,
13: 'message' => 'Campo não pode ser vazio'
14: ),
15: 'userFound' => array(
16: 'rule' => array('userFoundValidation'),
17: 'message' => 'Não foi encontrado um usuário com o nome de usuário ou email informado.'
18: ),
19: 'userActive' => array(
20: 'rule' => array('userActiveValidation'),
21: 'message' => 'Usuário não está habilitado. Contate o administrador do sistema.'
22: )
23: ),
24: );
25: public $_schema = array(
26: 'username' => array(
27: 'type' => 'string',
28: )
29: );
30:
31: public function __construct($id = false, $table = null, $ds = null) {
32: parent::__construct($id, $table, $ds);
33: $this->AuthenticationUser = ClassRegistry::init('AuthenticationUser');
34: }
35:
36: public function userFoundValidation($check) {
37: return $this->getUser($check['username_or_email']) != null;
38: }
39:
40: public function userActiveValidation($check) {
41: $user = $this->getUser($check['username_or_email']);
42: return $user && $user['AuthenticationUser']['active'];
43: }
44:
45: public function getUser($usernameOrEmail = null) {
46: if (!$usernameOrEmail) {
47: $usernameOrEmail = $this->data[$this->alias]['username_or_email'];
48: }
49:
50: $user = $this->AuthenticationUser->findByUsername(
51: $usernameOrEmail
52: );
53: if (!empty($user)) {
54: return $user;
55: }
56:
57: $user = $this->AuthenticationUser->findByEmail(
58: $usernameOrEmail
59: );
60: if (!empty($user)) {
61: return $user;
62: }
63:
64: return null;
65: }
66:
67: public function getUserId() {
68: if (($user = $this->getUser())) {
69: return $user['User']['id'];
70: } else {
71: throw new Exception("Usuário não encontrado.");
72: }
73: }
74:
75: }
76:
77: ?>
78: