Add a role to a given user

@param UserEntity $user
@param string     $role name of the role to add
435052
public function addRoleForUser(UserEntity $user, $role)
    {
        $roles = $user->getRoles();

        if (! in_array($role, $roles)) {
            $this->userRolePivotMapper->addRoleForUser($user->getId(), $role);

            array_push($roles, $role);

            $user->setRoles($roles);
        }
    }
1296413
public function addUser(UserInterface $user)
    {
        $this->users->add($user);

        if ($user->hasRole($this) === false)
            $user->addRole($this);

        return $this;
    }
1201393
protected void addUserToRole(String user, String role) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
            SibTr.entry(tc, CLASS_NAME + "addUserToRole", new Object[] { user, role });
        }
        Set<String> usersForTheRole = roleToUserMap.get(role);
        if (usersForTheRole != null) {
            usersForTheRole.add(user);
        } else {
            usersForTheRole = new HashSet<String>();
            usersForTheRole.add(user);
        }
        roleToUserMap.put(role, usersForTheRole);
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
            SibTr.exit(tc, CLASS_NAME + "addUserToRole");
        }
    }
794968
public function addUserRole(ChildUserRole $l)
    {
        if ($this->collUserRoles === null) {
            $this->initUserRoles();
            $this->collUserRolesPartial = true;
        }

        if (!$this->collUserRoles->contains($l)) {
            $this->doAddUserRole($l);
        }

        return $this;
    }
1191297
protected function addRolesToSU(Role $roleSu)
    {
        $this->stdout("Init SU assigning roles\n",  Console::FG_YELLOW);
        foreach ($this->authManager->getRoles() as $role) {
            try {
                if ($role->name !== $roleSu->name) {
                    $this->authManager->addChild($roleSu, $role);
                    $this->stdout("\tAssign to SU role: {$role->name} - success\n", Console::FG_GREEN);
                }
            } catch(\Exception $e) {
                $this->stdout("\t- already exist\n",  Console::FG_RED);
            }
        };
    }
860729
def add_user_role(self, user, role_name):
        """"""

        # For SQL: user.roles is list of pointers to Role objects
        if isinstance(self.db_adapter, SQLDbAdapter):
            # user.roles is a list of Role IDs
            # Get or add role
            role = self.db_adapter.find_first_object(self.RoleClass, name=role_name)
            if not role:
                role = self.RoleClass(name=role_name)
                self.db_adapter.add_object(role)
            user.roles.append(role)

        # For others: user.roles is a list of role names
        else:
            # user.roles is a list of role names
            user.roles.append(role_name)
936640
public function setRole(UserInterface $user, array $selectedRole)
    {
        $choices = $this->rolesManager->getChoices();

        if (!isset($choices[$selectedRole['roles']])) {
            throw new InvalidArgumentException(
                sprintf('Impossible to set role %s', $selectedRole['roles'])
            );
        }

        $user->setRoles([$choices[$selectedRole['roles']]]);

        $this->entityManager->flush();
    }
571709
public function bucket_acl_entity_add( $entity, $role ) {
	
		$result = null;
		try {
			
			$result = $this->bucket_acl->add( $entity, $role);
		
		}
		catch( \Exception $e ) {
		
			$result = $e;
			$this->errors[$this->error_count] = $e->getServiceException()->getMessage();
			$this->error_count++;
		
		}
		return $result;
	
	}
344132
protected function updateUserShadowLogin(Entity\Users $userEntity)
    {
        if (Carbon::now() > $userEntity->getShadowvalidity()) {
            $userEntity->setShadowpassword(null);
            $userEntity->setShadowtoken(null);
            $userEntity->setShadowvalidity(null);
        }

        return $userEntity;
    }
1317783
public function grantUserRole($user, $role)
    {
        $params = [
            'user' => $user,
            'role' => $role,
        ];

        $body = $this->request(self::URI_AUTH_USER_GRANT, $params);

        return $body;
    }
794068
public function edit(User $user, Role $role)
    {
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);

        $user->load(['roles', 'roles.permissions']);
        $roles = $role->all();

        $this->setTitle($title = trans('auth::users.titles.edit-user'));
        $this->addBreadcrumb($title);

        return $this->view('admin.users.edit', compact('user', 'roles'));
    }
826130
public function handleLogin(UserEntity $user)
    {
        $accessToken = new AccessToken($this->tokenStorage, $this->tokenStorage);
        $token = $accessToken->createAccessToken('', $user->getId(), null, true);

        return $token;
    }
294435
public function removeUserRole($user, $role)
    {
        $user   =   $this->findUserById($user);
        $role   =   $this->findRoleByKey($role);

        if ($user->not($role->key)) {
            return true;
        }

        return $user->leave($role);
    }
317739
def add_user_role(self, user, role):
        
        self.project_service.set_auth(self._token_project)
        self.project_service.add_user_role(user, role)
330104
public void addUser(String username, Role role) {
        if (StringUtils.isNullOrEmpty(username)) {
            throw new IllegalArgumentException("A username must be specified.");
        }

        if (role == null) {
            throw new IllegalArgumentException("A role must be specified.");
        }

        users.add(new User(username, role));
    }
342279
public function addChildRole(Role $parentRole, Role $childRole)
    {
        $roleHierarchy = new RoleHierarchy();

        $roleHierarchy->setParentRole($parentRole);
        $roleHierarchy->setChildRole($childRole);

        $this->checkForCyclicHierarchy($childRole->getId(), $parentRole->getId());

        try {
            $this->saveEntity($roleHierarchy);
        }  catch (UniqueConstraintViolationException $e) {
            throw new NotUniqueException('Child role ' . $childRole->getName() . ' is already has parent role ' . $parentRole->getName());
        }
    }
731532
def add_role(self, role):
        
        schema = RoleSchema()
        ok = schema.process(role)
        if not ok or not role.id:
            err = 'Role must be valid and saved before adding to user'
            raise x.UserException(err)

        self.__roles.append(role)