<commit_before><?php

namespace App;

use App\Entity;

use function in_array;
use function is_array;

class Acl
{
    public const GLOBAL_ALL = 'administer all';
    public const GLOBAL_VIEW = 'view administration';
    public const GLOBAL_LOGS = 'view system logs';
    public const GLOBAL_SETTINGS = 'administer settings';
    public const GLOBAL_API_KEYS = 'administer api keys';
    public const GLOBAL_STATIONS = 'administer stations';
    public const GLOBAL_CUSTOM_FIELDS = 'administer custom fields';
    public const GLOBAL_BACKUPS = 'administer backups';
    public const GLOBAL_STORAGE_LOCATIONS = 'administer storage locations';

    public const STATION_ALL = 'administer all';
    public const STATION_VIEW = 'view station management';
    public const STATION_REPORTS = 'view station reports';
    public const STATION_LOGS = 'view station logs';
    public const STATION_PROFILE = 'manage station profile';
    public const STATION_BROADCASTING = 'manage station broadcasting';
    public const STATION_STREAMERS = 'manage station streamers';
    public const STATION_MOUNTS = 'manage station mounts';
    public const STATION_REMOTES = 'manage station remotes';
    public const STATION_MEDIA = 'manage station media';
    public const STATION_AUTOMATION = 'manage station automation';
    public const STATION_WEB_HOOKS = 'manage station web hooks';

    protected Entity\Repository\RolePermissionRepository $permissionRepo;

    protected EventDispatcher $dispatcher;

    protected array $permissions;

    protected ?array $actions;

    protected ?Entity\User $user;

    public function __construct(
        Entity\Repository\RolePermissionRepository $rolePermissionRepository,
        EventDispatcher $dispatcher,
        ?Entity\User $user = null
    ) {
        $this->permissionRepo = $rolePermissionRepository;
        $this->dispatcher = $dispatcher;
        $this->setUser($user);

        $this->permissions = $this->listPermissions();
        $this->reload();
    }

    public function setUser(?Entity\User $user): void
    {
        $this->user = $user;
    }

    /**
     * Force a reload of the internal ACL cache (used in the event of a user status change).
     */
    public function reload(): void
    {
        $this->actions = $this->permissionRepo->getActionsForAllRoles();
    }

    /**
     * @param string $permission_name
     * @param bool $is_global
     */
    public function isValidPermission($permission_name, $is_global): bool
    {
        return $is_global
            ? isset($this->permissions['global'][$permission_name])
            : isset($this->permissions['station'][$permission_name]);
    }

    /**
     * @return mixed[]
     */
    public function listPermissions(): array
    {
        if (isset($this->permissions)) {
            return $this->permissions;
        }

        $permissions = [
            'global' => [
                self::GLOBAL_ALL => __('All Permissions'),
                self::GLOBAL_VIEW => __('View Administration Page'),
                self::GLOBAL_LOGS => __('View System Logs'),
                self::GLOBAL_SETTINGS => __('Administer Settings'),
                self::GLOBAL_API_KEYS => __('Administer API Keys'),
                self::GLOBAL_STATIONS => __('Administer Stations'),
                self::GLOBAL_CUSTOM_FIELDS => __('Administer Custom Fields'),
                self::GLOBAL_BACKUPS => __('Administer Backups'),
                self::GLOBAL_STORAGE_LOCATIONS => __('Administer Storage Locations'),
            ],
            'station' => [
                self::STATION_ALL => __('All Permissions'),
                self::STATION_VIEW => __('View Station Page'),
                self::STATION_REPORTS => __('View Station Reports'),
                self::STATION_LOGS => __('View Station Logs'),
                self::STATION_PROFILE => __('Manage Station Profile'),
                self::STATION_BROADCASTING => __('Manage Station Broadcasting'),
                self::STATION_STREAMERS => __('Manage Station Streamers'),
                self::STATION_MOUNTS => __('Manage Station Mount Points'),
                self::STATION_REMOTES => __('Manage Station Remote Relays'),
                self::STATION_MEDIA => __('Manage Station Media'),
                self::STATION_AUTOMATION => __('Manage Station Automation'),
                self::STATION_WEB_HOOKS => __('Manage Station Web Hooks'),
            ],
        ];

        $buildPermissionsEvent = new Event\BuildPermissions($permissions);
        $this->dispatcher->dispatch($buildPermissionsEvent);

        $permissions = $buildPermissionsEvent->getPermissions();

        return $permissions;
    }

    /**
     * Check if the current user associated with the request has the specified permission.
     *
     * @param string|array $action
     * @param int|Entity\Station|null $stationId
     */
    public function isAllowed($action, $stationId = null): bool
    {
        if (!isset($this->user)) {
            throw new \RuntimeException('Cannot check permission with no user set.');
        }

        return $this->userAllowed($this->user, $action, $stationId);
    }

