This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Laravel
BackendAuth::getUser()->groups;
Twig (Frontend Users)
{{ user.groups }}
Twig (Backend Users)
{{ backend.user.groups }}
This is not the correct code. Does anyone know what it is?
In the Code section of the page, you need to assign the current backend user to a variable, like this:
function onStart() {
$this['user'] = BackendAuth::getUser();
}
Then, in your page markup, you can use the following to display all group names:
{% for group in user.groups %}
{{ group.name }}
{% endfor %}
It works great. Thanks.
I have another quick question, how to verify which groups the Backend User is in, so I can use it in an If statement, instead of displaying in a text list.
My variable is:
$this['backend_user'] = BackendAuth::getUser();
First I tried to use:
{% if backend_user.groups.0.code == "moderators" %}
It works at first, but if the User is not part of a group, it will error Method name must be a string
. If the User is part of multiple groups, you have to change the number 0 to match.
So I tried just seaching the groups array for a key:
{% if "moderators" in backend_user.groups | keys %}
This works, it finds "moderators" in the list, but is it the best way to do this?
Last updated
The User model has a dedicated function for this purpose: public function inGroup($group)
Please note, that you need to pass a valid Group object to this function, not just a name.
alxy said:
The User model has a dedicated function for this purpose:
public function inGroup($group)
Please note, that you need to pass a valid Group object to this function, not just a name.
I'm not using it right, not sure where to put or call the function.
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
I also try to use with twig
// single group
{% if inGroup('owners') %}
You are in Owners group.
{% endif %}
// multiple groups
{% if inGroups(['owners', 'administrators', 'moderators']) %}
You are in a Backend group.
{% endif %}
But I get error
Unknown "inGroup" function.
Unknown "inGroups" function.
Last updated
But I get error
Unknown "inGroup" function. Unknown "inGroups" function.
Try to install Passage Permission and Roles System plugin. https://octobercms.com/plugin/kurtjensen-passage
Last updated
1-6 of 6