Inserts or updates RemoteUser.
@return IdentityInterface user
166854
public function setRemoteUser(ClientInterface $client, IdentityInterface $user)
    {
        $model = $this->getRemoteUser($client);
        $model->client_id = $user->getId();
        $model->save();

        return $user;
    }
1221073
public function authenticate(GenericUserInterface $user, $credential)
    {
        $userAttributes = $user->getAttributes();
        if (empty($userAttributes)) {
            throw new IdentityNotFoundException();
        }
        $this->store($user->getAttributes());

        return true;
    }
1129886
@PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("/{userId}/email")
    @Description("Update user email")
    public PrincipalUserDto updateUserEmail(@Context HttpServletRequest req,
        @PathParam("userId") final BigInteger userId,
        @FormParam("email") final String email) {
        if (userId == null || userId.compareTo(BigInteger.ZERO) < 1) {
            throw new WebApplicationException("User Id cannot be null and must be a positive non-zero number.", Status.BAD_REQUEST);
        }
        if (email == null || email.isEmpty()) {
            throw new WebApplicationException("Cannot update with null or empty email.", Status.BAD_REQUEST);
        }

        PrincipalUser remoteUser = getRemoteUser(req);
        PrincipalUser user = _uService.findUserByPrimaryKey(userId);

        validateResourceAuthorization(req, user, remoteUser);
        if (user == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND.getReasonPhrase(), Response.Status.NOT_FOUND);
        }
        user.setEmail(email);
        user = _uService.updateUser(user);
        return PrincipalUserDto.transformToDto(user);
    }
1145632
function signIn()
    {
        $this->identifier->withIdentity()->import($this->withIdentity());
        $this->identifier->signIn();
        return $this;
    }
649177
public function setUser($user = null)
    {
        $this->user = null;

        if (!$user = $this->internalResolveUser($user)) {
            $this->user = $this->userId = null;
        } else {
            $this->userId = $user->id;
            $this->user = $user;
        }

        return $this;
    }
297774
function setCurrentUser(UserInterface $user)
    {
        $this->currentUser = $user;

        // accessing the current user from the
        // DI container is deprecated
        $this->app['user'] = $user;

        return $this;
    }
1005304
public function setUser(User $v = null)
    {
        if ($v === null) {
            $this->setUserId(NULL);
        } else {
            $this->setUserId($v->getId());
        }

        $this->aUser = $v;

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


        return $this;
    }
1096769
public function update($user, array $credentials)
    {
        if (! $user instanceof User)
        {
            $user = $this->findById($user);
        }

        if ($user instanceof Roleable && isset($credentials['roles']))
        {
            foreach ($user->getRoles() as $role)
            {
                /** @var Role $role */
                $idx = array_search($role->getRoleSlug(), $credentials['roles']);
                if ($idx === false)
                {
                    $user->removeRole($role);
                }
                else
                {
                    unset($credentials['roles'][$idx]);
                }
            }

            foreach ($credentials['roles'] as $roleSlug)
            {
                /** @var Role|null $role */
                $role = $this->roles->findBySlug($roleSlug);

                if (! $role)
                {
                    throw new \InvalidArgumentException("Role [$roleSlug] does not exist.");
                }

                $user->addRole($role);
            }

            unset($credentials['roles']);
        }

        $user->update($credentials);

        return $this->save($user);
    }
1072138
public function authorize(): bool
    {
        $twoFactor = $this->user($this->route('guard'))->getTwoFactor();

        if (! $twoFactor['totp']['enabled']) {
            throw new GenericException(trans('cortex/auth::messages.verification.twofactor.totp.cant_backup'), route('adminarea.account.settings'));
        }

        return true;
    }
87277
public function add() {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
        $this->filteredSelectOptions();
    }
1314819
public function authorize(): bool
    {
        parent::authorize();

        $user = $this->user($this->route('guard'))
                ?? $this->attemptUser($this->route('guard'))
                   ?? app('cortex.auth.admin')->whereNotNull('phone')->where('phone', $this->get('phone'))->first();

        if (! $user) {
            // User instance required to detect active TwoFactor methods
            throw new GenericException(trans('cortex/foundation::messages.session_required'), route('adminarea.login'));
        }

        return true;
    }
342609
public function getIdentity(): ?IdentityInterface
    {
        if (!$this->identity) {
            // check user agent
            if (Session::get('auth:agent') === Request::getServer('HTTP_USER_AGENT')) {
                $this->identity = Session::get('auth:identity');
            } else {
                $this->clearIdentity();
            }
        }
        return $this->identity;
    }
1189066
public function currentUser(): User
    {
        if (BunqContext::getUserContext()->isOnlyUserCompanySet()) {
            return new User(BunqContext::getUserContext()->getUserCompany());
        }

        if (BunqContext::getUserContext()->isOnlyUserPersonSet()) {
            return new User(BunqContext::getUserContext()->getUserPerson());
        }

        throw new RuntimeException('Unexpected user type received.');
    }
1320190
public function createSession()
    {
        if (!$this->getUser()) {
            throw new \Exceptions\InternalException('Will not create session where no user is set');
        }

        $this->destroySessions(); // Delete old sessions for user

        $app = App::getInstance();

        $sql = $app['safesql']->query(
            "INSERT INTO `login`
            (
                session_id,
                session_name,
                ip,
                browser,
                user,
                logged_in,
                deleted
            ) VALUES (
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                1,
                0
            )",
            array(
                $app['env']['session']->getId(),
                SESSION_NAME,
                $app['env']['RemoteIP'],
                $app['env']['RemoteUA'],
                $this->getUser(),
            ));
        $app['db']->query($sql);

        $app['env']['session']['uname'] = $this->getUser();
        $app['env']['session']['loggedin'] = true;
    }
663702
protected function reconcileRootUser(OutputInterface $output)
    {
        if ($this->isDbSetup === false) {
            return true;
        }
        /** @var UsersRepository $repo */
        $repo = $this->app['storage']->getRepository(Entity\Users::class);
        if ($repo->count() > 0) {
            return true;
        }

        $this->step(++$this->step, 'Creating First User');
        if (!$this->io->confirm('Would you like to create the first user now?', false)) {
            return true;
        }

        $command = $this->getApplication()->find('user:add');
        $input = new ArrayInput([
            'role'     => ['root'],
            '--enable' => true,
        ]);
        $result = $command->run($input, $output);
        if ($result === 0) {
            $this->firstUser = $input->getArguments();
        }

        return $result === 0;
    }
470930
public function authenticate()
    {
        if (!$this->validate())
            return false;

        // Load the RemoteUserIdentity model, and return if we successfully could authenticate against it
        $this->_identity = new RemoteUserIdentity($this->adapter, $this->provider, $this->_user);
        return $this->_identity->authenticate();
    }
294556
public function addRemoteIdentity($provider, $identifier)
    {
        //! @todo delete all by provider and identifier
        $model = new UserRemoteIdentity();
        $model->setAttributes([
            'user_id' => $this->id,
            'provider' => $provider,
            'identifier' => $identifier,
        ], false);

        return $model->save();
    }