skip to Main Content

i have 2 roles writer and admin.

writer role has permission to create article, edit article, delete article

how can I remove the create article permission for a specified user without removing removing it from the writer role ?

2

Answers


  1. Roles and permissions are not necessarily the same thing. In the Spatie library you mentioned, you can use roles to group different permissions to make it easier for you to manage them.

    So instead of you assigning different permissions to every single user like "can-create-post," "can-edit-post," etc., you create a role, for example, "publisher," and assign the permissions to that role, then assign the role to the user you want.

    If I were you, I would create the "Writer" role like you mentioned, assign it the RUD (read, update, delete) permissions, and assign that role to the users who should have that ability. On top of that, in the admin panel, I would have a checkbox or something similar where the admin can tick "Can create article." If it’s ticked, on the backend, what would be called is:

        $user->assignRole('writer');
        $user->givePermissionTo('create-article');
    

    If not, then it would be just the

    $user->assignRole('writer');
    

    And if you ever wanted to edit a user and remove his ability to create posts, you would open hi profile in the admin panel and untick the checkbox which would call the

    $user->revokePermissionTo('create-article');
    

    Hope this was of help 🙂

    Login or Signup to reply.
  2. A permission can be revoked from a user:

    $user->revokePermissionTo('create article');
    

    you can checkout the documentation of Spatie for more information about Direct Permissions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search