I'm currently trying to figure out how to get the permissions for a role with Laravel and Entrust. It uses a pivot table called permission_role that has the role_id and permission_id stored in it.
What I'm trying to do is get the role with it's ID and then get the permissions associated with that role. I think I may be over complicating it, but since I have had zero luck on it I was hoping someone could help me out.
You may try this:
// Hope you have something like this:
class Role extends EntrustRole {}
Then get all permissions from the role whose id is 1:
$role = Role::with('perms')->find(1); // Assumed 1 is role id
dd($role->perms); // all permissions in the role
Related
does anyone know how to show multiple roles that are under a user? I have setup my permissions so a user can be under multiple roles, which is set easily enough using the following
$user->assignRole(['Root', 'IT', 'HR']); // assigning roles
I am however having trouble showing all roles a user relongs to in the same way?
Has anyone done this and knows how to? I simply want to show on a page which role a user belongs to.
Refer to this link
https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage
Get the names of the user's roles
$roles = $user->getRoleNames();
Returns a collection
if there are two tables (roles and user_roles) than you can check through inner join ('inner join' to check role must exist in 'roles' table that is assigned to a user) by passing user_id (user_id to get specific roles assigned by a user).
You can get a collection of roles assigned to the user via $user->roles
I am using Laravel built-in Authentication for users which I run from the terminal php artisan make:auth
It has one table in the database, users table and has an isAdmin column, so I am assuming it has two roles an admin and not_amin account.
Now, what I want is to modify this built-in functionality of Laravel make:auth to be able to add some roles like isEmployee and/or isCustomer.
How can I achieve this using the available option I have?
I hope, I somewhat explained my case. Thanks.
Having a column is_admin is targeted to only one role. This tells you if user is an admin or not.
I would suggest you to change depending on any of below cases :
1. If user can have just 1 role :
Modify users table and change is_admin to role. Inside role column, you can store the role of that particular user like admin, employee, 'customer` etc.
2. If user has multiple roles(many-to-many) :
Remove column is_admin from users table and create a new table role_user which will have user_id and role_id. You will need to have another table called roles to store all available roles in your system.
What is the most effective method to add User roles in Laravel 5.5?
I came across a couple of methods i.e.
Admin Middleware
Pivot Table for user_roles
I've tried and implemented both methods in my application but asking this question by keeping in mind my application's scalability on how they would be effective.
If you want you can add permissions on role table create a new permission table to define permissions to user.
Eg:
Without permission table: Based on cartalyst sentinel
User Table -> name, email, password etc
Role Table -> name, slug, permissions
Users_role -> user_id, role_id
With permission table: Based in Zizaco entrust
User Table -> name, email, password etc
Role Table -> name, slug, permissions
Users_role -> user_id, role_id
Permission table -> name, slug, description
role_permission -> permission_id, role_id
Why would you try to reinvent the wheel? Mayb you can use a package for that. The spatie/laravel-permission package is one I particulary like.
After a quick search I also found this discussion on laracasts with a few other options.
I'd say you need to have both: the middleware to check for access on each request & the permissions table (& pivot) for storing your permissions.
For a working example, you could:
take a look here at a package that is part of
a bigger app, and try out
a working demo
Disclaimer: I am a contributor to these packages.
In the above examples, in brief, permissions are tied to a role, which can be assigned to an owner, and the owner has users.
To me it looks that you need both of things mentioned in your question. You should have a Roles table that defines all possible roles a user can have in your application.
A user_roles pivot table that you would use to associate users to particular roles
And a Middleware which you will use to check for roles before authorizing user to access the functionality.
Detained demo is given in this link for roles based authentication in Laravel 5.5 https://www.5balloons.info/user-role-based-authentication-and-access-control-in-laravel/
I am new to Laravel 5.3 so bear with me ;)
I am using Laratrust for permission and roles, http://laratrust.readthedocs.io/en/3.0/configuration/models/user.html
I can do:
User::with('roles')->findOrFail( $id );
and get the user with the roles, but how can I get the user with a restructured variant of roles. Instead of the array with roles I want an array with a simplified variant of Role
Thank you in advance!
I think you're trying to find out what roles a user with a specific id has.
Given id is the id of the given user try this -
User::find($id)->roles()
This will give you a list of roles the user with the given id has.
I want to create dynamic permissions for each users through checkbox using Sentry in Laravel 4.
I've created a table named permissions that has two columns:
id | name | user_id
Now, I want to use:
Sentry::hasAccess('any_permission_name')
But this 'any_permission_name' should come from permissions table.
I've extend the SentrUserModel in my User model and add this relation to the User model:
public function permission(){
return $this->hasMany('Permission');
}
Now, I don't understand how to use Sentry with different permissions table for fetching the permissions.
Is there any way to use Sentry for this?