|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @namespace |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Application\Users; |
|
12
|
|
|
|
|
13
|
|
|
use Application\Exception; |
|
14
|
|
|
use Application\Privileges; |
|
15
|
|
|
use Application\Roles; |
|
16
|
|
|
use Bluz\Auth\AbstractRowEntity; |
|
17
|
|
|
use Bluz\Auth\AuthException; |
|
18
|
|
|
use Bluz\Proxy\Auth; |
|
19
|
|
|
use Bluz\Proxy\Session; |
|
20
|
|
|
use Bluz\Validator\Traits\Validator; |
|
21
|
|
|
use Bluz\Validator\Validator as v; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* User |
|
25
|
|
|
* |
|
26
|
|
|
* @package Application\Users |
|
27
|
|
|
* |
|
28
|
|
|
* @property integer $id |
|
29
|
|
|
* @property string $login |
|
30
|
|
|
* @property string $email |
|
31
|
|
|
* @property string $created |
|
32
|
|
|
* @property string $updated |
|
33
|
|
|
* @property string $status |
|
34
|
|
|
* |
|
35
|
|
|
* @SWG\Definition(definition="users", title="user", required={"id", "login", "status"}) |
|
36
|
|
|
* @SWG\Property(property="id", type="integer", description="User UID", example=2) |
|
37
|
|
|
* @SWG\Property(property="login", type="string", description="Login", example="admin") |
|
38
|
|
|
* @SWG\Property(property="email", type="string", description="Email", example="[email protected]") |
|
39
|
|
|
* @SWG\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01") |
|
40
|
|
|
* @SWG\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01") |
|
41
|
|
|
* @SWG\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"}) |
|
42
|
|
|
*/ |
|
43
|
|
|
class Row extends AbstractRowEntity |
|
44
|
|
|
{ |
|
45
|
|
|
use Validator; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Small cache of user privileges |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $privileges; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function beforeSave() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->email = strtolower($this->email); |
|
60
|
|
|
|
|
61
|
|
|
$this->addValidator( |
|
62
|
|
|
'login', |
|
63
|
|
|
v::required()->latin()->length(3, 255), |
|
64
|
|
|
v::callback( |
|
65
|
|
|
function ($login) { |
|
66
|
|
|
$user = $this->getTable() |
|
67
|
|
|
->select() |
|
68
|
|
|
->where('login = ?', $login) |
|
69
|
|
|
->andWhere('id != ?', $this->id) |
|
70
|
|
|
->execute(); |
|
71
|
|
|
return !$user; |
|
72
|
|
|
} |
|
73
|
|
|
)->setError('User with login "{{input}}" already exists') |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$this->addValidator( |
|
77
|
|
|
'email', |
|
78
|
|
|
v::required()->email(true), |
|
79
|
|
|
v::callback( |
|
80
|
|
|
function ($email) { |
|
81
|
|
|
$user = $this->getTable() |
|
82
|
|
|
->select() |
|
83
|
|
|
->where('email = ?', $email) |
|
84
|
|
|
->andWhere('id != ?', $this->id) |
|
85
|
|
|
->execute(); |
|
86
|
|
|
return !$user; |
|
87
|
|
|
} |
|
88
|
|
|
)->setError('User with email "{{input}}" already exists') |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function beforeInsert() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->created = gmdate('Y-m-d H:i:s'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function beforeUpdate() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->updated = gmdate('Y-m-d H:i:s'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Can entity login |
|
110
|
|
|
* |
|
111
|
|
|
* @throws Exception |
|
112
|
|
|
* @throws AuthException |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function tryLogin() |
|
116
|
|
|
{ |
|
117
|
1 |
|
switch ($this->status) { |
|
118
|
1 |
|
case (Table::STATUS_PENDING): |
|
119
|
|
|
throw new AuthException('Your account is pending activation', 403); |
|
120
|
1 |
|
case (Table::STATUS_DISABLED): |
|
121
|
|
|
throw new AuthException('Your account is disabled by administrator', 403); |
|
122
|
1 |
|
case (Table::STATUS_ACTIVE): |
|
123
|
|
|
// all ok |
|
124
|
|
|
// regenerate session |
|
125
|
1 |
|
if (PHP_SAPI !== 'cli') { |
|
126
|
|
|
Session::regenerateId(); |
|
127
|
|
|
} |
|
128
|
|
|
// save user to new session |
|
129
|
1 |
|
Auth::setIdentity($this); |
|
130
|
1 |
|
break; |
|
131
|
|
|
default: |
|
132
|
|
|
throw new Exception('User status is undefined in system'); |
|
133
|
|
|
} |
|
134
|
1 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get user roles |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getRoles() |
|
140
|
|
|
{ |
|
141
|
|
|
return Roles\Table::getInstance()->getUserRoles($this->id); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function getPrivileges(): array |
|
148
|
|
|
{ |
|
149
|
3 |
|
if (!$this->privileges) { |
|
|
|
|
|
|
150
|
3 |
|
$this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id); |
|
151
|
|
|
} |
|
152
|
3 |
|
return $this->privileges; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Check user role |
|
157
|
|
|
* |
|
158
|
|
|
* @param integer $roleId |
|
159
|
|
|
* |
|
160
|
|
|
* @return boolean |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function hasRole($roleId) |
|
163
|
|
|
{ |
|
164
|
1 |
|
$roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id); |
|
165
|
|
|
|
|
166
|
1 |
|
return in_array($roleId, $roles); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.