Assigns a role to a user for all apps in the system.

@param $user
@param $defaultRole

@return bool
@throws \Exception
498921
public static function applyDefaultUserAppRole($user, $defaultRole)
    {
        $apps = App::all();

        if (count($apps) === 0) {
            return false;
        }

        foreach ($apps as $app) {
            if (!UserAppRole::whereUserId($user->id)->whereAppId($app->id)->exists()) {
                $userAppRoleData = [
                    'user_id' => $user->id,
                    'app_id'  => $app->id,
                    'role_id' => $defaultRole
                ];

                UserAppRole::create($userAppRoleData);
            }
        }

        return true;
    }
1192127
public function assignRole($roleName, $userId)
    {
        $role = $this->role->findByName($roleName);
        $user = $this->model->find($userId);

        $user->roles()->attach($role);
    }
1150908
public Principal pushRole(Principal user, String role)
    {
        if (user==null)
            user=new User();
        
        return new WrappedUser(user,role);
    }
76041
public function actionRoleAssign() : void
    {
        $username = $this->prompt('Username:', ['required' => true]);
        $user = $this->findModel($username);
        $roleName = $this->select('Role:', ArrayHelper::map($this->authManager->getRoles(), 'name', 'description'));
        $role = $this->authManager->getRole($roleName);
        $this->authManager->assign($role, $user->id);
        $this->log(true);
    }
435342
public function assignRole($roleId = null)
    {
        $roles = $this->roles;

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

        return false;
    }
320790
protected function addUserRole()
    {
        $this->controlAccess('user_role_add');

        if ($this->role->add($this->getSubmitted())) {
            $this->redirect('admin/user/role', $this->text('Role has been added'), 'success');
        }

        $this->redirect('', $this->text('Role has not been added'), 'warning');
    }
128136
public function setRol($rol)
    {
        if (in_array($rol, self::getAllowedRoles())) {
            $u = new \WP_User($this->ID);
            $u->set_role($rol);
            return true;
        }
        return false;
    }
1115800
public function getNonPlatformRolesForUser(User $user, $executeQuery = true)
    {
        return $this->roleRepo->findNonPlatformRolesForUser($user, $executeQuery);
    }
785975
def add_user_role(self, user, role):
        
        self.service.add_user_role(
            user, role,
            self.url_prefix, self.auth, self.session, self.session_send_opts)
491838
def add_role(user, roles):
        
        def _add_role(role):
            user_role = UserRole()
            user_role.user_id = user.user_id
            user_role.role_id = role.role_id
            db.session.add(user_role)
            db.session.commit()
        [_add_role(role) for role in roles]
1190925
public ServerUpdater addRoleToUser(User user, Role role) {
        delegate.addRoleToUser(user, role);
        return this;
    }
1033857
public function setRole(Role $v = null)
    {
        if ($v === null) {
            $this->setRoleId(NULL);
        } else {
            $this->setRoleId($v->getId());
        }

        $this->aRole = $v;

        // Add binding for other direction of this n:n relationship.
        // If this object has already been added to the Role object, it will not be re-added.
        if ($v !== null) {
            $v->addUserRole($this);
        }


        return $this;
    }
1052253
public function setRole($role)
    {
        if ( ! in_array($role, [self::ROLE_ADMIN, self::ROLE_EDITOR, self::ROLE_USER]))
            throw new \Exception('Unknown role `' . $role . '`');

        $this->role = $role;

        return $this;
    }
939541
function assign($Role, $UserID = null)
	{
	    if ($UserID === null)
		    throw new \RbacUserNotProvidedException ("\$UserID is a required argument.");

		if (is_numeric($Role))
		{
			$RoleID = $Role;
		} else {
			if (substr($Role, 0, 1) == "/")
				$RoleID = Jf::$Rbac->Roles->pathId($Role);
			else
				$RoleID = Jf::$Rbac->Roles->titleId($Role);
		}

		$res = Jf::sql ( "INSERT INTO {$this->tablePrefix()}userroles
				(UserID,RoleID,AssignmentDate)
				VALUES (?,?,?)
				", $UserID, $RoleID, Jf::time () );
		return $res >= 1;
	}
309872
public function setRoleToRoleSubject(AbstractRoleSubject $ars, $roleName, $validate = true)
    {
        $role = $this->roleRepo->findOneBy(['name' => $roleName]);

        if ($validate) {
            $validated = $this->validateRoleInsert($ars, $role);
        } else {
            $validated = true;
        }

        if (!$validated) {
            throw new Exception\AddRoleException();
        }

        if ('Claroline\CoreBundle\Entity\Group' === get_class($ars) && 'ROLE_USER' === $role->getName()) {
            throw new Exception\AddRoleException('ROLE_USER cannot be added to groups');
        }

        if (!is_null($role)) {
            $ars->addRole($role);
            $this->om->persist($ars);
            $this->om->flush();
        }
    }
1127422
def add_role_to_user(self, user, role):
        
        user, role = self._prepare_role_modify_args(user, role)
        if role not in user.roles:
            user.roles.append(role)
            self.put(user)
            return True
        return False
156252
public function setAdminRole($user) {
    $classes = $this->loadClasses();

    $auth = Yii::$app->authManager;
    $auth->revokeAll($user->id);

    foreach ($classes as $item) {
      foreach ($item->adminRoles() as $role) {
        $auth->assign($auth->getRole($role), $user->id);
      }
    }

  }