1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Bluz PHP Team |
5
|
|
|
* @link https://github.com/bluzphp/skeleton |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Application\Users; |
11
|
|
|
|
12
|
|
|
use Application\Privileges; |
13
|
|
|
use Application\Roles; |
14
|
|
|
use Bluz\Auth\AbstractIdentity; |
|
|
|
|
15
|
|
|
use Bluz\Validator\Traits\Validator; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Row of User |
19
|
|
|
* |
20
|
|
|
* @package Application\Users |
21
|
|
|
* |
22
|
|
|
* @property integer $id |
23
|
|
|
* @property string $login |
24
|
|
|
* @property string $email |
25
|
|
|
* @property string $created |
26
|
|
|
* @property string $updated |
27
|
|
|
* @property string $status |
28
|
|
|
* |
29
|
|
|
* @OA\Schema(schema="user", title="user", required={"id", "login", "status"}) |
30
|
|
|
* @OA\Property(property="id", type="integer", description="User UID", example=2) |
31
|
|
|
* @OA\Property(property="login", type="string", description="Login", example="admin") |
32
|
|
|
* @OA\Property(property="email", type="string", description="Email", example="[email protected]") |
33
|
|
|
* @OA\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01") |
34
|
|
|
* @OA\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01") |
35
|
|
|
* @OA\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"}) |
36
|
|
|
*/ |
37
|
|
|
class Row extends AbstractIdentity |
38
|
|
|
{ |
39
|
|
|
use Validator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Small cache of user privileges |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $privileges; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
* |
51
|
|
|
* @throws \Bluz\Validator\Exception\ComponentException |
52
|
|
|
*/ |
53
|
|
|
public function beforeSave(): void |
54
|
|
|
{ |
55
|
|
|
$this->email = strtolower($this->email ?? ''); |
56
|
|
|
|
57
|
|
|
$this->addValidator('login') |
58
|
|
|
->required() |
59
|
|
|
->latin() |
60
|
|
|
->length(3, 255) |
61
|
|
|
->callback( |
62
|
|
|
function ($login) { |
63
|
|
|
$selector = static::getTable() |
64
|
|
|
::select() |
65
|
|
|
->where('login = ?', $login); |
66
|
|
|
|
67
|
|
|
if ($this->id) { |
68
|
|
|
$selector->andWhere('id != ?', $this->id); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$user = $selector->execute(); |
72
|
|
|
return !$user; |
73
|
|
|
}, |
74
|
|
|
'User with this login is already exists' |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->addValidator('email') |
78
|
|
|
->required() |
79
|
|
|
->email(true) |
80
|
|
|
->callback( |
81
|
|
|
function ($email) { |
82
|
|
|
$selector = static::getTable() |
83
|
|
|
::select() |
84
|
|
|
->where('email = ?', $email); |
85
|
|
|
|
86
|
|
|
if ($this->id) { |
87
|
|
|
$selector->andWhere('id != ?', $this->id); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$user = $selector->execute(); |
91
|
|
|
return !$user; |
92
|
|
|
}, |
93
|
|
|
'User with this email is already exists' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function beforeInsert(): void |
101
|
|
|
{ |
102
|
|
|
$this->created = gmdate('Y-m-d H:i:s'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function beforeUpdate(): void |
109
|
|
|
{ |
110
|
|
|
$this->updated = gmdate('Y-m-d H:i:s'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get user roles |
115
|
|
|
*/ |
116
|
|
|
public function getRoles() |
117
|
|
|
{ |
118
|
|
|
return Roles\Table::getInstance()->getUserRoles($this->id); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
6 |
|
*/ |
124
|
|
|
public function getPrivileges(): array |
125
|
6 |
|
{ |
126
|
6 |
|
if (!$this->privileges) { |
|
|
|
|
127
|
|
|
$this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id); |
128
|
6 |
|
} |
129
|
|
|
return $this->privileges; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check user role |
134
|
|
|
* |
135
|
|
|
* @param integer $roleId |
136
|
|
|
* |
137
|
|
|
* @return boolean |
138
|
1 |
|
*/ |
139
|
|
|
public function hasRole($roleId): bool |
140
|
1 |
|
{ |
141
|
|
|
$roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id); |
142
|
1 |
|
|
143
|
|
|
return in_array($roleId, $roles, false); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths