Check if Backend User belongs to Group with OctoberCMS Laravel?
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
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad