1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua) |
4
|
|
|
* @author Aleksandr Torosh <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Admin\Model; |
8
|
|
|
use Phalcon\Validation; |
9
|
|
|
use Phalcon\Validation\Validator\Email as EmailValidator; |
10
|
|
|
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
class AdminUser extends \Phalcon\Mvc\Model |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
public function getSource() |
17
|
|
|
{ |
18
|
|
|
return "admin_user"; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public $id; |
22
|
|
|
public $role; |
23
|
|
|
public $login; |
24
|
|
|
public $email; |
25
|
|
|
public $name; |
26
|
|
|
public $password; |
27
|
|
|
public $active = 0; |
28
|
|
|
|
29
|
|
|
public static $roles = [ |
30
|
|
|
'journalist' => 'Journalist', |
31
|
|
|
'editor' => 'Editor', |
32
|
|
|
'admin' => 'Admin', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public function initialize() |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setCheckboxes($post) |
41
|
|
|
{ |
42
|
|
|
$this->setActive(isset($post['active']) ? 1 : 0); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function validation() |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
$validator = new Validation(); |
49
|
|
|
$validator->add('login', new UniquenessValidator( |
50
|
|
|
[ |
51
|
|
|
"model" => $this, |
52
|
|
|
"message" => $this->getDi()->get('helper')->translate("The Login must be unique") |
|
|
|
|
53
|
|
|
] |
54
|
|
|
)); |
55
|
|
|
$validator->add('email', new UniquenessValidator( |
56
|
|
|
[ |
57
|
|
|
"model" => $this, |
58
|
|
|
"message" => $this->getDi()->get('helper')->translate("The Email must be unique") |
|
|
|
|
59
|
|
|
] |
60
|
|
|
)); |
61
|
|
|
return $this->validate($validator); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getId() |
65
|
|
|
{ |
66
|
|
|
return $this->id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getLogin() |
70
|
|
|
{ |
71
|
|
|
return $this->login; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getEmail() |
75
|
|
|
{ |
76
|
|
|
return $this->email; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getRole() |
80
|
|
|
{ |
81
|
|
|
return $this->role; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getRoleTitle() |
85
|
|
|
{ |
86
|
|
|
if (array_key_exists($this->role, self::$roles)) { |
87
|
|
|
return self::$roles[$this->role]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setRole($role) |
92
|
|
|
{ |
93
|
|
|
$this->role = $role; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getName() |
97
|
|
|
{ |
98
|
|
|
return $this->name; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
public function setName($name) |
103
|
|
|
{ |
104
|
|
|
$this->name = $name; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getPassword() |
108
|
|
|
{ |
109
|
|
|
return ''; // We don't need hash of password. Just return empty string. |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function checkPassword($password) |
113
|
|
|
{ |
114
|
|
|
if (password_verify($password, $this->password)) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getActive() |
120
|
|
|
{ |
121
|
|
|
return $this->active; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function isActive() |
125
|
|
|
{ |
126
|
|
|
if ($this->active) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setLogin($login) |
132
|
|
|
{ |
133
|
|
|
$this->login = $login; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setEmail($email) |
137
|
|
|
{ |
138
|
|
|
$this->email = $email; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setPassword($password) |
142
|
|
|
{ |
143
|
|
|
if ($password) { |
144
|
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setActive($active) |
149
|
|
|
{ |
150
|
|
|
$this->active = $active; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getAuthData() |
154
|
|
|
{ |
155
|
|
|
$authData = new stdClass(); |
156
|
|
|
$authData->id = $this->getId(); |
157
|
|
|
$authData->admin_session = true; |
158
|
|
|
$authData->login = $this->getLogin(); |
159
|
|
|
$authData->email = $this->getEmail(); |
160
|
|
|
$authData->name = $this->getName(); |
161
|
|
|
return $authData; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function getRoleById($id) |
165
|
|
|
{ |
166
|
|
|
$role = self::findFirst([ |
|
|
|
|
167
|
|
|
'conditions' => 'id = :id:', |
168
|
|
|
'bind' => ['id' => $id], |
169
|
|
|
'columns' => ['role'], |
170
|
|
|
'cache' => [ |
171
|
|
|
'key' => HOST_HASH . md5('Admin\Model\AdminUser::getRoleById::' . $id), |
172
|
|
|
'lifetime' => 60, |
173
|
|
|
] |
174
|
|
|
]); |
175
|
|
|
if ($role) { |
176
|
|
|
return $role->role; |
177
|
|
|
} else { |
178
|
|
|
return 'guest'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.