minaremonshaker's avatar

Is it a good practice to assign direct permissions to users when using the Spatie permissions package?

hi i am using spaite permissions for authorization , I want to ask what are the situation where i can assign direct permissions to users

0 likes
6 replies
LaryAI's avatar
Level 58

Certainly! Here’s a detailed answer to your question about assigning direct permissions to users when using the Spatie permissions package:

Assigning direct permissions to users (as opposed to assigning permissions via roles) is supported by the Spatie package, but it comes with considerations regarding maintainability and best practices.

Best Practice: Assign via Roles

  • Roles are generally preferred for grouping permissions. When you assign permissions to a role and then assign that role to a user, it’s easier to manage and audit permissions across your application.
  • For example, updating a role instantly updates permissions for all users with that role.

When to Assign Direct Permissions Assigning permissions directly to users can be useful in specific scenarios:

  1. Exceptional Privileges:
    When a user needs access to a feature outside of their assigned roles. E.g., a regular user needs to moderate a specific forum for a week.
  2. Temporary Access:
    If a user needs direct, short-term elevated privileges that shouldn’t last beyond a certain period.
  3. Custom-tailored Access:
    When a user’s permissions don’t fit exactly into any role, and creating a new role would be overkill.
  4. Granular Restrictions:
    In rare cases where you need to finely tune permissions on a per-user basis for business needs.

How to Assign Direct Permissions

$user = User::find(1);

// Give permission directly to the user
$user->givePermissionTo('edit articles');

// Check for a permission
$user->can('edit articles');

Drawbacks of Direct Permissions

  • Difficult to scale and audit as the user base grows.
  • Harder to manage during permission changes or staff turnover.
  • Increased risk of inconsistency.

Recommendation

  • Use roles for standardizing permissions across users.
  • Limit direct permissions to specific, justified cases.
  • Always document direct assignments for future audits.

References:

Summary:
Assigning direct permissions to users is possible, but should be used sparingly, mainly for exceptions or temporary access. Prefer role-based permission management for clarity and easier maintenance.

vincent15000's avatar

The Spatie package recommends as a best pratice to assign permissions to roles and roles to users.

https://spatie.be/docs/laravel-permission/v6/best-practices/roles-vs-permissions

But you can assign both direct permissions and/or roles, it really depends on what you need in your application.

Typical situation where you could need to assign permissions to users is when you have a user for which you want to give permissions that don't correspond to any roles. But even in this situation you can create a specific role just for this user.

You can consider that assigning direct permissions to users lets you set a fine configuration.

1 like
jlrdw's avatar
jlrdw
Best Answer
Level 75

Look at it like this.

A person with a certain role either can or cannot.

Take an app where you have bookkeeping. In one of mine an admin can view only. The bookkeeping is for the bookkeeper.

Now suppose you have Sally who is an admin and does bookkeeping. She would have both roles. So a controller method dealing with bookkeeping would check and verify Sally can do whatever is in that controller.

See this article from @martinbean where he has a good role based setup.

https://martinbean.dev/blog/2021/07/29/simple-role-based-authentication-laravel/

Myself I like protecting at controller level.

And you do understand how to ensure a user can see and edit their own data, correct? I generally use a query scope for that with the authenticated users id in the query, never rely on what's in a query string.

Edit:

Of course the exception to this is if the Boss or Admin (a known trusted person) is editing employee data then he or she can freely use the query string data as they have no malicious intent. They may be updating an employees pay, performing a performance report, etc.

And just my opinion but you don't really need Spatie for such things, learn how to implement a good RBAC system. Yes there are "packages", but I write my own RBAC.

1 like
martinbean's avatar

I want to ask what are the situation where i can assign direct permissions to users

@minaremonshaker In cases when you want to assign a specific permission to a user that does not come from a role as normal.

2 likes
Snapey's avatar

imagine a mid sized business that has a number of roles. Sometimes there are job overlaps. You are a manager but sometimes you need to perform certain functions that normally the accounts team would do such as approving a new supplier. Unless you can give this manager specific individual permissions, you end up needing to give them the whole accounts role as well as their manager role, or you have to create a new role that is all the manager permissions plus the one accounting permission needed.

It is also useful for times of sickness or holiday cover. You can temporarily assign someone additional permissions without also giving it to other managers.

There is nothing you can do with direct permissions that you cannot do by creating a new role and then assigning that role to one person. Its just more clumsy and harder to train for.

2 likes
vincent15000's avatar

I had tested the Spatie package in the past in a personal project, but I never used it in an application for a client, I just create my own roles and permissions functionalities, it's really easy and doesn't take so much time.

1 like

Please or to participate in this conversation.