|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Authentication; |
|
4
|
|
|
|
|
5
|
|
|
use Win\DAO\DAO; |
|
6
|
|
|
use Win\Authentication\User; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* User DAO |
|
10
|
|
|
*/ |
|
11
|
|
|
class UserDAO extends DAO implements UserDAOInterface { |
|
12
|
|
|
|
|
13
|
|
|
const TABLE = 'user'; |
|
14
|
|
|
const ALIAS = 'Usuário'; |
|
15
|
|
|
|
|
16
|
|
|
/** @var User */ |
|
17
|
|
|
protected $obj; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return string|null |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function validate() { |
|
23
|
|
|
if (strlen($this->obj->getName()) < 2) { |
|
24
|
|
|
return 'O campo Nome deve possuir pelo menos 2 caracteres.'; |
|
25
|
|
|
} elseif (strlen($this->obj->getEmail()) == 0) { |
|
26
|
|
|
return 'O campo E-mail deve ser preenchido.'; |
|
27
|
|
|
} elseif (!filter_var($this->obj->getEmail(), FILTER_VALIDATE_EMAIL)) { |
|
28
|
|
|
return 'O campo E-mail deve ser um e-mail válido.'; |
|
29
|
|
|
} elseif (strlen($this->obj->getPassword()) < 4) { |
|
30
|
|
|
return 'A senha deve possuir pelo menos 4 caracteres.'; |
|
31
|
|
|
} |
|
32
|
|
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $row |
|
37
|
|
|
* @return User |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function mapObject($row) { |
|
40
|
|
|
$obj = new User(); |
|
41
|
|
|
$obj->setId($row['id']); |
|
42
|
|
|
$obj->setActive($row['is_active']); |
|
43
|
|
|
$obj->setAccessLevel($row['access_level']); |
|
44
|
|
|
$obj->setGroupId($row['group_id']); |
|
45
|
|
|
$obj->setName($row['name']); |
|
46
|
|
|
$obj->setEmail($row['email']); |
|
47
|
|
|
$obj->setPasswordHash($row['password_hash']); |
|
48
|
|
|
$obj->setRecoreryHash($row['recovery_hash']); |
|
49
|
|
|
$obj->setImage($row['image']); |
|
50
|
|
|
$obj->setLastLogin($row['last_login']); |
|
51
|
|
|
return $obj; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param User $obj |
|
56
|
|
|
* @return mixed[] |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function mapRow($obj) { |
|
59
|
|
|
$row['id'] = $obj->getId(); |
|
|
|
|
|
|
60
|
|
|
$row['is_active'] = $obj->isActive(); |
|
61
|
|
|
$row['access_level'] = $obj->getAccessLevel(); |
|
62
|
|
|
$row['group_id'] = $obj->getGroupId(); |
|
63
|
|
|
$row['name'] = $obj->getName(); |
|
64
|
|
|
$row['email'] = $obj->getEmail(); |
|
65
|
|
|
$row['password_hash'] = $obj->getPasswordHash(); |
|
66
|
|
|
$row['recovery_hash'] = $obj->getRecoreryHash(); |
|
|
|
|
|
|
67
|
|
|
$row['image'] = $obj->getImage(); |
|
|
|
|
|
|
68
|
|
|
$row['last_login'] = $obj->getLastLogin(); |
|
|
|
|
|
|
69
|
|
|
return $row; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Insere o primeiro usuario |
|
74
|
|
|
* @param string $email |
|
75
|
|
|
* @param string $password |
|
76
|
|
|
* @return string|null |
|
77
|
|
|
*/ |
|
78
|
|
|
public function insertFirst($email, $password) { |
|
79
|
|
|
$totalUser = $this->numRows(); |
|
80
|
|
|
|
|
81
|
|
|
if ($totalUser == 0) { |
|
82
|
|
|
$user = new User(); |
|
83
|
|
|
$user->setName('Administrador'); |
|
84
|
|
|
$user->setEmail($email); |
|
85
|
|
|
$user->setPassword($password); |
|
86
|
|
|
$user->setActive(true); |
|
87
|
|
|
$user->setAccessLevel(User::ACCESS_ADMIN); |
|
88
|
|
|
return $this->save($user); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Atualiza data ultimo login |
|
94
|
|
|
* @param User $user |
|
95
|
|
|
* @return string|null |
|
96
|
|
|
*/ |
|
97
|
|
|
public function updateLastLogin(User $user) { |
|
98
|
|
|
$now = date('Y-m-d H:i:s'); |
|
99
|
|
|
$user->setLastLogin($now); |
|
100
|
|
|
return $this->save($user); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Gera/Atualiza um novo recoveryHash |
|
105
|
|
|
* @param User $user |
|
106
|
|
|
* @return string|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function updateRecoveryHash(User $user) { |
|
109
|
|
|
$hash = md5($user->getEmail() . date('Y-m-d')); |
|
110
|
|
|
$user->setRecoreryHash($hash); |
|
111
|
|
|
return $this->save($user); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Atualiza a senha |
|
116
|
|
|
* @param User $user |
|
117
|
|
|
* @param string $newPassword1 |
|
118
|
|
|
* @param string $newPassword2 |
|
119
|
|
|
* @return string erro |
|
120
|
|
|
*/ |
|
121
|
|
|
public function updatePassword(User $user, $newPassword1, $newPassword2) { |
|
122
|
|
|
$error = null; |
|
123
|
|
|
if ($newPassword1 != $newPassword2) { |
|
124
|
|
|
$error = 'A senha deve ser informada duas vezes iguais.'; |
|
125
|
|
|
} |
|
126
|
|
|
if (!$error) { |
|
|
|
|
|
|
127
|
|
|
$user->setPassword($newPassword1); |
|
128
|
|
|
$error = $this->save($user); |
|
129
|
|
|
} |
|
130
|
|
|
return $error; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.