i m trying to replicate role in roles tables using laravel-spatie-permission,
like i have already abc role with api guard , but i want to create same role with same permission , but it's throwing error like a role 'abc' already exists for guard 'api', so my question is is there any way that we can create same role with same permission ?
Thanks you
Even if it's possible don't create two roles with the same name instead you can do is to add a new column in your roles table with the name of DisplayName or any name that you want.
and on that you can add same same name for your roles but keep the original name column unique.
then show DisplayName to the user instead of name column
Related
I want to add 'OR' condition with middleware on laravel routes for Role and Permission. Can anyone please help me here. Below is the code which I am trying currently through some internet research but it is not working.
Route::get('get-all-users', 'API\Admin\AdminController#get_all_users')->middleware(['role:admin' OR 'permission:view.all.users']);
In this scenario, you will need to change the DB structure a bit. Tables would look like:
Actions
Roles
Role_action_mapping => This will have foreign keys of both the above tables as a many to many relationship.
Now, every action should have a role assigned to it. So, the permission view.all.users will have an entry in the table with admin(or any desired role) assigned to it by it's side in Role_action_mapping.
So all you will have to do is have a single middleware say permission:view.all.users on the route and just check if the current role of the user has an entry with view.all.users in the Role_action_mapping table.
I am using Spatie roles and permission package.
I have assigned permission to roles.
It stores in role_permission table
now i want to delete permission from role
i have tried
$role = Role::find($id);
$role->revokePermissionTo($request->permission_id);
but roles also get deleted from roles table
$role->revokePermissionTo() accepts Permission object or array and Permission name string or name string array. But you are passing the permission_id. That could be the issue.
You can try fetching the Permission object by permission_id first and pass it to the revokePermissionTo().
See the function signature here:
https://github.com/spatie/laravel-permission/blob/be87e2918a9fad30e5257bc336c89225c3e6eb5a/src/Traits/HasPermissions.php#L384
I have Model name Project and User by using laravel relationship.
- project and user using many to many relationship.
What i want is using laravel authentication giving roles to the user base on project.
Which mean :
user "Ali" have role "admin" in project "project a" ,
but in project "project b" the user "Ali" role is "member".
How can i implement it?
Problem fixed :
My solution is add additional data inside the project_users pivot table store as string role.
When create new project and add with additional data using :
$user->projects()->save($project, ['role' => 'Manager']);
When need to call it :
$user_role = $user->projects()->where('user_id', $user['id'])->first()->pivot->role;
It will return a string 'Manager'
and use it to assign the role.
$user->syncRoles($user_role);
Look at the following links which will help you to achieve it.
https://github.com/spatie/laravel-permission
https://www.itsolutionstuff.com/post/laravel-56-user-roles-and-permissions-acl-using-spatie-tutorialexample.html
You can add the Role data e.g. role_id with a separate Role table or just role in Project To User Pivot Table project_users and retrieve the role info from this relationship.
Check out this Saving Additional Data On A Pivot Table section at Update Many To Many Relationship Laravel Documentation.
How do i create an API that contains user, group and permissions.
I want to combine auth_user_group and auth_group_permissions. how to import both models in my code. I cannot find the models in django.contrib.auth.
There are no such models: auth_user_group is the intermediary table for the M2M relationshipt between users and groups and auth_group_permissionsthe intermediary table between Permission and Group
if you wan't to display the user's groups and permissions on your API you'll need to use the serializer relations http://www.django-rest-framework.org/api-guide/relations/
hope this helps
I have status select box with option New,Re-new,Delete . have roles admin,student,teacher. Now for admin all three select option can list where as for student and teacher I want to hide Delete option from dropdown.
How to do it in laravel ? Can it be a part of validation rule ? or I have to generate with if n else. For role and permission I am using entrust and confide.
{{Former::select('status'->options(Subject::getAllowedoption('status'))}}
in model class under $rule I have the option defined New|Re-new|Delete and I am trying to find a way to restrict option Delete for role teacher.