Back to Access class

Method check

public static bool|null
check
(mixed $userId, mixed $action, mixed $assetKey = null, mixed $preload = true)
Method to check if a user is authorised to perform an action, optionally on an asset.
Parameters
  • int $userId Id of the user for which to check authorisation.
  • string $action The name of the action to authorise.
  • int|string $assetKey The asset key (asset id or asset name). null fallback to root asset.
  • bool $preload Indicates whether preloading should be used.
Returns
  • bool|null True if allowed, false for an explicit deny, null for an implicit deny.
Since
  • 1.7.0
Class: Access
Project: Joomla

Method check - Source code

/**
 * Method to check if a user is authorised to perform an action, optionally on an asset.
 *
 * @param   integer         $userId    Id of the user for which to check authorisation.
 * @param   string          $action    The name of the action to authorise.
 * @param   integer|string  $assetKey  The asset key (asset id or asset name). null fallback to root asset.
 * @param   boolean         $preload   Indicates whether preloading should be used.
 *
 * @return  boolean|null  True if allowed, false for an explicit deny, null for an implicit deny.
 *
 * @since   1.7.0
 */
public static function check($userId, $action, $assetKey = null, $preload = true)
{
    // Sanitise inputs.
    $userId = (int) $userId;
    $action = strtolower(preg_replace('#[\\s\\-]+#', '.', trim($action)));
    if (!isset(self::$identities[$userId])) {
        // Get all groups against which the user is mapped.
        self::$identities[$userId] = self::getGroupsByUser($userId);
        array_unshift(self::$identities[$userId], $userId * -1);
    }
    return self::getAssetRules($assetKey, true, true, $preload)->allow($action, self::$identities[$userId]);
}