    /**
     * Check if a specified User entity is allowed to perform an action (or array of actions).
     *
     * @param Entity\User|null $user
     * @param string|array $action
     * @param int|Entity\Station|null $stationId
     */
    public function userAllowed(?Entity\User $user = null, $action, $stationId = null): bool
    {
        if (!($user instanceof Entity\User)) {
            return false;
        }
<commit_msg>Fix Acl error on actions without a user like internal API calls (#3661)<commit_after><?php

namespace App;

use App\Entity;

use function in_array;
use function is_array;

class Acl
{
    public const GLOBAL_ALL = 'administer all';
    public const GLOBAL_VIEW = 'view administration';
    public const GLOBAL_LOGS = 'view system logs';
    public const GLOBAL_SETTINGS = 'administer settings';
    public const GLOBAL_API_KEYS = 'administer api keys';
    public const GLOBAL_STATIONS = 'administer stations';
    public const GLOBAL_CUSTOM_FIELDS = 'administer custom fields';
    public const GLOBAL_BACKUPS = 'administer backups';
    public const GLOBAL_STORAGE_LOCATIONS = 'administer storage locations';

    public const STATION_ALL = 'administer all';
    public const STATION_VIEW = 'view station management';
    public const STATION_REPORTS = 'view station reports';
    public const STATION_LOGS = 'view station logs';
    public const STATION_PROFILE = 'manage station profile';
    public const STATION_BROADCASTING = 'manage station broadcasting';
    public const STATION_STREAMERS = 'manage station streamers';
    public const STATION_MOUNTS = 'manage station mounts';
    public const STATION_REMOTES = 'manage station remotes';
    public const STATION_MEDIA = 'manage station media';
    public const STATION_AUTOMATION = 'manage station automation';
    public const STATION_WEB_HOOKS = 'manage station web hooks';

    protected Entity\Repository\RolePermissionRepository $permissionRepo;

    protected EventDispatcher $dispatcher;

    protected array $permissions;

    protected ?array $actions;

    protected ?Entity\User $user;

    public function __construct(
        Entity\Repository\RolePermissionRepository $rolePermissionRepository,
        EventDispatcher $dispatcher,
        ?Entity\User $user = null
    ) {
        $this->permissionRepo = $rolePermissionRepository;
        $this->dispatcher = $dispatcher;
        $this->setUser($user);

        $this->permissions = $this->listPermissions();
        $this->reload();
    }

    public function setUser(?Entity\User $user): void
    {
        $this->user = $user;
    }

    /**
     * Force a reload of the internal ACL cache (used in the event of a user status change).
     */
    public function reload(): void
    {
        $this->actions = $this->permissionRepo->getActionsForAllRoles();
    }

    /**
     * @param string $permission_name
     * @param bool $is_global
     */
    public function isValidPermission($permission_name, $is_global): bool
    {
        return $is_global
            ? isset($this->permissions['global'][$permission_name])
            : isset($this->permissions['station'][$permission_name]);
    }

    /**
     * @return mixed[]
     */
    public function listPermissions(): array
    {
        if (isset($this->permissions)) {
            return $this->permissions;
        }

        $permissions = [
            'global' => [
                self::GLOBAL_ALL => __('All Permissions'),
                self::GLOBAL_VIEW => __('View Administration Page'),
                self::GLOBAL_LOGS => __('View System Logs'),
                self::GLOBAL_SETTINGS => __('Administer Settings'),
                self::GLOBAL_API_KEYS => __('Administer API Keys'),
                self::GLOBAL_STATIONS => __('Administer Stations'),
                self::GLOBAL_CUSTOM_FIELDS => __('Administer Custom Fields'),
                self::GLOBAL_BACKUPS => __('Administer Backups'),
                self::GLOBAL_STORAGE_LOCATIONS => __('Administer Storage Locations'),
            ],
            'station' => [
                self::STATION_ALL => __('All Permissions'),
                self::STATION_VIEW => __('View Station Page'),
                self::STATION_REPORTS => __('View Station Reports'),
                self::STATION_LOGS => __('View Station Logs'),
                self::STATION_PROFILE => __('Manage Station Profile'),
                self::STATION_BROADCASTING => __('Manage Station Broadcasting'),
                self::STATION_STREAMERS => __('Manage Station Streamers'),
                self::STATION_MOUNTS => __('Manage Station Mount Points'),
                self::STATION_REMOTES => __('Manage Station Remote Relays'),
                self::STATION_MEDIA => __('Manage Station Media'),
                self::STATION_AUTOMATION => __('Manage Station Automation'),
                self::STATION_WEB_HOOKS => __('Manage Station Web Hooks'),
            ],
        ];

        $buildPermissionsEvent = new Event\BuildPermissions($permissions);
        $this->dispatcher->dispatch($buildPermissionsEvent);

        $permissions = $buildPermissionsEvent->getPermissions();

        return $permissions;
    }

    /**
     * Check if the current user associated with the request has the specified permission.
     *
     * @param string|array $action
     * @param int|Entity\Station|null $stationId
     */
    public function isAllowed($action, $stationId = null): bool
    {
        return $this->userAllowed($this->user, $action, $stationId);
    }

    /**
     * Check if a specified User entity is allowed to perform an action (or array of actions).
     *
     * @param Entity\User|null $user
     * @param string|array $action
     * @param int|Entity\Station|null $stationId
     */
    public function userAllowed(?Entity\User $user = null, $action, $stationId = null): bool
    {
        if (!($user instanceof Entity\User)) {
            return false;
        }

{'fraction_non_alphanumeric': 0.10904830569574621, 'fraction_numerical': 0.0003604902667627974, 'mean_word_length': 2.72632639355272, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6381838', 'n_tokens_mistral': 3039, 'n_tokens_neox': 2972, 'n_words': 791}