Back to User class

Method getAuthorisedCategories

public array
getAuthorisedCategories
(mixed $component, mixed $action)
Method to return a list of all categories that a user has permission for a given action
Parameters
  • string $component The component from which to retrieve the categories
  • string $action The name of the section within the component from which to retrieve the actions.
Returns
  • array List of categories that this group can do this action to (empty array if none). Categories must be published.
Since
  • 1.7.0
Class: User
Project: Joomla

Method getAuthorisedCategories - Source code

/**
 * Method to return a list of all categories that a user has permission for a given action
 *
 * @param   string  $component  The component from which to retrieve the categories
 * @param   string  $action     The name of the section within the component from which to retrieve the actions.
 *
 * @return  array  List of categories that this group can do this action to (empty array if none). Categories must be published.
 *
 * @since   1.7.0
 */
public function getAuthorisedCategories($component, $action)
{
    // Brute force method: get all published category rows for the component and check each one
    // @todo: Modify the way permissions are stored in the db to allow for faster implementation and better scaling
    $db = Factory::getDbo();
    $subQuery = $db->getQuery(true)->select($db->quoteName(['id', 'asset_id']))->from($db->quoteName('#__categories'))->where([$db->quoteName('extension') . ' = :component', $db->quoteName('published') . ' = 1']);
    $query = $db->getQuery(true)->select($db->quoteName(['c.id', 'a.name']))->from('(' . $subQuery . ') AS ' . $db->quoteName('c'))->join('INNER', $db->quoteName('#__assets', 'a'), $db->quoteName('c.asset_id') . ' = ' . $db->quoteName('a.id'))->bind(':component', $component);
    $db->setQuery($query);
    $allCategories = $db->loadObjectList('id');
    $allowedCategories = array();
    foreach ($allCategories as $category) {
        if ($this->authorise($action, $category->name)) {
            $allowedCategories[] = (int) $category->id;
        }
    }
    return $allowedCategories;
}