Search code examples
octobercmsoctobercms-pluginsoctobercms-backend

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?


Solution

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