1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Gewaer\Models; |
5
|
|
|
|
6
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
7
|
|
|
use Phalcon\Di; |
8
|
|
|
use Phalcon\Validation; |
9
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
10
|
|
|
use Phalcon\Validation\Validator\StringLength; |
11
|
|
|
use Phalcon\Acl\Role as AclRole; |
12
|
|
|
use Gewaer\Exception\ModelException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Roles |
16
|
|
|
* |
17
|
|
|
* @package Gewaer\Models |
18
|
|
|
* |
19
|
|
|
* @property AccesList $accesList |
20
|
|
|
* @property \Phalcon\Di $di |
21
|
|
|
*/ |
22
|
|
|
class Roles extends AbstractModel |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var integer |
27
|
|
|
*/ |
28
|
|
|
public $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $description; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var integer |
45
|
|
|
*/ |
46
|
|
|
public $scope; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
public $companies_id; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
public $apps_id; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
public $created_at; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $updated_at; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @var integer |
75
|
|
|
*/ |
76
|
|
|
public $is_deleted; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Default ACL company |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
const DEFAULT_ACL_COMPANY_ID = 1; |
83
|
|
|
const DEFAULT_ACL_APP_ID = 1; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialize method for model. |
87
|
|
|
*/ |
88
|
1 |
|
public function initialize() |
89
|
|
|
{ |
90
|
1 |
|
$this->setSource('roles'); |
91
|
|
|
|
92
|
1 |
|
$this->hasMany( |
93
|
1 |
|
'id', |
94
|
1 |
|
'Gewaer\Models\AccessList', |
95
|
1 |
|
'roles_id', |
96
|
1 |
|
['alias' => 'accesList'] |
97
|
|
|
); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validations and business logic |
102
|
|
|
*/ |
103
|
|
|
public function validation() |
104
|
|
|
{ |
105
|
|
|
$validator = new Validation(); |
106
|
|
|
|
107
|
|
|
$validator->add( |
108
|
|
|
'name', |
109
|
|
|
new PresenceOf([ |
110
|
|
|
'field' => 'name', |
111
|
|
|
'required' => true, |
112
|
|
|
]) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$validator->add( |
116
|
|
|
'description', |
117
|
|
|
new PresenceOf([ |
118
|
|
|
'field' => 'description', |
119
|
|
|
'required' => true, |
120
|
|
|
]) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$validator->add( |
124
|
|
|
'name', |
125
|
|
|
new StringLength([ |
126
|
|
|
'max' => 32, |
127
|
|
|
'messageMinimum' => _('Role Name. Maxium 32 characters.'), |
128
|
|
|
]) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $this->validate($validator); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns table name mapped in the model. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
1 |
|
public function getSource(): string |
140
|
|
|
{ |
141
|
1 |
|
return 'roles'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Check if the role existe in the db |
146
|
|
|
* |
147
|
|
|
* @param AclRole $role |
148
|
|
|
* @return int |
149
|
|
|
*/ |
150
|
2 |
|
public static function exist(AclRole $role): int |
151
|
|
|
{ |
152
|
2 |
|
return self::count([ |
153
|
2 |
|
'conditions' => 'name = ?0 AND companies_id = ?1 AND apps_id = ?2', |
154
|
2 |
|
'bind' => [$role->getName(), Di::getDefault()->getAcl()->getCompany()->getId(), Di::getDefault()->getAcl()->getApp()->getId()] |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* check if this string is already a role |
160
|
|
|
* whats the diff with exist or why not merge them? exist uses the alc object and only check |
161
|
|
|
* with your current app, this also check with de defautl company ap |
162
|
|
|
* |
163
|
|
|
* @param string $roleName |
164
|
|
|
* @return boolean |
165
|
|
|
*/ |
166
|
2 |
|
public static function isRole(string $roleName) : bool |
167
|
|
|
{ |
168
|
2 |
|
return (bool) self::count([ |
169
|
2 |
|
'conditions' => 'name = ?0 AND apps_id in (?1, ?3) AND companies_id in (?2, ?3)', |
170
|
2 |
|
'bind' => [$roleName, Di::getDefault()->getAcl()->getApp()->getId(), Di::getDefault()->getAcl()->getCompany()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the entity by its name |
176
|
|
|
* |
177
|
|
|
* @param string $name |
178
|
|
|
* @return Roles |
179
|
|
|
*/ |
180
|
2 |
|
public static function getByName(string $name): Roles |
181
|
|
|
{ |
182
|
2 |
|
$role = self::findFirst([ |
183
|
2 |
|
'conditions' => 'name = ?0 AND apps_id in (?1, ?3) AND companies_id in (?2, ?3) AND is_deleted = 0', |
184
|
2 |
|
'bind' => [$name, Di::getDefault()->getAcl()->getApp()->getId(), Di::getDefault()->getAcl()->getCompany()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
if (!is_object($role)) { |
188
|
|
|
throw new ModelException(_('Roles ' . $name . ' not found on this app ' . Di::getDefault()->getAcl()->getApp()->getId() . ' AND Company' . Di::getDefault()->getAcl()->getCompany()->getId())); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $role; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the entity by its name |
196
|
|
|
* |
197
|
|
|
* @param string $name |
198
|
|
|
* @return Roles |
199
|
|
|
*/ |
200
|
|
|
public static function getById(int $id): Roles |
201
|
|
|
{ |
202
|
|
|
return self::findFirst([ |
203
|
|
|
'conditions' => 'id = ?0 AND companies_id in (?1, ?2) AND apps_id in (?3, ?4) AND is_deleted = 0', |
204
|
|
|
'bind' => [$id, Di::getDefault()->getUserData()->currentCompanyId(), Apps::GEWAER_DEFAULT_APP_ID, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
205
|
|
|
]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the Role by it app name |
210
|
|
|
* |
211
|
|
|
* @param string $role |
212
|
|
|
* @return Roles |
213
|
|
|
*/ |
214
|
|
|
public static function getByAppName(string $role, Companies $company): Roles |
215
|
|
|
{ |
216
|
|
|
//echeck if we have a dot , taht means we are sending the specific app to use |
217
|
|
|
if (strpos($role, '.') === false) { |
218
|
|
|
throw new ServerErrorHttpException('ACL - We are expecting the app for this role'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$appRole = explode('.', $role); |
222
|
|
|
$role = $appRole[1]; |
223
|
|
|
$appName = $appRole[0]; |
224
|
|
|
|
225
|
|
|
//look for the app and set it |
226
|
|
|
if (!$app = Apps::getACLApp($appName)) { |
227
|
|
|
throw new ServerErrorHttpException('ACL - No app found for this role'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return self::findFirst([ |
231
|
|
|
'conditions' => 'apps_id in (?0, ?1) AND companies_id in (?2 , ?3)', |
232
|
|
|
'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID] |
233
|
|
|
]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Duplicate a role with it access list |
238
|
|
|
* |
239
|
|
|
* @return Roles |
240
|
|
|
*/ |
241
|
|
|
public function copy(): Roles |
242
|
|
|
{ |
243
|
|
|
$accesList = $this->accesList; |
244
|
|
|
|
245
|
|
|
//remove id to create new record |
246
|
|
|
$this->name .= 'Copie'; |
247
|
|
|
$this->scope = 1; |
248
|
|
|
$this->id = null; |
249
|
|
|
$this->companies_id = $this->di->getUserData()->currentCompanyId(); |
250
|
|
|
$this->apps_id = $this->di->getApp()->getId(); |
251
|
|
|
$this->save(); |
252
|
|
|
|
253
|
|
|
foreach ($accesList as $access) { |
254
|
|
|
$copyAccessList = new AccessList(); |
255
|
|
|
$copyAccessList->apps_id = $this->apps_id; |
256
|
|
|
$copyAccessList->roles_id = $this->getId(); |
257
|
|
|
$copyAccessList->roles_name = $this->name; |
258
|
|
|
$copyAccessList->resources_name = $access->resources_name; |
259
|
|
|
$copyAccessList->access_name = $access->access_name; |
260
|
|
|
$copyAccessList->allowed = $access->allowed; |
261
|
|
|
$copyAccessList->create(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Add inherit to a given role |
269
|
|
|
* |
270
|
|
|
* @param string $roleName |
271
|
|
|
* @param string $roleToInherit |
272
|
|
|
* @return boolean |
273
|
|
|
*/ |
274
|
|
|
public static function addInherit(string $roleName, string $roleToInherit) : bool |
275
|
|
|
{ |
276
|
|
|
$role = self::findFirstByName($roleName); |
277
|
|
|
|
278
|
|
|
if (!is_object($role)) { |
279
|
|
|
throw new ModelException("Role '{$roleName}' does not exist in the role list"); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$inheritExist = RolesInherits::count([ |
283
|
|
|
'conditions' => 'roles_name = ?0 and roles_inherit = ?1', |
284
|
|
|
'bind' => [$role->name, $roleToInherit] |
285
|
|
|
]); |
286
|
|
|
|
287
|
|
|
if (!$inheritExist) { |
288
|
|
|
$rolesInHerits = new RolesInherits(); |
289
|
|
|
$rolesInHerits->roles_id = $role->getId(); |
290
|
|
|
$rolesInHerits->roles_inherit = (int) $roleToInherit; |
291
|
|
|
|
292
|
|
|
if (!$rolesInHerits->save()) { |
293
|
|
|
throw new ModelException((string) current($rolesInHerits->getMessages())); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return true; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return false; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* After update |
304
|
|
|
* |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function afterUpdate() |
308
|
|
|
{ |
309
|
|
|
//if we deleted the role |
310
|
|
|
if ($this->is_deleted) { |
311
|
|
|
//delete |
312
|
|
|
foreach ($this->accesList as $access) { |
313
|
|
|
$access->softDelete(); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Check if role exists by its id |
320
|
|
|
* @param integer $role_id |
321
|
|
|
* @return Roles |
322
|
|
|
*/ |
323
|
|
|
public static function existsById(int $id): Roles |
324
|
|
|
{ |
325
|
|
|
$role = self::getById($id); |
326
|
|
|
|
327
|
|
|
if (!is_object($role)) { |
328
|
|
|
throw new ModelException('Role does not exist'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return $role; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|