Gets the user's MFA device if there is one

@param  int $iUserId The user's ID

@return mixed         \stdClass on success, false on failure
430260
public function mfaDeviceSecretGet($iUserId)
    {
        $oDb      = Factory::service('Database');
        $oEncrypt = Factory::service('Encrypt');

        $oDb->where('user_id', $iUserId);
        $oDb->limit(1);
        $aResult = $oDb->get(NAILS_DB_PREFIX . 'user_auth_two_factor_device_secret')->result();

        if (empty($aResult)) {
            return false;
        }

        $oReturn         = reset($aResult);
        $oReturn->secret = $oEncrypt->decode($oReturn->secret, APP_PRIVATE_KEY);

        return $oReturn;
    }
825950
public function getAlternatesortstring1ByUserId($userID) {
        return DB::table('email_messages')
            ->join('email_attachments', 'email_messages.id', '=', 'email_attachments.email_messages_id')
            ->select('email_attachments.alternate_sort_string1')
            ->where('email_messages.user_id', '=', $userID)
            ->distinct()
            ->orderBy('email_attachments.alternate_sort_string1', 'desc')
            ->get()
        ;
    }
576896
public function mfaDeviceSecretGenerate($iUserId, $sExistingSecret = null)
    {
        //  Get an identifier for the user
        $oUserModel = Factory::model('User', 'nails/module-auth');
        $oUser      = $oUserModel->getById($iUserId);

        if (!$oUser) {
            $this->setError('User does not exist.');
            return false;
        }

        $oGoogleAuth = new GoogleAuthenticator();

        //  Generate the secret
        if (empty($sExistingSecret)) {
            $sSecret = $oGoogleAuth->generateSecret();
        } else {
            $sSecret = $sExistingSecret;
        }

        //  Get the hostname
        $sHostname = Functions::getDomainFromUrl(BASE_URL);

        //  User identifier
        $sUsername = $oUser->username;
        $sUsername = empty($sUsername) ? preg_replace('/[^a-z]/', '', strtolower($oUser->first_name . $oUser->last_name)) : $sUsername;
        $sUsername = empty($sUsername) ? preg_replace('/[^a-z]/', '', strtolower($oUser->email)) : $sUsername;

        return [
            'secret' => $sSecret,
            'url'    => $oGoogleAuth->getUrl($sUsername, $sHostname, $sSecret),
        ];
    }
30734
protected function getUser4Id($userId): ?UserInterface
    {
        /** @var \PServerCore\Entity\Repository\User $userRepository */
        $userRepository = $this->entityManager->getRepository($this->entityOptions->getUser());

        return $userRepository->getUser4Id($userId);
    }
477251
public function getKeyById(string $id = null)
    {
        if ($id) {
            if (!isset($this->keys[$id])) {
                throw new AuthenticationException("Unknown 'kid' $id");
            }

            return $this->keys[$id];
        }
        if (count($this->keys) > 1) {
            throw new AuthenticationException("Missing 'kid'");
        }

        return current($this->keys);
    }
1002621
public function getCredentialObject($userId = null)
    {
        if ($userId == null && array_key_exists($this->defaultAccountName, $this->credentialHashmap)) {
            $credObj = $this->credentialHashmap[$this->defaultAccountName];
        } else if (array_key_exists($userId, $this->credentialHashmap)) {
            $credObj = $this->credentialHashmap[$userId];
        }

        if (empty($credObj)) {
            throw new BlockCypherInvalidCredentialException("Credential not found for " . ($userId ? $userId : " default user") .
                ". Please make sure your configuration/APIContext has credential information");
        }
        return $credObj;
    }
1235516
public function getDeviceForUserAgent($userAgent = '')
    {
        if (!is_string($userAgent)) {
            $userAgent = '';
        }

        $requestFactory = new Request\GenericRequestFactory();

        $request = $requestFactory->createRequestForUserAgent($userAgent);
        $device  = $this->getDeviceForRequest($request);

        return $device;
    }
953173
public function get($userId)
    {
        $endpoint = '/admin/users/'.$userId.'.json';
        $response = $this->request($endpoint);
        return $this->createObject(User::class, $response['user']);
    }
185903
public function getDeviceFromID($deviceId)
    {
        $success = null;
        $data    = $this->getAdapter()->getItem($deviceId, $success);

        if (!$success) {
            return;
        }

        return $data;
    }
1329219
public function resetFailedLogin($iUserId)
    {
        $oDb = Factory::service('Database');
        $oDb->set('failed_login_count', 0);
        $oDb->set('failed_login_expires', 'null', false);
        return $this->update($iUserId);
    }
