entrust icon indicating copy to clipboard operation
entrust copied to clipboard

User Roles on differents spaces

Open lsteamgeo opened this issue 8 years ago • 10 comments

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

lsteamgeo avatar Aug 30 '16 16:08 lsteamgeo

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...

matomoravcik avatar Sep 01 '16 10:09 matomoravcik

Hello there,

maybe prefixes?

for example: space1-comments_edit, space2-comments_view etc

vkarampinis avatar Sep 18 '16 18:09 vkarampinis

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.

lsteamgeo avatar Jan 31 '17 20:01 lsteamgeo

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.

matomoravcik avatar Feb 02 '17 10:02 matomoravcik

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

lsteamgeo avatar Feb 02 '17 11:02 lsteamgeo

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 avatar Feb 02 '17 11:02 matomoravcik

@matomoravcik ,

Thank you very much.

I'll look at your method.

Thanks again,

Jeffrey

lsteamgeo avatar Feb 02 '17 18:02 lsteamgeo

@matomoravcik , please Its posible a one more help. For example, what about relationships ? THANKS A LOT

cpndaj avatar Feb 20 '19 15:02 cpndaj

Please any help ?

cpndaj avatar Mar 09 '19 20:03 cpndaj

A very basic method: would be creating the same roles with different permissions!

Mohammad-Yasin-Noori avatar Jun 15 '19 10:06 Mohammad-Yasin-Noori