bwakad Posted May 9, 2014 Share Posted May 9, 2014 I have this code, and 2 things do not work: sort by roles, and display the role of each user. I did read in the API I can use $type = $child->get("roles"); echo $type; but that seems a bit over the top, or not? Furthermore, as I understand, any extra role added will also be using the guest role. Why is that??? if($user->hasRole("member")) $selects = $users->find("roles=company");if($user->hasRole("company")) $selects = $users->find("roles=member");if($user->hasRole("superuser")) $selects = $users->find("roles=member|company, sort=-roles");foreach($selects as $child) { ?> <row> <div class="medium-6 columns"><?php echo $child->name; ?></div> <div class="medium-6 columns"><?php // display role ?></div> </row><?php } ?> Link to comment Share on other sites More sharing options...
Mats Posted May 9, 2014 Share Posted May 9, 2014 Hi! I think you should use the role id instead of name. Link to comment Share on other sites More sharing options...
bwakad Posted May 9, 2014 Author Share Posted May 9, 2014 I do not follow what you say... in what part? Link to comment Share on other sites More sharing options...
Martijn Geerts Posted May 9, 2014 Share Posted May 9, 2014 how to display role of users? Don't you mean: how to display roles of a user ? $user->roles // returns PageArray Link to comment Share on other sites More sharing options...
bwakad Posted May 9, 2014 Author Share Posted May 9, 2014 So in the first foreach I need to cycle through the second one? edit - Maybe it's easier to just use $selects = pages->find("template=user"); in this case it's the superuser, which has right to see all... And I believe on the user template I can acces the field roles, am I right? edit - this did not work either Btw, does anyone know why the role guest is present in all other roles.? Link to comment Share on other sites More sharing options...
Raymond Geerts Posted May 9, 2014 Share Posted May 9, 2014 I'm not sure what you are trying to acomplish. Anyway the folowing code prints all the roles and name of the role for the current user foreach($user->roles as $role) { echo "Role: {$role} = {$role->name} <br>"; } Or if you need the role for a specific user $u = $users->get('admin'); foreach($u->roles as $role) { echo "Role: {$role} = {$role->name} <br>"; } All anonymous visitors, loggedin users, admins etc... by default have the guest role 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now