Check if Backend User belongs to Group with OctoberCMS Laravel?

- 1 answer

Ad

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

Ad

Answer

Ad

There could be some helper functions for this but you can also use this:

$backend_user->groups()->whereName('administrators')->exists();
Ad
source: stackoverflow.com
Ad