Skip to content

fix: multilevel permissions in can() method #1229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/no-merge-commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ jobs:
uses: actions/checkout@v4

- name: Run test
uses: NexusPHP/no-merge-commits@v2.2.1
uses: NexusPHP/no-merge-commits@v2.1.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
14 changes: 0 additions & 14 deletions docs/customization/user_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,3 @@ class UserModel extends ShieldUserModel
}
}
```

## Creating a Custom User Entity

Starting from v1.2.0, `UserModel` in Shield has the `createNewUser()` method to
create a new User Entity.

```php
$user = $userModel->createNewUser($data);
```

It takes an optional user data array as the first argument, and passes it to the
constructor of the `$returnType` class.

If your custom User entity cannot be instantiated in this way, override this method.
4 changes: 2 additions & 2 deletions docs/references/authentication/hmac.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ permissions the token grants to the user. Scopes are provided when the token is
cannot be modified afterword.

```php
$token = $user->generateHmacToken('Work Laptop', ['posts.manage', 'forums.manage']);
$token = $user->gererateHmacToken('Work Laptop', ['posts.manage', 'forums.manage']);
```

By default, a user is granted a wildcard scope which provides access to all scopes. This is the
same as:

```php
$token = $user->generateHmacToken('Work Laptop', ['*']);
$token = $user->gererateHmacToken('Work Laptop', ['*']);
```

During authentication, the HMAC Keys the user used is stored on the user. Once authenticated, you
Expand Down
2 changes: 1 addition & 1 deletion docs/user_management/forcing_password_reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if ($user->requiresPasswordReset()) {

!!! note

You can use the [force-reset](../references/controller_filters.md/#forcing-password-reset)
You can use the [force-reset](../references/controller_filters/#forcing-password-reset)
filter to check.

### Force Password Reset On a User
Expand Down
12 changes: 9 additions & 3 deletions src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function can(string ...$permissions): bool
if (strpos($permission, '.') === false) {
throw new LogicException(
'A permission must be a string consisting of a scope and action, like `users.create`.'
. ' Invalid permission: ' . $permission
. ' Invalid permission: ' . $permission
);
}

Expand All @@ -280,8 +280,14 @@ public function can(string ...$permissions): bool
}

// Check wildcard match
$check = substr($permission, 0, strpos($permission, '.')) . '.*';
if (isset($matrix[$group]) && in_array($check, $matrix[$group], true)) {
$checks = [];
$parts = explode('.', $permission);

for ($i = count($parts); $i > 0; $i--) {
$check = implode('.', array_slice($parts, 0, $i)) . '.*';
$checks[] = $check;
}
if (isset($matrix[$group]) && array_intersect($checks, $matrix[$group]) !== []) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Collectors/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function display(): string

$html = '<h3>Current User</h3>';
$html .= '<table><tbody>';
$html .= "<tr><td width=\"150\">User ID</td><td>#{$user->id}</td></tr>";
$html .= "<tr><td style='width:150px;'>User ID</td><td>#{$user->id}</td></tr>";
$html .= "<tr><td>Username</td><td>{$user->username}</td></tr>";
$html .= "<tr><td>Email</td><td>{$user->email}</td></tr>";
$html .= "<tr><td>Groups</td><td>{$groupsForUser}</td></tr>";
Expand Down
9 changes: 3 additions & 6 deletions src/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function registerAction(): RedirectResponse

// Save the user
$allowedPostFields = array_keys($rules);
$user = $users->createNewUser($this->request->getPost($allowedPostFields));
$user = $this->getUserEntity();
$user->fill($this->request->getPost($allowedPostFields));

// Workaround for email only registration/login
if ($user->username === null) {
Expand Down Expand Up @@ -159,14 +160,10 @@ protected function getUserProvider(): UserModel

/**
* Returns the Entity class that should be used
*
* @deprecated 1.2.0 No longer used.
*/
protected function getUserEntity(): User
{
$userProvider = $this->getUserProvider();

return $userProvider->createNewUser();
return new User();
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Entities/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ public function can(string $permission): bool
}

// Check wildcard match
$check = substr($permission, 0, strpos($permission, '.')) . '.*';
$checks = [];
$parts = explode('.', $permission);

return $this->permissions !== null && $this->permissions !== [] && in_array($check, $this->permissions, true);
for ($i = count($parts); $i > 0; $i--) {
$check = implode('.', array_slice($parts, 0, $i)) . '.*';
$checks[] = $check;
}

return $this->permissions !== null
&& $this->permissions !== []
&& array_intersect($checks, $this->permissions) !== [];
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,4 @@ private function checkReturnType(): void
throw new LogicException('Return type must be a subclass of ' . User::class);
}
}

/**
* Returns a new User Entity.
*
* @param array<string, array<array-key, mixed>|bool|float|int|object|string|null> $data (Optional) user data
*/
public function createNewUser(array $data = []): User
{
return new $this->returnType($data);
}
}
24 changes: 24 additions & 0 deletions tests/Authorization/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,28 @@ public function testCan(): void
$this->assertTrue($group2->can('users.edit'));
$this->assertFalse($group2->can('foo.bar'));
}

public function testCanNestedPerms(): void
{
$group = $this->groups->info('user');

$group->addPermission('foo.bar.*');
$group->addPermission('foo.biz.buz.*');

$this->assertTrue($group->can('foo.bar'));
$this->assertTrue($group->can('foo.bar.*'));
$this->assertTrue($group->can('foo.bar.baz'));
$this->assertTrue($group->can('foo.bar.buz'));
$this->assertTrue($group->can('foo.bar.buz.biz'));
$this->assertTrue($group->can('foo.biz.buz'));
$this->assertTrue($group->can('foo.biz.buz.*'));
$this->assertTrue($group->can('foo.biz.buz.bar'));
$this->assertFalse($group->can('foo'));
$this->assertFalse($group->can('foo.*'));
$this->assertFalse($group->can('foo.biz'));
$this->assertFalse($group->can('foo.buz'));
$this->assertFalse($group->can('foo.biz.*'));
$this->assertFalse($group->can('foo.biz.bar'));
$this->assertFalse($group->can('foo.biz.bar.buz'));
}
}
Loading