4
4
5
5
use CodeIgniter \I18n \Time ;
6
6
use CodeIgniter \Shield \Authorization \AuthorizationException ;
7
- use Exception ;
7
+ use CodeIgniter \Shield \Authorization \Models \GroupModel ;
8
+ use CodeIgniter \Shield \Authorization \Models \PermissionModel ;
8
9
9
10
trait Authorizable
10
11
{
@@ -19,9 +20,8 @@ trait Authorizable
19
20
public function addGroup (string ...$ groups ): self
20
21
{
21
22
$ this ->populateGroups ();
22
- $ configGroups = function_exists ('setting ' )
23
- ? array_keys (setting ('AuthGroups.groups ' ))
24
- : array_keys (config ('AuthGroups ' )->groups );
23
+
24
+ $ configGroups = $ this ->getConfigGroups ();
25
25
26
26
$ groupCount = count ($ this ->groupCache );
27
27
@@ -43,7 +43,7 @@ public function addGroup(string ...$groups): self
43
43
44
44
// Only save the results if there's anything new.
45
45
if (count ($ this ->groupCache ) > $ groupCount ) {
46
- $ this ->saveGroupsOrPermissions ( ' group ' );
46
+ $ this ->saveGroups ( );
47
47
}
48
48
49
49
return $ this ;
@@ -66,7 +66,7 @@ public function removeGroup(string ...$groups): self
66
66
$ this ->groupCache = array_diff ($ this ->groupCache , $ groups );
67
67
68
68
// Update the database.
69
- $ this ->saveGroupsOrPermissions ( ' group ' );
69
+ $ this ->saveGroups ( );
70
70
71
71
return $ this ;
72
72
}
@@ -83,9 +83,8 @@ public function removeGroup(string ...$groups): self
83
83
public function syncGroups (array $ groups ): self
84
84
{
85
85
$ this ->populateGroups ();
86
- $ configGroups = function_exists ('setting ' )
87
- ? array_keys (setting ('AuthGroups.groups ' ))
88
- : array_keys (config ('AuthGroups ' )->groups );
86
+
87
+ $ configGroups = $ this ->getConfigGroups ();
89
88
90
89
foreach ($ groups as $ group ) {
91
90
if (! in_array ($ group , $ configGroups , true )) {
@@ -94,7 +93,7 @@ public function syncGroups(array $groups): self
94
93
}
95
94
96
95
$ this ->groupCache = $ groups ;
97
- $ this ->saveGroupsOrPermissions ( ' group ' );
96
+ $ this ->saveGroups ( );
98
97
99
98
return $ this ;
100
99
}
@@ -130,9 +129,8 @@ public function getPermissions(): ?array
130
129
public function addPermission (string ...$ permissions ): self
131
130
{
132
131
$ this ->populatePermissions ();
133
- $ configPermissions = function_exists ('setting ' )
134
- ? array_keys (setting ('AuthGroups.permissions ' ))
135
- : array_keys (config ('AuthGroups ' )->permissions );
132
+
133
+ $ configPermissions = $ this ->getConfigPermissions ();
136
134
137
135
$ permissionCount = count ($ this ->permissionsCache );
138
136
@@ -154,7 +152,7 @@ public function addPermission(string ...$permissions): self
154
152
155
153
// Only save the results if there's anything new.
156
154
if (count ($ this ->permissionsCache ) > $ permissionCount ) {
157
- $ this ->saveGroupsOrPermissions ( ' permission ' );
155
+ $ this ->savePermissions ( );
158
156
}
159
157
160
158
return $ this ;
@@ -177,7 +175,7 @@ public function removePermission(string ...$permissions): self
177
175
$ this ->permissionsCache = array_diff ($ this ->permissionsCache , $ permissions );
178
176
179
177
// Update the database.
180
- $ this ->saveGroupsOrPermissions ( ' permission ' );
178
+ $ this ->savePermissions ( );
181
179
182
180
return $ this ;
183
181
}
@@ -194,9 +192,8 @@ public function removePermission(string ...$permissions): self
194
192
public function syncPermissions (array $ permissions ): self
195
193
{
196
194
$ this ->populatePermissions ();
197
- $ configPermissions = function_exists ('setting ' )
198
- ? array_keys (setting ('AuthGroups.permissions ' ))
199
- : array_keys (config ('AuthGroups ' )->permissions );
195
+
196
+ $ configPermissions = $ this ->getConfigPermissions ();
200
197
201
198
foreach ($ permissions as $ permission ) {
202
199
if (! in_array ($ permission , $ configPermissions , true )) {
@@ -205,7 +202,7 @@ public function syncPermissions(array $permissions): self
205
202
}
206
203
207
204
$ this ->permissionsCache = $ permissions ;
208
- $ this ->saveGroupsOrPermissions ( ' permission ' );
205
+ $ this ->savePermissions ( );
209
206
210
207
return $ this ;
211
208
}
@@ -218,6 +215,7 @@ public function syncPermissions(array $permissions): self
218
215
public function hasPermission (string $ permission ): bool
219
216
{
220
217
$ this ->populatePermissions ();
218
+
221
219
$ permission = strtolower ($ permission );
222
220
223
221
return in_array ($ permission , $ this ->permissionsCache , true );
@@ -230,6 +228,7 @@ public function hasPermission(string $permission): bool
230
228
public function can (string $ permission ): bool
231
229
{
232
230
$ this ->populatePermissions ();
231
+
233
232
$ permission = strtolower ($ permission );
234
233
235
234
// Check user's permissions
@@ -291,12 +290,10 @@ private function populateGroups(): void
291
290
return ;
292
291
}
293
292
294
- $ groups = db_connect ()->table ('auth_groups_users ' )
295
- ->where ('user_id ' , $ this ->id )
296
- ->get ()
297
- ->getResultArray ();
293
+ /** @var GroupModel $groupModel */
294
+ $ groupModel = model (GroupModel::class);
298
295
299
- $ this ->groupCache = array_column ( $ groups , ' group ' );
296
+ $ this ->groupCache = $ groupModel -> getByUserId ( $ this -> id );
300
297
}
301
298
302
299
/**
@@ -309,50 +306,57 @@ private function populatePermissions(): void
309
306
return ;
310
307
}
311
308
312
- $ permissions = db_connect ()->table ('auth_permissions_users ' )
313
- ->where ('user_id ' , $ this ->id )
314
- ->get ()
315
- ->getResultArray ();
309
+ /** @var PermissionModel $permissionModel */
310
+ $ permissionModel = model (PermissionModel::class);
316
311
317
- $ this ->permissionsCache = array_column ( $ permissions , ' permission ' );
312
+ $ this ->permissionsCache = $ permissionModel -> getByUserId ( $ this -> id );
318
313
}
319
314
320
315
/**
321
- * Inserts or Updates either the current groups
322
- * or the current permissions.
316
+ * Inserts or Updates the current groups.
317
+ */
318
+ private function saveGroups (): void
319
+ {
320
+ /** @var GroupModel $model */
321
+ $ model = model (GroupModel::class);
322
+
323
+ $ cache = $ this ->groupCache ;
324
+
325
+ $ this ->saveGroupsOrPermissions ('group ' , $ model , $ cache );
326
+ }
327
+
328
+ /**
329
+ * Inserts or Updates either the current permissions.
330
+ */
331
+ private function savePermissions (): void
332
+ {
333
+ /** @var PermissionModel $model */
334
+ $ model = model (PermissionModel::class);
335
+
336
+ $ cache = $ this ->permissionsCache ;
337
+
338
+ $ this ->saveGroupsOrPermissions ('permission ' , $ model , $ cache );
339
+ }
340
+
341
+ /**
342
+ * @phpstan-param 'group'|'permission' $type
323
343
*
324
- * @throws Exception
344
+ * @param GroupModel|PermissionModel $model
325
345
*/
326
- private function saveGroupsOrPermissions (string $ type ): void
346
+ private function saveGroupsOrPermissions (string $ type, $ model , array $ cache ): void
327
347
{
328
- $ table = $ type === 'group '
329
- ? 'auth_groups_users '
330
- : 'auth_permissions_users ' ;
331
- $ cache = $ type === 'group '
332
- ? $ this ->groupCache
333
- : $ this ->permissionsCache ;
334
-
335
- $ existing = db_connect ()->table ($ table )
336
- ->where ('user_id ' , $ this ->id )
337
- ->get ()
338
- ->getResultArray ();
339
- $ existing = array_column ($ existing , $ type );
348
+ $ existing = $ model ->getByUserId ($ this ->id );
340
349
341
350
$ new = array_diff ($ cache , $ existing );
342
351
343
352
// Delete any not in the cache
344
- if (count ($ cache )) {
345
- db_connect ()->table ($ table )
346
- ->where ('user_id ' , $ this ->id )
347
- ->whereNotIn ($ type , $ cache )
348
- ->delete ();
353
+ if ($ cache !== []) {
354
+ $ model ->deleteNotIn ($ this ->id , $ cache );
349
355
}
350
356
// Nothing in the cache? Then make sure
351
357
// we delete all from this user
352
358
else {
353
- db_connect ()->table ($ table )
354
- ->where ('user_id ' , $ this ->id )
355
- ->delete ();
359
+ $ model ->deleteAll ($ this ->id );
356
360
}
357
361
358
362
// Insert new ones
@@ -366,7 +370,28 @@ private function saveGroupsOrPermissions(string $type): void
366
370
'created_at ' => Time::now ()->toDateTimeString (),
367
371
];
368
372
}
369
- db_connect ()->table ($ table )->insertBatch ($ inserts );
373
+
374
+ $ model ->insertBatch ($ inserts );
370
375
}
371
376
}
377
+
378
+ /**
379
+ * @return string[]
380
+ */
381
+ private function getConfigGroups (): array
382
+ {
383
+ return function_exists ('setting ' )
384
+ ? array_keys (setting ('AuthGroups.groups ' ))
385
+ : array_keys (config ('AuthGroups ' )->groups );
386
+ }
387
+
388
+ /**
389
+ * @return string[]
390
+ */
391
+ private function getConfigPermissions (): array
392
+ {
393
+ return function_exists ('setting ' )
394
+ ? array_keys (setting ('AuthGroups.permissions ' ))
395
+ : array_keys (config ('AuthGroups ' )->permissions );
396
+ }
372
397
}
0 commit comments