I need to get the group code of the current user in backend, how can I do this?
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 multipleusergroup-code
then you can check from it. (in this example we are checking user hasowners
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.