File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 6
6
7
7
use CodeIgniter \I18n \Time ;
8
8
use CodeIgniter \Shield \Authorization \AuthorizationException ;
9
+ use CodeIgniter \Shield \Exceptions \LogicException ;
9
10
use CodeIgniter \Shield \Models \GroupModel ;
10
11
use CodeIgniter \Shield \Models \PermissionModel ;
11
12
@@ -226,9 +227,18 @@ public function hasPermission(string $permission): bool
226
227
/**
227
228
* Checks user permissions and their group permissions
228
229
* to see if the user has a specific permission.
230
+ *
231
+ * @param string $permission string consisting of a scope and action, like `users.create`
229
232
*/
230
233
public function can (string $ permission ): bool
231
234
{
235
+ if (strpos ($ permission , '. ' ) === false ) {
236
+ throw new LogicException (
237
+ 'A permission must be a string consisting of a scope and action, like `users.create`. '
238
+ . ' Invalid permission: ' . $ permission
239
+ );
240
+ }
241
+
232
242
$ this ->populatePermissions ();
233
243
234
244
$ permission = strtolower ($ permission );
Original file line number Diff line number Diff line change 6
6
7
7
use CodeIgniter \I18n \Time ;
8
8
use CodeIgniter \Shield \Authorization \AuthorizationException ;
9
+ use CodeIgniter \Shield \Exceptions \LogicException ;
9
10
use CodeIgniter \Shield \Models \UserModel ;
10
11
use CodeIgniter \Test \DatabaseTestTrait ;
11
12
use Locale ;
@@ -299,6 +300,16 @@ public function testCanCascadesToGroupsWithWildcards(): void
299
300
$ this ->assertTrue ($ this ->user ->can ('admin.access ' ));
300
301
}
301
302
303
+ public function testCanGetsInvalidPermission (): void
304
+ {
305
+ $ this ->expectException (LogicException::class);
306
+ $ this ->expectExceptionMessage ('Invalid permission: developer ' );
307
+
308
+ $ this ->user ->addGroup ('superadmin ' );
309
+
310
+ $ this ->assertTrue ($ this ->user ->can ('developer ' ));
311
+ }
312
+
302
313
/**
303
314
* @see https://github.com/codeigniter4/shield/pull/238
304
315
*/
You can’t perform that action at this time.
0 commit comments