assign role and permission to a user dynamically laravel 7
i created the system where permission are created dynamically and as well as roles. then the permissions are assigned to roles and roles are then assigned to users.
and when i run the below code in edit function of propertiescontroller
if (Gate::allows('property_edit')) {
dd('yes');
}else{
dd('no');
}
and hence i created the permission named as 'property_edit' and assign the permission to role 'Admin' and assign this role to admin user still i m getting no
@salmankhan2482 I follow Laracasts ways (https://laracasts.com/search?query=acl), declare all gate definition with all permission code/name and check the roles at boot method in AuthServiceProvider.php. Actually is quite similar as what Snapey shown below, just slightly amendment.
typically you would register the gate inside a loop
foreach (Permission::all() as $permission) {
Gate::define($permission->name, function (User $user, $thing) use ($permission){
$action= $permission->name;
// do your check, can $user do $action with $thing
});
}
so then you need to check if the passed user has the explicit permission, or a role that grants that permission
@Snapey sir there is a many to many relationship between roles and permission and users and roles.
and you mentioned thing in the above query. what does thing means right there in above query ?