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:
<?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

GeeEM
 
linuxoid replied on at Permalink Reply
linuxoid
Try this:
use Concrete\Core\User\Group\Group;
$user = $this->app->make(User::class);
$group = Group::getByName($group_name); // or by id: $group = Group::getByID($group_id);
if ($user->isLoggedIn() && $user->inGroup($group)) {
    ...
}
GeeEM replied on at Permalink Reply
GeeEM
Thanks for the reply.

This returns:

An unexpected error occurred.
Call to a member function make() on null
linuxoid replied on at Permalink Best Answer Reply
linuxoid
Either put this in the controller:
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;
GeeEM replied on at Permalink Reply
GeeEM
This seems to have worked. Now, the "complete" code is...

<?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.