678114
public static function getUser($userId)
    {
        /** @var \tao_models_classes_UserService $userService */
        $userService = ServiceManager::getServiceManager()->get(\tao_models_classes_UserService::SERVICE_ID);
        $user = $userService->getUserById($userId);
        return $user;
    }
734883
public function getDevice($deviceId)
    {
        if (!isset($this->_deviceCache[$deviceId])) {
            $device = $this->persistenceStorage->load($deviceId);
            if (!$device) {
                throw new Exception("There is no device with ID [$deviceId] in the loaded WURFL Data");
            }
            $this->_deviceCache[$deviceId] = $device;
        }
        return $this->_deviceCache[$deviceId];
    }
261779
public function mfaDeviceSecretValidate($iUserId, $sSecret, $iCode)
    {
        //  Tidy up codes so that they only contain digits
        $sCode = preg_replace('/[^\d]/', '', $iCode);

        //  New instance of the authenticator
        $oGoogleAuth = new GoogleAuthenticator();

        if ($oGoogleAuth->checkCode($sSecret, $sCode)) {

            $oDb      = Factory::service('Database');
            $oEncrypt = Factory::service('Encrypt');

            $oDb->set('user_id', $iUserId);
            $oDb->set('secret', $oEncrypt->encode($sSecret, APP_PRIVATE_KEY));
            $oDb->set('created', 'NOW()', false);

            if ($oDb->insert(NAILS_DB_PREFIX . 'user_auth_two_factor_device_secret')) {

                $iSecretId = $oDb->insert_id();
                $oNow      = Factory::factory('DateTime');

                $oDb->set('secret_id', $iSecretId);
                $oDb->set('code', $sCode);
                $oDb->set('used', $oNow->format('Y-m-d H:i:s'));
                $oDb->insert(NAILS_DB_PREFIX . 'user_auth_two_factor_device_code');

                return true;

            } else {
                $this->setError('Could not save secret.');
                return false;
            }

        } else {
            $this->setError('Codes did not validate.');
            return false;
        }
    }
203576
public function findForUser($id, $userId)
    {
        return Passport::token()->where('id', $id)->where('user_id', $userId)->first();
    }
981600
public static function getWardForUser($userId)
    {
        $entityManager = (object) DoctrineBootstrap::getEntityManager();
        $user = $entityManager->getRepository('Hospitalplugin\Entities\User')->findOneById($userId);
        return $user->getWard();
    }
1033183
public function mfaDeviceCodeValidate($iUserId, $sCode)
    {
        //  Get the user's secret
        $oSecret = $this->mfaDeviceSecretGet($iUserId);

        if (!$oSecret) {
            $this->setError('Invalid User');
            return false;
        }

        //  Has the code been used before?
        $oDb = Factory::service('Database');
        $oDb->where('secret_id', $oSecret->id);
        $oDb->where('code', $sCode);

        if ($oDb->count_all_results(NAILS_DB_PREFIX . 'user_auth_two_factor_device_code')) {
            $this->setError('Code has already been used.');
            return false;
        }

        //  Tidy up codes so that they only contain digits
        $sCode = preg_replace('/[^\d]/', '', $sCode);

        //  New instance of the authenticator
        $oGoogleAuth = new GoogleAuthenticator();
        $checkCode   = $oGoogleAuth->checkCode($oSecret->secret, $sCode);

        if ($checkCode) {

            //  Log the code so it can't be used again
            $oDb->set('secret_id', $oSecret->id);
            $oDb->set('code', $sCode);
            $oDb->set('used', 'NOW()', false);

            $oDb->insert(NAILS_DB_PREFIX . 'user_auth_two_factor_device_code');

            return true;

        } else {
            return false;
        }
    }
1080675
static protected function getUserCacheByUserId( $userId )
    {
        $userCache = null;

        if ( eZINI::instance()->variable( 'RoleSettings', 'EnableCaching' ) === 'true' )
        {
            $cacheFilePath = eZUser::getCacheDir( $userId ) . "/user-data-{$userId}.cache.php";
            $cacheFile = eZClusterFileHandler::instance( $cacheFilePath );
            $userCache = $cacheFile->processCache( array( 'eZUser', 'retrieveUserCacheFromFile' ),
                                                   array( 'eZUser', 'generateUserCacheForFile' ),
                                                   null,
                                                   self::userInfoExpiry(),
                                                   $userId );
        }

        if ( $userCache === null || $userCache instanceof eZClusterFileFailure )
        {
            $userCache = self::generateUserCacheContent( $userId );
        }

        return $userCache;
    }