Displaying content in single page or block based on group/access level

Permalink
I’m currently working on a single_page but could be a block as well and was wondering if there is a way to obtain the logged in users access or group to use in a conditional statement? For example, I have a page to add/mod/delete data but I want the delete option to only be display if the logged in user has permission via a group or defined permission.

 
ScottC replied on at Permalink Reply
ScottC
yeah the flow of the code will be like:

grab the current user object
$u = new User(); //construct fills a user object

//grab the user's groups

if($u->inGroup('yourGroup')){
//display whatever
}

that'll get you started, just read every model class definition and methods and you'll get a good idea of the class method definitions and be able to "wing" more code using common concrete5 terminology. Most classes have getByID and getByHandle/getByName available.
okhayat replied on at Permalink Reply
okhayat
The API reference is always a valuable resource. Look at it here (Permissions class):
http://docs.concrete5addons.com/...
Thanks David[MIRV]!
Ricalsin replied on at Permalink Reply
Ricalsin
Old post (I know, but) same problem.

I was writing this exact same code and having trouble. The code I have is:
$u= new User();
if($u->inGroup("Vics")) {   echo "Hey, you're in Vics!";
   } else { echo "Sorry, no Vics group found for you...<br />"; var_dump($u); echo "<br />";
   }
The var_dump outputs:
Sorry, no Vics group found for you...
object(User)#214 (6) { ["uID"]=> string(1) "1" ["uName"]=> string(5) "admin" ["uGroups"]=> array(3) { [2]=> string(16) "Registered Users" [5]=> string(12) "Vics" [1]=> string(5) "Guest" } ["superUser"]=> bool(true) ["uTimezone"]=> string(19) "America/Los_Angeles" ["error"]=> string(0) "" }

I dumped the caches on both ff and IE as well as C5. In this code the admin was logged on, who is both a superuser and assigned to the Vics Group. Same problem with other non-super users but members of Vics Group - no recognition of the group. Why the three different groups? I thought maybe the cache/cookies, but problem persists. I've double and single quoted around the name just for fun. I can't find the inGroup function to see if the array of users is causing the problem. I have created a new user with no history and the var_dump comes out just the same.

Any thoughts?
tdi00 replied on at Permalink Reply
You are missing the Group::getByName call. The correct code would be the following for your example.

$u = new User();
$g = Group::getByName('Vics');
if($u->inGroup($g)) {
    echo "Hey, you're in Vics!";
} else { 
    echo "Sorry, no Vics group found for you...<br />"; var_dump($u); echo "<br />";
}