Grouped assign actions to have access.

@param  string  $role
@param  array   $actions
@param  bool    $allow

@return bool

@throws \InvalidArgumentException
968839
protected function groupedAssignAction($role, array $actions, $allow = true)
    {
        foreach ($actions as $action) {
            if (!$this->actions->has($action)) {
                throw new InvalidArgumentException("Action {$action} does not exist.");
            }

            $this->assign($role, $action, $allow);
        }

        return true;
    }
849061
public function setRole($role)
    {
        $allowedValues = $this->getRoleAllowableValues();
        if (!in_array($role, $allowedValues)) {
            throw new \InvalidArgumentException(
                sprintf(
                    "Invalid value for 'role', must be one of '%s'",
                    implode("', '", $allowedValues)
                )
            );
        }
        $this->container['role'] = $role;

        return $this;
    }
269023
protected function assign($role = null, $action = null, $allow = true)
    {
        $role   = $this->roles->findKey($role);
        $action = $this->actions->findKey($action);

        if (!is_null($role) && !is_null($action)) {
            $key             = $role . ':' . $action;
            $this->acl[$key] = $allow;
        }
    }
835102
protected function countMatchingRoles($authority, $roles)
    {
        $lookups = $this->getRolesLookup($authority);

        return count(array_filter($roles, function ($role) use ($lookups) {
            switch (true) {
                case is_string($role):
                    return $lookups['names']->has($role);
                case is_numeric($role):
                    return $lookups['ids']->has($role);
                case $role instanceof Model:
                    return $lookups['ids']->has($role->getKey());
            }

            throw new InvalidArgumentException('Invalid model identifier');
        }));
    }
781672
public function authorize($ability, $arguments = [])
    {
        $result = $this->allows($ability, $arguments);
        return $result ? $this->allow() : $this->deny();
    }
434190
public function user_roles_and_actions($userid, $roles, $assignableroles, $canassign, $pageurl) {

        // Get list of roles.
        $rolesoutput = '';
        foreach ($roles as $roleid=>$role) {
            if ($canassign and (is_siteadmin() or isset($assignableroles[$roleid])) and !$role['unchangeable']) {
                $strunassign = get_string('unassignarole', 'role', $role['text']);
                $icon = $this->output->pix_icon('t/delete', $strunassign);
                $url = new moodle_url($pageurl, array('action'=>'unassign', 'roleid'=>$roleid, 'user'=>$userid));
                $rolesoutput .= html_writer::tag('div', $role['text'] . html_writer::link($url, $icon, array('class'=>'unassignrolelink', 'rel'=>$roleid, 'title'=>$strunassign)), array('class'=>'role role_'.$roleid));
            } else {
                $rolesoutput .= html_writer::tag('div', $role['text'], array('class'=>'role unchangeable', 'rel'=>$roleid));
            }
        }
        $output = '';
        if (!empty($assignableroles) && $canassign) {
            $roleids = array_keys($roles);
            $hasallroles = true;
            foreach (array_keys($assignableroles) as $key) {
                if (!in_array($key, $roleids)) {
                    $hasallroles = false;
                    break;
                }
            }
            if (!$hasallroles) {
                $url = new moodle_url($pageurl, array('action' => 'assign', 'user' => $userid));
                $roleicon = $this->output->pix_icon('i/assignroles', get_string('assignroles', 'role'));
                $link = html_writer::link($url, $roleicon, array('class' => 'assignrolelink'));
                $output = html_writer::tag('div', $link, array('class'=>'addrole'));
            }
        }
        $output .= html_writer::tag('div', $rolesoutput, array('class'=>'roles'));
        return $output;
    }
552678
public function addRole(Role $role): bool
    {
        return (new actions\AddRole($this->user, $role))->execute();
    }
443667
public static function forAuthority(Model $authority, $allowed = true)
    {
        return Models::ability()->where(function ($query) use ($authority, $allowed) {
            $query->whereExists(static::getRoleConstraint($authority, $allowed));
            $query->orWhereExists(static::getAuthorityConstraint($authority, $allowed));
            $query->orWhereExists(static::getEveryoneConstraint($allowed));
        });
    }
