Skip to content

feat: add allow to cancel actions for custom cases #324

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
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
19 changes: 19 additions & 0 deletions docs/auth_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

- [Authentication Actions](#authentication-actions)
- [Configuring Actions](#configuring-actions)
- [Cancel Actions For Custom Cases](#cancel-actions-for-custom-cases)
- [Defining New Actions](#defining-new-actions)


Authentication Actions are a way to group actions that can happen after login or registration.
Shield ships with two actions you can use, and makes it simple for you to define your own.

Expand Down Expand Up @@ -55,6 +57,23 @@ Views for all of these pages are defined in the `Auth` config file, with the `$v
];
```

## Cancel Actions For Custom Cases

By default, if the actions are set with the `$actions` variable, Actions will be applied regardless of the limit.
If you need actions cancel for custom cases, you can complete the following cases.

```php
public array $cancelActions = [
'groups' => ['superadmin', 'admin'],
'permissions' => ['users.create', 'users.edit'],
'usersId' => null,
];
```

In the above example, given that the groups value and permissions are set, if the target user
has one of the `superadmin` or `admin` groups, or one of the permissions `users.create` or `users.edit`,
The actions is not executed for him.

## Defining New Actions

While the provided email-based activation and 2FA will work for many sites, others will have different
Expand Down
24 changes: 24 additions & 0 deletions src/Authentication/Authenticators/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ public function startUpAction(string $type, User $user): bool
return false;
}

$hasCancelAction = setting('Auth.cancelActions')['groups'] !== null
|| setting('Auth.cancelActions')['permissions'] !== null
|| setting('Auth.cancelActions')['usersId'] !== null;

if ($hasCancelAction) {
$userPermissions = $user->getPermissions();
$userGroups = $user->getGroups();
$userId = (array) $user->id;

$currentUserInfo = array_merge($userPermissions, $userGroups, $userId);

$casesforCancelAction = array_merge(
setting('Auth.cancelActions')['groups'] ?? [],
setting('Auth.cancelActions')['permissions'] ?? [],
setting('Auth.cancelActions')['usersId'] ?? [],
);

foreach ($casesforCancelAction as $casesCancel) {
if (in_array($casesCancel, $currentUserInfo, true)) {
return false;
}
}
}

$action = Factories::actions($actionClass); // @phpstan-ignore-line

// Create identity for the action.
Expand Down
24 changes: 24 additions & 0 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ class Auth extends BaseConfig
'register' => null,
];

/**
* --------------------------------------------------------------------
* Cancel Authentication Actions For Custom Cases
* --------------------------------------------------------------------
* By default, if the actions are set, they will be applied regardless of the limit.
* If you need actions cancel for custom cases,you can complete the following cases.
*
* Method of set values:
* - groups: ['superadmin', 'admin'],
* - permissions: ['users.create'],
* - usersId: ['1','2','3'],
*
* Example:
* If set this, actions not applay for users have groups "superadmin" or "admin"
* - groups: ['superadmin', 'admin'],
*
* @var array<string, array|null>
*/
public array $cancelActions = [
'groups' => ['superadmin', 'staff'],
'permissions' => null,
'usersId' => null,
];

/**
* --------------------------------------------------------------------
* Authenticators
Expand Down