Change the user's environment-level role.

@param Environment $environment
@param string $newRole The new role (see EnvironmentAccess::$roles).

@return Result
509865
public function changeEnvironmentRole(Environment $environment, $newRole)
    {
        $access = $environment->getUser($this->id);
        if ($access) {
            if ($access->role === $newRole) {
                throw new \InvalidArgumentException("There is nothing to change");
            }

            return $access->update(['role' => $newRole]);
        }

        return $environment->addUser($this->id, $newRole);
    }
892020
public function setPlatformRole($platformRole)
    {
        $roles = $this->getEntityRoles();

        foreach ($roles as $role) {
            if ($role->getType() != Role::WS_ROLE) {
                $removedRole = $role;
            }
        }

        if (isset($removedRole)) {
            $this->roles->removeElement($removedRole);
        }

        $this->roles->add($platformRole);
    }
1219179
private function getSpecifiedEnvironmentRoles(array $roles)
    {
        $environmentRoles = [];
        $project = $this->getSelectedProject();
        $environments = $this->api()->getEnvironments($project);
        foreach ($roles as $role) {
            if (strpos($role, ':') === false) {
                continue;
            }
            list($id, $role) = explode(':', $role, 2);
            $role = $this->validateEnvironmentRole($role);
            // Match environment IDs by wildcard.
            if (strpos($id, '%') !== false) {
                $pattern = '/^' . str_replace('%', '.*', preg_quote($id)) . '$/';
                $matched = preg_grep($pattern, array_keys($environments));
                if (empty($matched)) {
                    throw new InvalidArgumentException('No environment IDs match: ' . $id);
                }
                foreach ($matched as $environmentId) {
                    $environmentRoles[$environmentId] = $role;
                }
                continue;
            }
            if (!$this->api()->getEnvironment($id, $project)) {
                throw new InvalidArgumentException('Environment not found: ' . $id);
            }
            $environmentRoles[$id] = $role;
        }

        return $environmentRoles;
    }
1078568
public function assignRoles(...$roles): self
    {
        $roles = array_flatten($roles);
        $roles = $this->getRoles($roles);

        if (! $roles) {
            return $this;
        }

        $this->roles()->syncWithoutDetaching($roles);

        return $this;
    }
557541
public function setRole( Acl\Role\RoleInterface $role = null )
    {
        $this->role = $role;
        return $this;
    }
1235547
protected function _addRoles($roles)
    {
        foreach ($roles as $name => $parent) {
            if (!$this->hasRole($name)) {
                if (empty($parent)) {
                    $parent = array();
                } else {
                    $parent = explode(',', $parent);
                }

                $this->addRole(new Role($name), $parent);
            }
        }

        return $this;
    }
960163
public function changeRoles($roleRepository, $roleMapping = null)
    {
        $this->repositories['role'] = $roleRepository;
        $this->mappings['role'] = $roleMapping ?: $this->mappings['role'];

        return $this;
    }
505125
public function setRoles(array $roles)
    {
        $this->roles->clear();
        foreach ($roles as $role) {
            $this->addRole($role);
        }

        return $this;
    }
435342
public function assignRole($roleId = null)
    {
        $roles = $this->roles;

        if (! $roles->contains($roleId)) {
            return $this->roles()->attach($roleId);
        }

        return false;
    }
508300
private function setCasEnvironment($environment)
    {
        switch ($environment) {
            case 'development':
                $this->setCasHost('authtest.it.usf.edu');
                $this->setCasPort(443);
                $this->setCasContext('/');
                $this->configureCasClient();
                $this->setCasCaCertPath('/etc/USF-CA-chain.pem');
                phpCAS::handleLogoutRequests(false);
                break;

            case 'production':
                $this->setCasHost('webauth.usf.edu');
                $this->setCasPort(443);
                $this->setCasContext('/');
                $this->configureCasClient();
                $this->setCasCaCertPath('/etc/USF-CA-chain.pem');
                phpCAS::handleLogoutRequests(false);
                break;

            case 'impersonate':
                $this->setCasHost('authtest.it.usf.edu');
                $this->setCasPort(444);
                $this->setCasContext('/');
                $this->configureCasClient();
                $this->setCasCaCertPath('/etc/USF-CA-chain.pem');
                phpCAS::handleLogoutRequests(false);
                break;

            default:
                throw new Exception("Unknown auth environment: " . $environment, 500);
                break;
        }
    }
395801
public function createRole(Role $role)
    {
        try {
            return $this->innerGateway->createRole($role);
        } catch (DBALException $e) {
            throw new RuntimeException('Database error', 0, $e);
        } catch (PDOException $e) {
            throw new RuntimeException('Database error', 0, $e);
        }
    }
606330
private function updateUserRoles(User $user, array $currentRoles, array $newRoles, $addOnly)
    {
        $rolesToDissociate = [];

        foreach ($currentRoles as $role) {
            $index = array_search($role, $newRoles, true);

            if (false === $index) {
                $rolesToDissociate[] = $role;
            } else {
                unset($newRoles[$index]);
            }
        }
        if (!$addOnly) {
            foreach ($rolesToDissociate as $roleToDissociate) {
                $this->roleManager->dissociateRole($user, $roleToDissociate);
            }
        }
        $this->roleManager->associateRoles($user, $newRoles);
    }
406805
public function setRoles(Collection $roles)
    {
        $this->roles->clear();
        foreach ($roles as $role) {
            $this->roles[] = $role;
        }
    }
371774
public function addRole(Role $role)
    {
        try {
            $this->saveEntity($role);
        } catch (UniqueConstraintViolationException $e) {
            throw new NotUniqueException('Role with name ' . $role->getName() . ' already created');
        }
    }
971052
public function setRole($v)
    {
        if ($v !== null) {
            $v = (string) $v;
        }

        if ($this->role !== $v) {
            $this->role = $v;
            $this->modifiedColumns[] = RolePeer::ROLE;
        }


        return $this;
    }
147317
public function addRole(Role $role)
    {
        if (!in_array($role, $this->roles)) {
            $this->roles[] = $role;
            return true;
        }
        return false;
    }
1125828
private function _translateRoles(
        $environmentInfo,
        RoleInstance $currentInstance,
        $currentRole
    ) {
        $rolesMap = [];

        $rolesInfo = Utilities::tryGetKeysChainValue(
            $environmentInfo,
            'Roles',
            'Role'
        );

        if (!is_null($rolesInfo)) {
            if (array_key_exists('@attributes', $rolesInfo)) {
                $rolesInfo = [0 => $rolesInfo];
            }

            foreach ($rolesInfo as $roleInfo) {
                $roleInstances = $this->_translateRoleInstances($roleInfo);

                if ($roleInfo['@attributes']['name'] == $currentRole) {
                    $roleInstances[$currentInstance->getId()] = $currentInstance;
                }

                $role = new Role($roleInfo['@attributes']['name'], $roleInstances);

                foreach ($roleInstances as $instance) {
                    $instance->setRole($role);
                }

                $rolesMap[$roleInfo['@attributes']['name']] = $role;
            }
        }

        if (!array_key_exists($currentRole, $rolesMap)) {
            $roleInstances = [];
            $roleInstances[$currentInstance->getId()] = $currentInstance;

            $singleRole = new Role($currentRole, $roleInstances);
            $currentInstance->setRole($singleRole);

            $rolesMap[$currentRole] = $singleRole;
        }

        return $rolesMap;
    }