Getting groups that the user is in
Permalink 2 users found helpfulI have searched in the dashboard -> User and groups code. But it doesn't make sense. I have spend way to much time on this already and you guys will probably know what I mean in a second. I hope...
So.. I just want to get an array of groups the logged in user is in. I mean I know I am supposed to be in the Loader::model('user')/Loader::model('userinfo')
But the only thing I get back is: Guests, Registered Users. Where is: Administratorion, Clients, etc.?
Code? Well I tried to use the code of the dashboard like I said above, and just randomly trying out functions in the classes.
HELP?
Thanks Bill
So, Sure if I have another question I will...
Thanks
$u = new User(); $uId = $u->uID; $ui = UserInfo::getByID($uId); $uArray=get_object_vars($u); $uGroups = $uArray['uGroups'];
$u comes back as an object - hence the use of get_object_vars
$uGroups will then contain an array of the group names and IDs.
$u = new user(); $groups = $u->getUserGroups(); //My if statement for find a group
This also returned what I wanted, maybe it isn't correct?
That I should use that getByID(); function?
I'm not using it right now, but maybe for in the future...
Array ( [2] => Registered Users [1] => Guest )
Here's the code I used:
I get the same array returned by both.
Why didn't it return 3 => Administrators?
If I use the following
print_r($u->superUser);
it returns 1, as expected
Thanks!
Logout and then login again.
I asked if you added yourself to a group you expected that would be returned.
If you did do that. Logout and login again. That is needed to get the correct results.
After logging in again everything will work as expected.
This happened to me once and I'm pretty sure this is the same thing.
For example,
doesn't seem to work as expected. It returns the correct username but not the user's groups. How can I get the user's groups? I am trying to test code and don't want to have to log in as a user in another browser every time I want to see test results. Thanks!
Also, if the account I'm logged into doesn't belong to a group yet, such as the Administrator, why would it return an array with results?
Here's how you get a user's groups.
<?php $u = User::getByUserID(3); $groups = $u->getUserGroups(); print_r($groups); ?>
Let me know if this doesn't help.
In concrete\core\models\user_list.php
for some functions the "$ui"-variable doens`t seem to be accepted.
Where as an
public function getAktiveUser($ui) {
return $ui->isActive();
}
works without any problems, it does not work with:
$g = Group::getByName('Premium');
return $ui->inGroup($g);
But andrew posted the solution, a function can in these cases made by:
public function getGroup($ui) {
$u = User::getByUserID($ui->getUserID());
$g = Group::getByName('Premium');
if ($u->inGroup($g))
return 1;
else return 0;
}
Best regards, Kai
Alright, never mind me then...