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