|
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 = 0; |
|
83
|
|
|
const DEFAULT_ACL_APP_ID = 0; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Initialize method for model. |
|
87
|
|
|
*/ |
|
88
|
20 |
|
public function initialize() |
|
89
|
|
|
{ |
|
90
|
20 |
|
$this->setSource('roles'); |
|
91
|
|
|
|
|
92
|
20 |
|
$this->hasMany( |
|
93
|
20 |
|
'id', |
|
94
|
20 |
|
'Gewaer\Models\AccessList', |
|
95
|
20 |
|
'roles_id', |
|
96
|
20 |
|
['alias' => 'accesList'] |
|
97
|
|
|
); |
|
98
|
20 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Validations and business logic |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function validation() |
|
104
|
|
|
{ |
|
105
|
3 |
|
$validator = new Validation(); |
|
106
|
|
|
|
|
107
|
3 |
|
$validator->add( |
|
108
|
3 |
|
'name', |
|
109
|
3 |
|
new PresenceOf([ |
|
110
|
3 |
|
'field' => 'name', |
|
111
|
|
|
'required' => true, |
|
112
|
|
|
]) |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
3 |
|
$validator->add( |
|
116
|
3 |
|
'description', |
|
117
|
3 |
|
new PresenceOf([ |
|
118
|
3 |
|
'field' => 'description', |
|
119
|
|
|
'required' => true, |
|
120
|
|
|
]) |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
3 |
|
$validator->add( |
|
124
|
3 |
|
'name', |
|
125
|
3 |
|
new StringLength([ |
|
126
|
3 |
|
'max' => 32, |
|
127
|
3 |
|
'messageMinimum' => _('Role Name. Maxium 32 characters.'), |
|
128
|
|
|
]) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
3 |
|
return $this->validate($validator); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns table name mapped in the model. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
19 |
|
public function getSource(): string |
|
140
|
|
|
{ |
|
141
|
19 |
|
return 'roles'; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Check if the role existe in the db |
|
146
|
|
|
* |
|
147
|
|
|
* @param AclRole $role |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
3 |
|
public static function exist(AclRole $role): int |
|
151
|
|
|
{ |
|
152
|
3 |
|
return self::count([ |
|
153
|
3 |
|
'conditions' => 'name = ?0 AND companies_id = ?1 AND apps_id = ?2', |
|
154
|
3 |
|
'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
|
4 |
|
public static function isRole(string $roleName) : bool |
|
167
|
|
|
{ |
|
168
|
4 |
|
return (bool) self::count([ |
|
169
|
4 |
|
'conditions' => 'name = ?0 AND apps_id = ?1 AND companies_id in (?2, ?3)', |
|
170
|
4 |
|
'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
|
8 |
|
public static function getByName(string $name): Roles |
|
181
|
|
|
{ |
|
182
|
8 |
|
$role = self::findFirst([ |
|
183
|
8 |
|
'conditions' => 'name = ?0 AND apps_id = ?1 AND companies_id in (?2, ?3) AND is_deleted = 0', |
|
184
|
8 |
|
'bind' => [$name, Di::getDefault()->getAcl()->getApp()->getId(), Di::getDefault()->getAcl()->getCompany()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
|
185
|
|
|
]); |
|
186
|
|
|
|
|
187
|
8 |
|
if (!is_object($role)) { |
|
188
|
|
|
throw new ModelException(_('Roles ' . $role . ' not found on this app ' . Di::getDefault()->getAcl()->getApp()->getId() . ' AND Company' . Di::getDefault()->getAcl()->getCompany()->getId())); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
8 |
|
return $role; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get the entity by its name |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $name |
|
198
|
|
|
* @return Roles |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public static function getById(int $id): Roles |
|
201
|
|
|
{ |
|
202
|
1 |
|
return self::findFirst([ |
|
203
|
1 |
|
'conditions' => 'id = ?0 AND companies_id in (?1, ?2) AND apps_id in (?3, ?4) AND is_deleted = 0', |
|
204
|
1 |
|
'bind' => [$id, Di::getDefault()->getUserData()->default_company, 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
|
7 |
|
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
|
7 |
|
if (strpos($role, '.') === false) { |
|
218
|
|
|
throw new ServerErrorHttpException('ACL - We are expecting the app for this role'); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
7 |
|
$appRole = explode('.', $role); |
|
222
|
7 |
|
$role = $appRole[1]; |
|
223
|
7 |
|
$appName = $appRole[0]; |
|
224
|
|
|
|
|
225
|
|
|
//look for the app and set it |
|
226
|
7 |
|
if (!$app = Apps::getACLApp($appName)) { |
|
227
|
|
|
throw new ServerErrorHttpException('ACL - No app found for this role'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
7 |
|
return self::findFirst([ |
|
231
|
7 |
|
'conditions' => 'apps_id in (?0, ?1) AND companies_id in (?2 , ?3)', |
|
232
|
7 |
|
'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()->default_company; |
|
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
|
|
|
|