1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Bluz PHP Team |
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Application\Users; |
10
|
|
|
|
11
|
|
|
use Application\Privileges; |
12
|
|
|
use Application\Roles; |
13
|
|
|
use Bluz\Auth\AbstractRowEntity; |
14
|
|
|
use Bluz\Validator\Traits\Validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User |
18
|
|
|
* |
19
|
|
|
* @package Application\Users |
20
|
|
|
* |
21
|
|
|
* @property integer $id |
22
|
|
|
* @property string $login |
23
|
|
|
* @property string $email |
24
|
|
|
* @property string $created |
25
|
|
|
* @property string $updated |
26
|
|
|
* @property string $status |
27
|
|
|
* |
28
|
|
|
* @SWG\Definition(definition="users", title="user", required={"id", "login", "status"}) |
29
|
|
|
* @SWG\Property(property="id", type="integer", description="User UID", example=2) |
30
|
|
|
* @SWG\Property(property="login", type="string", description="Login", example="admin") |
31
|
|
|
* @SWG\Property(property="email", type="string", description="Email", example="[email protected]") |
32
|
|
|
* @SWG\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01") |
33
|
|
|
* @SWG\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01") |
34
|
|
|
* @SWG\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"}) |
35
|
|
|
*/ |
36
|
|
|
class Row extends AbstractRowEntity |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
use Validator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Small cache of user privileges |
42
|
|
|
* |
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('login') |
55
|
|
|
->required() |
56
|
|
|
->latin() |
57
|
|
|
->length(3, 255) |
58
|
|
|
->callback( |
59
|
|
|
function ($login) { |
60
|
|
|
$selector = static::getTable() |
61
|
|
|
::select() |
62
|
|
|
->where('login = ?', $login); |
63
|
|
|
|
64
|
|
|
if ($this->id) { |
65
|
|
|
$selector->andWhere('id != ?', $this->id); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$user = $selector->execute(); |
69
|
|
|
return !$user; |
70
|
|
|
}, |
71
|
|
|
'User with this login is already exists' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->addValidator('email') |
75
|
|
|
->required() |
76
|
|
|
->email(true) |
77
|
|
|
->callback( |
78
|
|
|
function ($email) { |
79
|
|
|
$selector = static::getTable() |
80
|
|
|
::select() |
81
|
|
|
->where('email = ?', $email); |
82
|
|
|
|
83
|
|
|
if ($this->id) { |
84
|
|
|
$selector->andWhere('id != ?', $this->id); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$user = $selector->execute(); |
88
|
|
|
return !$user; |
89
|
|
|
}, |
90
|
|
|
'User with this email is already exists' |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function beforeInsert() |
98
|
|
|
{ |
99
|
|
|
$this->created = gmdate('Y-m-d H:i:s'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function beforeUpdate() |
106
|
|
|
{ |
107
|
|
|
$this->updated = gmdate('Y-m-d H:i:s'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get user roles |
112
|
|
|
*/ |
113
|
|
|
public function getRoles() |
114
|
|
|
{ |
115
|
|
|
return Roles\Table::getInstance()->getUserRoles($this->id); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
6 |
|
public function getPrivileges(): array |
122
|
|
|
{ |
123
|
6 |
|
if (!$this->privileges) { |
|
|
|
|
124
|
6 |
|
$this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id); |
125
|
|
|
} |
126
|
6 |
|
return $this->privileges; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check user role |
131
|
|
|
* |
132
|
|
|
* @param integer $roleId |
133
|
|
|
* |
134
|
|
|
* @return boolean |
135
|
|
|
*/ |
136
|
1 |
|
public function hasRole($roleId) |
137
|
|
|
{ |
138
|
1 |
|
$roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id); |
139
|
|
|
|
140
|
1 |
|
return in_array($roleId, $roles); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|