0
votes

I need to get the group code of the current user in backend, how can I do this?

2
The current user might be member of multiple groups. Check the following link, it may give you a clue: stackoverflow.com/a/47875582/69537B Faley

2 Answers

2
votes

Shorter version of Hardik Satasiya code

$user = \BackendAuth::getUser();
$userGroupCodes = $user->getGroups()->lists('code');

$neededCode = 'owners';

$hasPermission = in_array($neededCode, $userGroupCodes);
1
votes

I guess you need to check weather user is inside of group or not and based on you need to enforce some security/rights etc..

here is the code which can be useful.

user can have multiple usergroup so you will get multiple usergroup-code then you can check from it. (in this example we are checking user has owners code in his groups)

$user = \BackendAuth::getUser();
$currentUserGroups = $user->getGroups();
$userGroupCodes = [];

$neededCode = 'owners';

foreach ($currentUserGroups as $group) {
    $userGroupCodes[] = $group->code;
}

$hasPermission = false;
if(in_array($neededCode, $userGroupCodes)) {
    $hasPermission = true;
}

dd($hasPermission);

$hasPermission will have the Boolean value now you can use it in your condition and enforce security.