<commit_before><?php
namespace App;

use App\Entity;

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_USERS          = 'administer user accounts';
    public const GLOBAL_PERMISSIONS    = 'administer permissions';
    public const GLOBAL_STATIONS       = 'administer stations';
    public const GLOBAL_CUSTOM_FIELDS  = 'administer custom fields';

    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';

    /** @var Entity\Repository\RolePermissionRepository */
    protected $permission_repo;

    /** @var array|null An array of actions enabled by each role. */
    protected $_actions = null;

    public function __construct(Entity\Repository\RolePermissionRepository $permission_repo)
    {
        $this->permission_repo = $permission_repo;
        $this->reload();
    }

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

    /**
     * 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 $station_id
     * @return bool
     */
    public function userAllowed(?Entity\User $user = null, $action, $station_id = null): bool
    {
        if (!($user instanceof Entity\User)) {
            return false;
        }

        if ($station_id instanceof Entity\Station) {
            $station_id = $station_id->getId();
        }

        $num_roles = $user->getRoles()->count();
        if ($num_roles > 0) {
            if ($num_roles === 1) {
                $role = $user->getRoles()->first();
                return $this->roleAllowed($role->getId(), $action, $station_id);
            }

            $roles = [];
            if ($user->getRoles()->count() > 0) {
                foreach ($user->getRoles() as $role) {
                    $roles[] = $role->getId();
                }
            }

            return $this->roleAllowed($roles, $action, $station_id);
        }

        return false;
    }

    /**
     * Check if a role (or array of roles) is allowed to perform an action (or array of actions).
     *
     * @param int|array $role_id
     * @param string|array $action
     * @param int|Station|null $station_id
     * @return bool
     */
    public function roleAllowed($role_id, $action, $station_id = null): bool
    {
        if ($station_id instanceof Entity\Station) {
            $station_id = $station_id->getId();
        }<commit_msg>Fix type hinting typo in ACL.<commit_after><?php
namespace App;

use App\Entity;

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_USERS          = 'administer user accounts';
    public const GLOBAL_PERMISSIONS    = 'administer permissions';
    public const GLOBAL_STATIONS       = 'administer stations';
    public const GLOBAL_CUSTOM_FIELDS  = 'administer custom fields';

    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';

    /** @var Entity\Repository\RolePermissionRepository */
    protected $permission_repo;

    /** @var array|null An array of actions enabled by each role. */
    protected $_actions = null;

    public function __construct(Entity\Repository\RolePermissionRepository $permission_repo)
    {
        $this->permission_repo = $permission_repo;
        $this->reload();
    }

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

    /**
     * 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 $station_id
     * @return bool
     */
    public function userAllowed(?Entity\User $user = null, $action, $station_id = null): bool
    {
        if (!($user instanceof Entity\User)) {
            return false;
        }

        if ($station_id instanceof Entity\Station) {
            $station_id = $station_id->getId();
        }

        $num_roles = $user->getRoles()->count();
        if ($num_roles > 0) {
            if ($num_roles === 1) {
                $role = $user->getRoles()->first();
                return $this->roleAllowed($role->getId(), $action, $station_id);
            }

            $roles = [];
            if ($user->getRoles()->count() > 0) {
                foreach ($user->getRoles() as $role) {
                    $roles[] = $role->getId();
                }
            }

            return $this->roleAllowed($roles, $action, $station_id);
        }

        return false;
    }

    /**
     * Check if a role (or array of roles) is allowed to perform an action (or array of actions).
     *
     * @param int|array $role_id
     * @param string|array $action
     * @param int|Entity\Station|null $station_id
     * @return bool
     */
    public function roleAllowed($role_id, $action, $station_id = null): bool
    {
        if ($station_id instanceof Entity\Station) {
            $station_id = $station_id->getId();
        }
{'fraction_non_alphanumeric': 0.10350949064591014, 'fraction_numerical': 0.0008193363375665711, 'mean_word_length': 2.3170289855072466, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '4062109', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2022, 'n_words': 595}