Helper function to safely switch org during login

@param \Gems_User_User $user
430731
protected function setCurrentOrganizationTo(\Gems_User_User $user)
    {
        if ($this->currentUser !== $user) {
            $this->currentUser->setCurrentOrganization($user->getCurrentOrganization());
        }
    }
1152162
public function setUser(User $user)
    {
        $this->user = $user;

        unset($_SESSION[self::class][$this->getConfig()->getEntityID()]['user']);

        $_SESSION[self::class][$this->getConfig()->getEntityID()]['user'] = $this->user->toArray();
        $this->setRole($this->user->getCurrentRole());
        $this->setLocalUsername($this->user->getLocalUsername());

        return $this;
    }
713682
private function cleanFtpCredentials(User $user)
    {
        if (null !== $credential = $user->getFtpCredential()) {
            $this->objectManager->remove($credential);
        }
    }
366854
protected function fireOtherDeviceLogoutEvent($user)
    {
        if (isset($this->events)) {
            $this->events->dispatch(new Events\OtherDeviceLogout(
                $this->name, $user
            ));
        }
    }
429342
public function beforeSave(Event $event, User $user)
    {
        if (!$user->isNew() && $user->has('password') && empty($user->password)) {
            $user->unsetProperty('password');
            $user->dirty('password', false);
        }
    }
1310034
public function refresh(User $user)
    {
        if ($user['id'] == $this->session['user']['id']) {
            $this->loginByRecord($user);
        }

        return $this;
    }
477899
protected function childChangeUser($user)
    {
        if (is_array($user) || ($user = $this->tryChangeUser($user))) {
            posix_setgid($user['gid']);
            posix_setuid($user['uid']);
        }
    }
249054
private function cleanUserSessions(User $user)
    {
        $sessions = $this->objectManager->getRepository('Phraseanet:Session')
            ->findByUser(['user' => $user]);

        foreach ($sessions as $session) {
            $this->objectManager->remove($session);
        }
    }
563614
public function convertGuestToUser($user, $credentials, $activate = false)
    {
        $user->fill($credentials);
        $user->convertToRegistered(false);

        // Remove user from guest group
        if ($group = UserGroupModel::getGuestGroup()) {
            $user->groups()->remove($group);
        }

        if ($activate) {
            $user->attemptActivation($user->getActivationCode());
        }

        // Prevents revalidation of the password field
        // on subsequent saves to this model object
        $user->password = null;

        return $this->user = $user;
    }
772896
function isOwner($user) {
        if($user instanceof \CharlotteDunois\Yasmin\Models\User || $user instanceof \CharlotteDunois\Yasmin\Models\GuildMember) {
            $user = $user->id;
        }
        
        return (\in_array($user, $this->options['owners']));
    }
371684
public function postlogout_hook($user) {
        global $CFG;
        // Only redirect to CAS logout if the user is logged as a CAS user.
        if (!empty($this->config->logoutcas) && $user->auth == $this->authtype) {
            $backurl = !empty($this->config->logout_return_url) ? $this->config->logout_return_url : $CFG->wwwroot;
            $this->connectCAS();
            phpCAS::logoutWithRedirectService($backurl);
        }
    }
153458
public function forceUserStateless(UserInterface $user)
    {
        $user = $this->sentinel->authenticate($user, false, false);

        if ( ! ($user instanceof CartalystUserInterface)) {
            return false;
        }

        event( new CmsUserLoggedIn($user, true, true) );
        return true;
    }
639055
private function cleanAuthProvider(User $user)
    {
        $providers = $this->objectManager->getRepository('Phraseanet:UsrAuthProvider')
            ->findBy(['user' => $user]);

        foreach ($providers as $provider) {
            $this->objectManager->remove($provider);
        }
    }
693620
public function isApiEnabledForUser(User $user)
    {
        $roles = $user->roles->pluck('id')->toArray();
        return !count($roles) ? false : $this->newQuery()->whereIn('role_id', array_values($roles))->where('enabled', 1)->get()->count() > 0;
    }
844837
public function action_personal_options( WP_User $user ) {
		$link = self::maybe_switch_url( $user );

		if ( ! $link ) {
			return;
		}

		?>
		<tr>
			<th scope="row"><?php echo esc_html_x( 'User Switching', 'User Switching title on user profile screen', 'user-switching' ); ?></th>
			<td><a href="<?php echo esc_url( $link ); ?>"><?php esc_html_e( 'Switch&nbsp;To', 'user-switching' ); ?></a></td>
		</tr>
		<?php
	}
1025861
public function changePassword(User $user, Request $request)
    {
        $user->password = Hash::make($request->get('password'));
        $user->save();

        Alert::info(__('User was saved'));

        return back();
    }
1239989
public function getAuthAdapter(\Gems_User_User $user, $password)
    {
        $db2 = $this->getDb2();

        $credentialValidationCallback = $this->getCredentialValidationCallback();

        $adapter = new CallbackCheckAdapter($db2, 'gems__user_passwords', 'gul_login', 'gup_password', $credentialValidationCallback);

        $select = $adapter->getDbSelect();
        $select->join('gems__user_logins', 'gup_id_user = gul_id_user', array())
               ->where('gul_can_login = 1')
               ->where(array('gul_id_organization ' => $user->getBaseOrganizationId()));

        $adapter->setIdentity($user->getLoginName())
                ->setCredential($password);

        return $adapter;
    }