1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\kadro\Auth; |
4
|
|
|
|
5
|
|
|
use HexMakina\BlackBox\Database\SelectInterface; |
6
|
|
|
use HexMakina\BlackBox\Auth\OperatorInterface; |
7
|
|
|
use HexMakina\TightORM\{TightModel,RelationManyToMany}; |
8
|
|
|
|
9
|
|
|
class Operator extends TightModel implements OperatorInterface |
10
|
|
|
{ |
11
|
|
|
public const TABLE_NAME = 'kadro_operator'; |
12
|
|
|
public const TABLE_ALIAS = 'operator'; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
protected $permissions = null; |
16
|
|
|
|
17
|
|
|
// use Permissionability; |
18
|
|
|
use \HexMakina\TightORM\RelationManyToMany; |
19
|
|
|
|
20
|
|
|
public function __toString() |
21
|
|
|
{ |
22
|
|
|
return $this->get('username'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function isActive(): bool |
26
|
|
|
{ |
27
|
|
|
return !empty($this->get('active')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function operatorId() |
31
|
|
|
{ |
32
|
|
|
return $this->getId(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function username() |
36
|
|
|
{ |
37
|
|
|
return $this->get('username'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// TODO remove this, pwd only useful when checkinin |
41
|
|
|
public function password() |
42
|
|
|
{ |
43
|
|
|
return $this->get('password'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function passwordChange($string) |
47
|
|
|
{ |
48
|
|
|
$this->set('password', password_hash($this->validate_password($string), PASSWORD_DEFAULT)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function passwordVerify($string): bool |
52
|
|
|
{ |
53
|
|
|
return password_verify($this->validate_password($string), $this->password() ?? ''); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function name() |
57
|
|
|
{ |
58
|
|
|
return $this->get('name'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function email() |
62
|
|
|
{ |
63
|
|
|
return $this->get('email'); |
64
|
|
|
} |
65
|
|
|
public function phone() |
66
|
|
|
{ |
67
|
|
|
return $this->get('phone'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function languageCode() |
71
|
|
|
{ |
72
|
|
|
return $this->get('language_code'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function safeLoading($op_id): OperatorInterface |
76
|
|
|
{ |
77
|
|
|
$op = static::one($op_id); |
78
|
|
|
$op->set('password', null); |
79
|
|
|
return $op; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function query_retrieve($filters = [], $options = []): SelectInterface |
83
|
|
|
{ |
84
|
|
|
$Query = static::table()->select(); |
85
|
|
|
if (isset($options['eager']) && $options['eager'] === true) { |
86
|
|
|
$Query->groupBy('id'); |
87
|
|
|
|
88
|
|
|
$Query->autoJoin([ACL::table(), 'acl'], null, 'LEFT OUTER'); |
|
|
|
|
89
|
|
|
$Query->autoJoin([Permission::table(), 'kadro_permission'], null, 'LEFT OUTER'); |
90
|
|
|
$Query->selectAlso(["GROUP_CONCAT(DISTINCT kadro_permission.id) as permission_ids", "GROUP_CONCAT(DISTINCT kadro_permission.name) as permission_names"]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (isset($filters['model']) && !empty($filters['model'])) { |
94
|
|
|
$Query->join([static::otm('t'), static::otm('a')], [[static::otm('a'),static::otm('k'), 't_from','id']], 'INNER'); |
|
|
|
|
95
|
|
|
$Query->whereFieldsEQ(['model_id' => $filters['model']->getId(), 'model_type' => get_class($filters['model'])::model_type()], static::otm('a')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$Query->orderBy([$Query->tableLabel(), 'name', 'ASC']); |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
return $Query; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function immortal(): bool |
105
|
|
|
{ |
106
|
|
|
return true; // never delete a user, always deactivate |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function permission_names() |
110
|
|
|
{ |
111
|
|
|
if (property_exists($this, 'permission_names') && !is_null($this->get('permission_names'))) { |
112
|
|
|
return explode(',', $this->get('permission_names') ?? ''); |
113
|
|
|
} elseif (property_exists($this, 'permission_ids') && !is_null($this->get('permission_ids'))) { |
114
|
|
|
$ids = explode(',', $this->get('permission_ids') ?? ''); |
115
|
|
|
$ret = []; |
116
|
|
|
$permissions = Permission::get_many_by_AIPK($ids); |
117
|
|
|
foreach ($permissions as $id => $p) { |
118
|
|
|
$ret[] = "$p"; |
119
|
|
|
} |
120
|
|
|
return $ret; |
121
|
|
|
} else { |
122
|
|
|
return ACL::permissions_names_for($this); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function permissions() |
127
|
|
|
{ |
128
|
|
|
|
129
|
|
|
if (!is_null($this->permissions)) { |
130
|
|
|
return $this->permissions; |
131
|
|
|
} |
132
|
|
|
$permission_unique_keys = null; |
133
|
|
|
if (property_exists($this, 'permission_names') && !is_null($this->get('permission_names'))) { |
134
|
|
|
$permission_unique_keys = explode(',', $this->get('permission_names') ?? ''); |
135
|
|
|
$this->permissions = Permission::retrieve(Permission::table()->select()->whereStringIn('name', $permission_unique_keys)); |
136
|
|
|
} elseif (property_exists($this, 'permission_ids') && !is_null($this->get('permission_ids'))) { |
137
|
|
|
$permission_unique_keys = explode(',', $this->get('permission_ids') ?? ''); |
138
|
|
|
$this->permissions = Permission::retrieve(Permission::table()->select()->whereNumericIn('id', $permission_unique_keys)); |
139
|
|
|
} else { |
140
|
|
|
$this->permissions = ACL::permissions_for($this); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->permissions; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function hasPermission($p): bool |
147
|
|
|
{ |
148
|
|
|
// new instances or inactive operators, none shall pass |
149
|
|
|
if ($this->isNew() === true || $this->isActive() === false) { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$permission_name = $permission_id = null; |
154
|
|
|
if (is_subclass_of($p, '\HexMakina\kadro\Auth\Permission')) { |
155
|
|
|
$permission_name = $p->get('name'); |
156
|
|
|
$permission_id = $p->getId(); |
157
|
|
|
} elseif (preg_match('/[0-9]+/', $p)) { |
158
|
|
|
$permission_id = $p; |
159
|
|
|
} else { |
160
|
|
|
$permission_name = $p; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!is_null($this->get('permission_names')) && !is_null($permission_name)) { |
164
|
|
|
return strpos($this->get('permission_names'), $permission_name) !== false; |
|
|
|
|
165
|
|
|
} elseif (!is_null($this->get('permission_ids')) && !is_null($permission_id)) { |
166
|
|
|
return strpos($this->get('permission_ids'), $permission_id) !== false; |
167
|
|
|
} elseif (!is_null($permission_name)) { |
168
|
|
|
if (method_exists($this, $permission_name) && $this->$permission_name() == true) { |
169
|
|
|
return true; |
170
|
|
|
} elseif (property_exists($this, $permission_name) && $this->get('$permission_name') == true) { |
171
|
|
|
return true; |
172
|
|
|
} elseif (ACL::match($this, $permission_name) === true) { |
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function validate_password($string): string |
181
|
|
|
{ |
182
|
|
|
if (empty($string)) { |
183
|
|
|
throw new \InvalidArgumentException('PASSWORD_CANT_BE_EMPTY'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $string; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.