422444
public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager
    {
        if (!$this->hasRole($role)) {
            throw new RoleException("Undefined role '{$role}'");
        }

        if (!$this->rules->has($rule)) {
            throw new PermissionException("Undefined rule '{$rule}'");
        }

        $this->permissions[$role][$permission] = $rule;

        return $this;
    }
454321
protected static function getRoleConstraint(Model $authority, $allowed)
    {
        return function ($query) use ($authority, $allowed) {
            $permissions = Models::table('permissions');
            $abilities   = Models::table('abilities');
            $roles       = Models::table('roles');
            $prefix      = Models::prefix();

            $query->from($roles)
                  ->join($permissions, $roles.'.id', '=', $permissions.'.entity_id')
                  ->whereRaw("{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id")
                  ->where($permissions.".forbidden", ! $allowed)
                  ->where($permissions.".entity_type", Models::role()->getMorphClass());

            Models::scope()->applyToModelQuery($query, $roles);
            Models::scope()->applyToRelationQuery($query, $permissions);

            $query->where(function ($query) use ($roles, $authority, $allowed) {
                $query->whereExists(static::getAuthorityRoleConstraint($authority));

                if ($allowed) {
                    static::addRoleInheritCondition($query, $authority, $roles);
                }
            });
        };
    }
955454
public function roleHasPermission($permission, $force = false)
    {
        if (! is_null($this->getUser())) {
            return $this->getUser()->roleHasPermission($permission, $force);
        }

        return false;
    }
651638
public function try_to_login_as_the_role( $role )
	{
		$role = $this->replace_variables( $role );

		$params = $this->get_params();

		if ( empty( $params['roles'][ $role ] ) ) {
			throw new \InvalidArgumentException( sprintf(
				"Role '%s' is not defined in the `behat.yml`", $role
			) );
		} else {
			$result = $this->login(
				$params['roles'][ $role ]['username'],
				$params['roles'][ $role ]['password']
			);
		}
	}
1037974
public function hasRolePermission(Role $role, Permission $permission)
    {
        // Iterate over each permission from the user and check if the permission is in the array.
        foreach ($this->getRolePermissions($role) as $rolePermission) {
            // If a matching permission was found, immediately break the sequence and return true.
            if ($rolePermission->matchesPermission($permission)) {
                return true;
            }
        }

        return false;
    }
943713
protected function authorization($type, $rolesPermissions, $team, $options)
    {
        list($team, $requireAll, $guard) = $this->assignRealValuesTo($team, $options);
        $method = $type == 'roles' ? 'hasRole' : 'hasPermission';

        if (!is_array($rolesPermissions)) {
            $rolesPermissions = explode(self::DELIMITER, $rolesPermissions);
        }

        return !Auth::guard($guard)->guest()
            && Auth::guard($guard)->user()->$method($rolesPermissions, $team, $requireAll);
    }
1180367
public function allow( $role, $controller_name, $action_name )
	{
        $route = $this->_getRoute( $controller_name, $action_name );

        $this->_allow_list[$role][] = $route;

	}
1252024
public function isGranted(string $permission, array $roles): bool
    {
        foreach ($this->flattenRoles($roles) as $role) {
            if ($role->hasPermission($permission)) {
                return true;
            }
        }

        return false;
    }
1082952
public function setGrantableRoles($grantableRoles)
    {
        $allowedValues = $this->getGrantableRolesAllowableValues();
        if (!is_null($grantableRoles) && array_diff($grantableRoles, $allowedValues)) {
            throw new \InvalidArgumentException(
                sprintf(
                    "Invalid value for 'grantableRoles', must be one of '%s'",
                    implode("', '", $allowedValues)
                )
            );
        }
        $this->container['grantableRoles'] = $grantableRoles;

        return $this;
    }