08 November 2010

Check the group id of a user in Joomla

If you are doing your own custom Joomla component, somtimes it is very helpful to determine the user group of a user in order to perform necessary actions (for example, only admins can delete a record).

For doing this, we must get the group id. They are as follows:

Public : 0
Registered : 1
Special : 2

Users that have access to the backend fall under the Special group.

The following code will check the group id of the current user and display a message if user is not of 'Special' type.
$user = JFactory::getUser();
$userGID = $user->get('gid');
if ($userGID != 2)
{
   echo "You do not have the necessary permissions to perform this action.";
}