Saturday, March 4, 2017

Check if Backend User belongs to Group with OctoberCMS Laravel?

I'm using OctoberCMS based on Laravel.

I have a backend user, Matt in Groups owners, administrators.

How do I check if user belongs to a specific group to allow authentication?

I was told I need to pass a Group Object, but I don't know what that is.

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') {

    }
}

Error

public function inGroup($group)
Call to a member function getKey() on string

Another way I've tried

if (User::inGroup('administrators') {

} 

Error

Non-static method October\Rain\Auth\Models\User::inGroup() should not be called statically

Docs

https://octobercms.com/docs/backend/users

https://github.com/octobercms/library/blob/master/src/Auth/Models/User.php



via Matt McManis

Advertisement