October CMS - 获取当前用户的组代码?

October CMS - get the group code of current user?

我需要在后台获取当前用户的群组代码,我该怎么做?

我猜你需要检查天气用户是否在组内,并且基于你需要强制执行一些 security/rights 等。

这是有用的代码。

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.

Hardik Satasiya 代码

的简化版本
$user = \BackendAuth::getUser();
$userGroupCodes = $user->getGroups()->lists('code');

$neededCode = 'owners';

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