-
Notifications
You must be signed in to change notification settings - Fork 58
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
Filter Transitions deending on user role or permission #20
Comments
Hello, you could achieve this in several ways, but it depends on how your roles/permissions are structured. If you are using gates/policies as guards for the transitions, Could give me an example of what would you like to achieve? Are you interested in the transitions allowed for the authenticated user? Or do you want to check another |
Hello |
You're welcome! You can definitely use this package, and it will work the same as if you had your own gates/policies. Cheers! |
Hello @himan72 , Here is an example of some permissions set up with spatie/laravel permissions: $writer = Role::findByName('writer');
$writer->givePermissionTo('submit articles');
$admin = Role::findByName('admin');
$admin->givePermissionTo('submit articles');
$admin->givePermissionTo('publish articles');
$admin->givePermissionTo('unpublish articles');
$user->assignRole('writer');
$superUser->assignRole('admin'); And a state machine configuration: The name of the permission in the 'articles' => [
'class' => App\Article::class,
'graph' => 'default',
'property_path' => 'state',
'states' => [
'pending',
'unpublished',
'published',
],
'transitions' => [
'submit' => [
'from' => ['pending'],
'to' => 'unpublished',
],
'publish' => [
'from' => ['unpublished'],
'to' => 'published',
],
'unpublish' => [
'from' => ['published'],
'to' => 'unpublished',
],
],
'callbacks' => [
'guard' => [
'guard_on_submitting' => [
'on' => 'submit',
'can' => 'submit articles',
],
'guard_on_publishing' => [
'on' => 'publish',
'can' => 'publish articles',
],
'guard_on_unpublishing' => [
'on' => 'unpublish',
'can' => 'unpublish articles',
],
],
],
], So when the $sm = StateMachine::get($article);
$sm->getPossibleTransitions(); // ['submit'] But when the $sm = StateMachine::get($article);
$sm->getPossibleTransitions(); // ['submit', 'publish', 'unpublish'] |
Wouldn't it check the gates on the policies for |
Yep I plan to use a policy to handle the permissions.
Le dim. 5 mai 2019 08:21, divan-mt <[email protected]> a écrit :
… And a state machine configuration: The name of the permission in the can
keys *must match* the permissions given above.
Wouldn't it check the gates on App\Article rather than the user
permissions?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#20 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABTSI2K7DHSSOTIFDU7EWYDPT2KGZANCNFSM4HHZVNUA>
.
|
I believe with this configuration that it would NOT check the policies. Using the |
@sebdesign Actually, in the project I'm working on right now, I have created policies for the Object and it works without explicitly having //state-machine.php
'guard_on_ask_for_employee_changes' => [
'on' => 'ask_for_employee_changes',
'can' => 'ask-for-employee-changes',
],
//TrainingRequestPolicy.php
public function askForEmployeeChanges(User $user, TrainingRequest $trainingRequest)
{
if (null === $user) {
return false;
}
if ($user->can('ask for department employee changes') && RequestState::PendingDepartmentHeadReview === $trainingRequest->last_state) {
return true;
}
if ($user->can('ask for any employee changes') && RequestState::PendingHRTeamReview === $trainingRequest->last_state) {
return true;
}
return false;
} |
Hello
Is there any way to filter the allowed transitions depending on the auth user role(s) or permission(s) ?
Thanks for your support
The text was updated successfully, but these errors were encountered: