Created
June 10, 2011 23:13
-
-
Save AmyStephen/1019994 to your computer and use it in GitHub Desktop.
getAuthorisedCategories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!$checkedOut && ($canDo->get('core.edit') || count($user->getAuthorisedCategories('com_banners', 'core.create')) > 0)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Method to return a list of user groups mapped to a user. The returned list can optionally hold | |
* only the groups explicitly mapped to the user or all groups both explicitly mapped and inherited | |
* by the user. | |
* | |
* @param integer $userId Id of the user for which to get the list of groups. | |
* @param boolean $recursive True to include inherited user groups. | |
* | |
* @return array List of user group ids to which the user is mapped. | |
* | |
* @since 11.1 | |
*/ | |
public static function getGroupsByUser($userId, $recursive = true) | |
{ | |
static $results = array(); | |
// Creates a simple unique string for each parameter combination: | |
$storeId = $userId.':'.(int) $recursive; | |
if (!isset($results[$storeId])) { | |
// Guest user | |
if (empty($userId)) { | |
$result = array(JComponentHelper::getParams('com_users')->get('guest_usergroup', 1)); | |
} | |
// Registered user | |
else { | |
$db = JFactory::getDbo(); | |
// Build the database query to get the rules for the asset. | |
$query = $db->getQuery(true); | |
$query->select($recursive ? 'b.id' : 'a.id'); | |
$query->from('#__user_usergroup_map AS map'); | |
$query->where('map.user_id = '.(int) $userId); | |
$query->leftJoin('#__usergroups AS a ON a.id = map.group_id'); | |
// If we want the rules cascading up to the global asset node we need a self-join. | |
if ($recursive) { | |
$query->leftJoin('#__usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt'); | |
} | |
// Execute the query and load the rules from the result. | |
$db->setQuery($query); | |
$result = $db->loadColumn(); | |
// Clean up any NULL or duplicate values, just in case | |
JArrayHelper::toInteger($result); | |
if (empty($result)) { | |
$result = array('1'); | |
} | |
else { | |
$result = array_unique($result); | |
} | |
} | |
$results[$storeId] = $result; | |
} | |
return $results[$storeId]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment