Ad

October CMS - Get The Group Code Of Current User?

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

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad