Checking user group
Permalink
I am making a link which changes based on group membership (since the page you can access will change). I have been trying to usehttps://documentation.concrete5.org/developers/users-groups/groups/o... but am having issues.
Currently, I am trying to do:
I end up getting:
Any ideas?
Edit:
I removed $user = $userInfo->getUserObject(); and now get
An unexpected error occurred.
Call to a member function inGroup() on null
Currently, I am trying to do:
<?php use Concrete\Core\User\UserInfo; use Concrete\Core\User\User; $u = new User(); $ui = UserInfo::getByID($u->getUserID()); $user = $userInfo->getUserObject(); if($user->inGroup(Group::getByName("Members"))) { echo "Member"; } ?>
I end up getting:
An unexpected error occurred. Call to a member function getUserObject() on null
Any ideas?
Edit:
I removed $user = $userInfo->getUserObject(); and now get
An unexpected error occurred.
Call to a member function inGroup() on null
Try this:
Thanks for the reply.
This returns:
An unexpected error occurred.
Call to a member function make() on null
This returns:
An unexpected error occurred.
Call to a member function make() on null
Either put this in the controller:
or try use this:
BTW have you declared the User?
public function on_start() { $this->set('app', $this->app); }
or try use this:
use Concrete\Core\Support\Facade\Application; $app = Application::getFacadeApplication(); ... $user = $app->make(User::class);
BTW have you declared the User?
use Concrete\Core\User\User as User;
This seems to have worked. Now, the "complete" code is...
Hope that helps anyone else who tries this.
<?php use Concrete\Core\User\Group\Group; use Concrete\Core\User\User; use Concrete\Core\Support\Facade\Application; $app = Application::getFacadeApplication(); $user = $app->make(User::class); $group_name = "Member"; $group = Group::getByName($group_name); if ($user->isLoggedIn() && $user->inGroup($group)) { echo "Member"; } ?>
Hope that helps anyone else who tries this.