Getting groups that the user is in

Permalink 2 users found helpful
I have searched the forum, even with Google...

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

Maynar
 
Maynar replied on at Permalink Reply
Maynar
Aha.. it only gets the group you are in when you are logged in. so you have to logout and login again to get the groups you are in (if it had changed).

Alright, never mind me then...
bcarone replied on at Permalink Reply
bcarone
Keep asking the questions and if you find the fix, keep doing what you did and post a comment on how you got the answer you needed :)

Thanks Bill
Maynar replied on at Permalink Reply
Maynar
Yeah, I can understand why you want me to do this. Kind of making a documentation for problems you might run into. Might help people like me when you want to build something.

So, Sure if I have another question I will...
bcarone replied on at Permalink Reply
bcarone
Your are spot-on Maynar. This will help as we create documents for our users (including YOU!!)

Thanks
hockeyshooter replied on at Permalink Reply
hockeyshooter
Whilst writing a custom block, I've managed to get a list of all the groups the logged-in user is a member of:
$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.
Maynar replied on at Permalink Reply
Maynar
I did it like this:
$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...
myregistration replied on at Permalink Reply
When I am logged in as Administrator and print the array returned for my user groups it displays the following ...

Array ( [2] => Registered Users [1] => Guest )

Here's the code I used:
$u = new User(); 
$groups = $u->getUserGroups(); 
print_r($u->uGroups);
print_r($groups);

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!
Maynar replied on at Permalink Reply
Maynar
Using the account currently logged in and you added it to a group that session?

Logout and then login again.
myregistration replied on at Permalink Reply
I'm logged in as the site admin and displaying my own user values for testing functionality of a conditional, I would assume that works since I am a user, but it returned unexpected results.
Maynar replied on at Permalink Reply
Maynar
Yes... but~!

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.
myregistration replied on at Permalink Reply
Do you know how to cast the user and then get it's group?

For example,
$ui = UserInfo::getByID(3); 
echo $ui->getUserName();
print_r($ui->getUserGroups());

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?
andrew replied on at Permalink Reply
andrew
In an effort to keep overhead low (which now seems kind of futile, but whatever) we tried to separate core user-related functionality and auxiliary userdata-related functionality. The core stuff, which includes checking whether a user is logged in, and what groups he/she is in, is in the User object. The auxiliary userdata stuff, including the magic method that we were just talking about (which translates user attributes into methods) is in the UserInfo object.

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.
KaiTrallafitti replied on at Permalink Reply
KaiTrallafitti
Really a weird thing (well, that discussion was long time ago, but that answer may be helpful to someone).

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