Set roles for a user

@param array $roles (optional) - the users roles
@return boolean
429894
public function setRoles(array $roles = array())
    {
        if ($this->instance instanceof \Phavour\Session\Storage) {
            if ($this->instance->isLocked()) {
                $this->instance->unlock();
            }
            $this->instance->set('roles', $roles);
            $this->instance->lock();
            return true;
        }

        return false;
    }
1315439
public function seed(array $roles)
    {
        Role::query()->insert(
            $this->prepareRoles($roles)
        );

        $this->syncAdminRole();
    }
653066
public function personalWorkspaceAllowed($roles)
    {
        $roles[] = $this->roleManager->getRoleByName('ROLE_USER');

        foreach ($roles as $role) {
            if ($role->isPersonalWorkspaceCreationEnabled()) {
                return true;
            }
        }

        return false;
    }
1260722
protected function flattenRoles(array $roles)
    {
        $roleNames = [];
        $iterator  = $this->traversalStrategy->getRolesIterator($roles);

        foreach ($iterator as $role) {
            $roleNames[] = $role->getName();
        }

        return array_unique($roleNames);
    }
699582
public function findUsersByRolesIncludingGroups(
        array $roles
    ) {
        //very slow otherwise. If we want to do it properly, the OR clause won't do it.
        //we must use UNION wich is not supported by Doctrine
        $dql = '
            SELECT u, r1, ws
            From Claroline\\CoreBundle\\Entity\\User u
            LEFT JOIN u.roles r1
            LEFT JOIN u.personalWorkspace ws
            WHERE r1 in (:roles)
            AND u.isRemoved = false
            ORDER BY u.lastName, u.firstName ASC
        ';

        $query = $this->_em->createQuery($dql);
        $query->setParameter('roles', $roles);

        $resA = $query->getResult();
        $resA = $resA ? $resA : [];

        $dql = '
            SELECT u, g, r2, ws
            From Claroline\\CoreBundle\\Entity\\User u
            LEFT JOIN u.personalWorkspace ws
            LEFT JOIN u.groups g
            LEFT JOIN g.roles r2
            WHERE r2 in (:roles)
            AND u.isRemoved = false
            ORDER BY u.lastName, u.firstName ASC
        ';

        $query = $this->_em->createQuery($dql);
        $query->setParameter('roles', $roles);

        $resB = $query->getResult();
        $resB = $resB ? $resB : [];

        return array_unique(array_merge($resA, $resB));
    }
459017
public function hasRole(...$roles)
    {
        $roles = array_flatten($roles);

        foreach ($roles as $role) {
            $name = $role instanceof Role ? $role->name : $role;

            if ($this->roles->contains('name', $name)) {
                return true;
            }
        }

        return false;
    }
168049
public function whereRoleIn(array $roles)
    {
        if (0 < $count = count($roles)) {
            $this->leftJoinRights = true;
            $eol = PHP_EOL;
            $clause = "{$eol}({$eol}";

            foreach ($roles as $i => $role) {
                $role = $roles[$i] instanceof RoleInterface ? $roles[$i]->getRole() : $roles[$i];
                $clause .= $i > 0 ? '    OR ' : '    ';
                $clause .= "rightRole.name = :role_{$i}{$eol}";
                $this->parameters[":role_{$i}"] = $role;
            }

            $this->addWhereClause($clause . ')');
        }

        return $this;
    }
147317
public function addRole(Role $role)
    {
        if (!in_array($role, $this->roles)) {
            $this->roles[] = $role;
            return true;
        }
        return false;
    }
1243768
public function setRoles($roles)
    {
        $allowedValues = $this->getRolesAllowableValues();
        if (!is_null($roles) && array_diff($roles, $allowedValues)) {
            throw new \InvalidArgumentException(
                sprintf(
                    "Invalid value for 'roles', must be one of '%s'",
                    implode("', '", $allowedValues)
                )
            );
        }
        $this->container['roles'] = $roles;

        return $this;
    }
591874
public function beforeRole($roles)
    {
        $roles = is_array($roles) ? $roles : func_get_args();

        $this->beforeFilter(function () use ($roles) {
            Trusty::filterByRole($roles);
        });
    }
481385
protected function isOne(array $roles)
    {
        foreach ($roles as $role) {
            if ($this->hasRole($role)) {
                return true;
            }
        }

        return false;
    }
961594
public function assignRole(...$roles)
    {
        $roles = collect($roles)
            ->flatten()
            ->map(function ($role) {
                if (empty($role)) {
                    return false;
                }

                return $this->getStoredRole($role);
            })
            ->filter(function ($role) {
                return $role instanceof Role;
            })
            ->each(function ($role) {
                $this->ensureModelSharesGuard($role);
            })
            ->map->id
            ->all();

        $model = $this->getModel();

        if ($model->exists) {
            $this->roles()->sync($roles, false);
            $model->load('roles');
        } else {
            $class = \get_class($model);

            $class::saved(
                function ($object) use ($roles, $model) {
                    static $modelLastFiredOn;
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
                        return;
                    }
                    $object->roles()->sync($roles, false);
                    $object->load('roles');
                    $modelLastFiredOn = $object;
                });
        }

        $this->forgetCachedPermissions();

        return $this;
    }
654806
protected function _addRoles(array $roles) {
        foreach ($roles as $roleName => $properties) {
            // If the properties aren't set as an array, then we will consider
            // the value as the role ID.
            if (!is_array($properties)) {
                $properties = array('id' => $properties);
            }

            $id = $properties['id'];

            if (is_null($id) || empty($id)) {
                throw new Zend_Application_Resource_Exception(
                    sprintf(
                        $this->_missingPropertyMessage, 'ID', 'role', $roleName
                    )
                );
            }
            $this->_addRoleById($roles, $id);
        }
    }
529590
public function hasRole($role) {
        $roles = (array) $this->Session->read('Acl.roles');

        if (is_numeric($role)) {
            return isset($roles[$role]);
        }

        return in_array($role, $roles);
    }
1009491
protected function syncRoles(array $roles)
    {
        /** @var \Illuminate\Database\Eloquent\Collection $permissions */
        $permissions = Permission::all();

        foreach ($roles as $roleSlug => $permissionSlug) {
            /** @var  \Arcanesoft\Auth\Models\Role  $role */
            if ($role = Role::query()->where('slug', $roleSlug)->first()) {
                $filtered = $permissions->filter(function (Permission $permission) use ($permissionSlug) {
                    return Str::startsWith($permission->slug, $permissionSlug);
                });

                $role->permissions()->sync(
                    $filtered->pluck('id')->toArray()
                );
            }
        }
    }
377223
public static function hasRole($roles)
    {
        $user = wp_get_current_user();

        if (is_string($roles)) {
            $roles = array($roles);
        }

        if (!array_intersect($roles, $user->roles)) {
            return false;
        }

        return true;
    }
710393
public function filterByRole($roles)
    {
        $roles = is_array($roles) ? $roles : func_get_args();

        foreach ($roles as $role) {
            if (!$this->auth->user()->is($role)) {
                throw new PermissionDeniedException("You aren't a \"{$role}\".");
            }
        }
    }