0

This code is to check if user belong to one group:

 //get logged in user
 $user = JFactory::getUser();

$group_id = 20; //This is the group we are checking. ->usergroup **Sales** Group

if(in_array($group_id, $user->getAuthorisedGroups()))
{ 
  //The user is in this group
}else{
    echo 'No output';
}

BUT I want to check if user belongs to 2 groups.

$group_id = 20;//This is the group we are checking. ->usergroup **Sales** Group
and
$group_id = 21;//This is the group we are checking. ->usergroup **Design** Group

How do I do that?

Thanks in advance!

  • Probably not the most efficient way, but you could do a nested loop where if it matches the first condition, you check the second. – YellowWebMonkey Aug 29 '16 at 21:19
3

I would go for code like...

# get logged in user
$user = JFactory::getUser();

# groups to match
$sales_group_id = 20;
$design_group_id = 21;

# see if the user is in both
if( in_array($sales_group_id, $user->groups ) AND in_array($design_group_id, $user->groups ) ){ 
   echo "the user is in both sales and design"
}else{
    echo "Use is not";
}

I'm not familiar with the getAuthorisedGroups function, but you can just get the groups as an array from the user object.

The guts of the code are about the same really, the solution is mainly in using and AND in the IF, to make sure you have both groups in the array.

There is some more documentation at...

  • 1
    Your solution worked perfectly! THANK YOU for your help ;) – EmDep Sep 2 '16 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.