entrust
entrust copied to clipboard
User Roles on differents spaces
Hello, I would like your idea about a problem I have. My application has Users who have Roles and Permissions. The difficulty I have is that on the application the User can choose several spaces. Space is an establishment (like a store). According to the chosen space, the User will not have the same Role and therefore the same Permissions. The Role and permissions of the User will be linked to the selected space. The User can have a Admin role on the Establisment1 and a simple User role on the Establishment2 What solution would you recommend ? Best regards
I have same problem, my example: we ave application where one user can be assigned to several companies, and user can have different roles in different companies. For example I as a user can be "Admin" in Company 1 but "Member" in Company 2... is it some workaround for such case or? I can see only fork and adjust of this package as solution for this case now...
Hello there,
maybe prefixes?
for example:
space1-comments_edit
, space2-comments_view
etc
Hello,
I'm looking for a solution but I still have not found it. The prefix method is a good idea but not very practical to use (in my case). Has anyone tried anything else?
Thank you for your help.
Hi @lsteamgeo, i did this by overloading some entrust classes / traits: EntrustRole EntrustRoleInterface EntrustRoleTrait EntrustUserTrait basically you need to change function like can, ability, attachRole an so. Everything is working for my app - it's already in production. You only need to watch entrust updates and updates your custom classes if you use most recent updates of entrust package.
HI @matomoravcik ,
Thank you very much for your message.
Is there a place where I can see your changes?
My case : I have several establishments. A user belongs to one or many establishments. In each establishment the user can have one or more roles (different depending on the establishment).
Best regards,
Jeffrey
Sorry @lsteamgeo i can't send you those files - it's commercial project of company where i work but i can help you little bit:
public function attachRole($role, $companyId = null)
{
if(is_object($role)) {
$role = $role->getKey();
}
if(is_array($role)) {
$role = $role['id'];
}
// if companyId is null load default companyId from user model
$companyId = ($companyId == null) ? Auth::user()->getCompanyId() : $companyId;
// we need to save userHasCompany_id as well for onDelete cascade so wee get this id and put it in role_user pivot table
$userHasCompanyId = UserHasCompany::where('userId', '=', $this->id)->where('companyId', '=', $companyId)->value('id');
$this->roles()->attach($role, ['company_id' => $companyId, 'userHasCompany_id' => ($userHasCompanyId) ? $userHasCompanyId : null]);
}
As you can see there is $companyId added (in my case i have one application user but many companies where he can be attached with different roles - same as your establishment).
So you should copy EntrustUserTrait somewhere to your application for example to "App\Traits\CustomEntrustUserTrait". Next you have to rework at least these functions of this new file:
public function attachRole($role, $companyId = null); // like i sent
public function cachedRoles($companyId = null);
public function hasRole($name, $requireAll = false, $companyId = null);
public function can($permission, $requireAll = false, $companyId = null);
public function ability($roles, $permissions, $options = [], $companyId = null);
After all these changes you should change in you auth user model entrust trait to you new "CustomEntrustTrait".
Hope it will help
@matomoravcik ,
Thank you very much.
I'll look at your method.
Thanks again,
Jeffrey
@matomoravcik , please Its posible a one more help. For example, what about relationships ? THANKS A LOT
Please any help ?
A very basic method: would be creating the same roles with different permissions!