Skip to content

Commit 2294c68

Browse files
committed
fix: allow single-segment user permissions
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 498b35d commit 2294c68

4 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎docs/quick_start_guide/using_authorization.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ When a user registers on your site, they are assigned the group specified at `Co
2222

2323
### Change Available Permissions
2424

25-
The permissions on the site are stored in the `AuthGroups` config file also. Each one is defined by a string with dot-separated segments, like `users.create` or `forum.posts.create`.
25+
The permissions on the site are stored in the `AuthGroups` config file also. Permissions are usually written with dot-separated segments, like `users.create` or `forum.posts.create`, but single-segment permissions are also allowed.
2626

2727
```php
2828
public array $permissions = [

‎docs/references/authorization.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public string $defaultGroup = 'user';
3636
## Defining Available Permissions
3737

3838
Permissions that can be assigned directly to users must be added to the `AuthGroups` config file.
39-
A permission is a string consisting of dot-separated segments, like `users.create` or
40-
`forum.posts.create`. Each permission can have a description for display within UIs if needed.
39+
A permission is a string, usually written with dot-separated segments like `users.create` or
40+
`forum.posts.create`. Single-segment permissions are also allowed. Each permission can have a description for display within UIs if needed.
4141

4242
```php
4343
public array $permissions = [

‎src/Authorization/Traits/Authorizable.php‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\I18n\Time;
1717
use CodeIgniter\Shield\Authorization\AuthorizationException;
1818
use CodeIgniter\Shield\Authorization\PermissionMatcher;
19-
use CodeIgniter\Shield\Exceptions\LogicException;
2019
use CodeIgniter\Shield\Models\GroupModel;
2120
use CodeIgniter\Shield\Models\PermissionModel;
2221

@@ -256,7 +255,7 @@ public function hasPermission(string $permission): bool
256255
* Checks user permissions and their group permissions
257256
* to see if the user has one or more permissions.
258257
*
259-
* @param string $permissions Dot-separated permission string(s), like `users.create`
258+
* @param string $permissions Permission string(s), usually dot-separated like `users.create`
260259
*/
261260
public function can(string ...$permissions): bool
262261
{
@@ -270,14 +269,6 @@ public function can(string ...$permissions): bool
270269
$matrix = setting('AuthGroups.matrix');
271270

272271
foreach ($permissions as $permission) {
273-
// Permission must contain at least two dot-separated segments.
274-
if (! str_contains($permission, '.')) {
275-
throw new LogicException(
276-
'A permission must be a dot-separated string, like `users.create`.'
277-
. ' Invalid permission: ' . $permission,
278-
);
279-
}
280-
281272
$permission = strtolower($permission);
282273

283274
// Check user's permissions

‎tests/Authorization/AuthorizableTest.php‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\I18n\Time;
1717
use CodeIgniter\Shield\Authorization\AuthorizationException;
18-
use CodeIgniter\Shield\Exceptions\LogicException;
1918
use CodeIgniter\Shield\Models\UserModel;
2019
use Locale;
2120
use Tests\Support\DatabaseTestCase;
@@ -353,12 +352,22 @@ public function testCanChecksLaterPermissionsWithoutGroups(): void
353352
$this->assertTrue($this->user->can('beta.access', 'admin.access'));
354353
}
355354

356-
public function testCanGetsInvalidPermission(): void
355+
public function testCanChecksSingleSegmentPermission(): void
357356
{
358-
$this->expectException(LogicException::class);
359-
$this->expectExceptionMessage('Invalid permission: developer');
357+
$this->addConfigPermissions([
358+
'developer' => 'Can access developer tools',
359+
]);
360360

361-
$this->user->addGroup('superadmin');
361+
$this->user->addPermission('developer');
362+
363+
$this->assertTrue($this->user->can('developer'));
364+
}
365+
366+
public function testCanChecksSingleSegmentGroupPermission(): void
367+
{
368+
$this->setGroupPermissions('developer', ['developer']);
369+
370+
$this->user->addGroup('developer');
362371

363372
$this->assertTrue($this->user->can('developer'));
364373
}

0 commit comments

Comments
 (0)