我使用的是基於Laravel的OctoberCMS。用OctoberCMS Laravel檢查後端用戶是否屬於Group?
我有一個後端用戶,Matt
組owners
,administrators
。
如何檢查用戶是否屬於特定組以允許身份驗證?
我被告知需要傳遞一個組對象,但我不知道那是什麼。
use Auth;
use BackendAuth;
use Backend\Models\User;
if (BackendAuth::check()) {
// get current backend user
$backend_user = BackendAuth::getUser();
// get current backend user's groups
$backend_user_groups = Backend::getUser()->groups;
// authenticate
if ($backend_user->inGroup('administrators') {
}
}
錯誤
public function inGroup($group)
Call to a member function getKey() on string
另一種方法我試過
if (User::inGroup('administrators') {
}
錯誤
Non-static method October\Rain\Auth\Models\User::inGroup() should not be called statically
文檔
https://octobercms.com/docs/backend/users
https://github.com/octobercms/library/blob/master/src/Auth/Models/User.php
有沒有理由檢查一個組,而不是權限? – dragontree
@dragontree在OctoberCMS中,我製作了具有一組權限的後端用戶組。我向管理員或版主添加用戶,然後在php中檢查當前用戶的組以允許訪問功能。我應該以不同的方式去做嗎? –
你應該使用'hasAccess'或'hasPermission()'函數。 其記錄在這裏:https://octobercms.com/docs/backend/users#features – dragontree