Finds a user by the given activation code.

@param  string  $code
@return \Cartalyst\Sentry\Users\UserInterface
@throws \Cartalyst\Sentry\Users\UserNotFoundException
@throws InvalidArgumentException
@throws RuntimeException
346348
public function findByActivationCode($code)
	{
		if ( ! $code)
		{
			throw new \InvalidArgumentException("No activation code passed.");
		}

		$model = $this->createModel();

		$result = $model->newQuery()->where('activation_code', '=', $code)->get();

		if (($count = $result->count()) > 1)
		{
			throw new \RuntimeException("Found [$count] users with the same activation code.");
		}

		if ( ! $user = $result->first())
		{
			throw new UserNotFoundException("A user was not found with the given activation code.");
		}

		return $user;
	}
848605
public function get($code)
    {
        $result = $this->getConnection()->table('oauth_auth_codes')
            ->where('oauth_auth_codes.id', $code)
            ->where('oauth_auth_codes.expire_time', '>=', time())
            ->first();

        if (is_null($result)) {
            return;
        }

        return (new AuthCodeEntity($this->getServer()))
            ->setId($result->id)
            ->setRedirectUri($result->redirect_uri)
            ->setExpireTime((int) $result->expire_time);
    }
193029
public function get($code) {
		if (!$this->responses->has($code)) {
			$this->responses->set($code, new Response($code));
		}

		return $this->responses->get($code);
	}
992406
private function resolveObject($code)
    {
        $value = $this->resolveCode($code);

        if (!\is_object($value)) {
            throw new RuntimeException('Unable to inspect a non-object');
        }

        return $value;
    }
1011557
public function checkCode($code)
    {
        $res = $this->twofactorAdapter->check($code);

        if($res)
        {
            setcookie('twofactorDir-' . $this->dir, $this->cookieCode, 0, "/");
            self::redirect();
        }

        return $res;
    }
1289791
public function findUserByPersistenceCode($code)
    {
        $persistence = $this->findByPersistenceCode($code);

        if ($persistence)
        {
            return $persistence->getUser();
        }

        return false;
    }
329790
protected function findLanguage($code) {
        $language = Language::findOne($code);
        if ($language === null) {
            throw new \yii\web\NotFoundHttpException('The requested language does not exist');
        }
        return $language;
    }
250206
public function actionConnect($code)
    {

        $account = $this->finder->findAccount()->byCode($code)->one();

        if ($account === null || $account->getIsConnected()) {
            throw new NotFoundHttpException();
        }

        /** @var User $user */
        $user = \Yii::createObject([
            'class'    => User::className(),
            'scenario' => 'connect',
            'email'    => $account->email,
        ]);
        $profile = new Profile();

        $event = $this->getConnectEvent($account, $user);

        $this->trigger(self::EVENT_BEFORE_CONNECT, $event);

        if ($user->load(\Yii::$app->request->post())) {
            if($profile->load(\Yii::$app->request->post()) && $profile->validate()) {
                $user->username = $user->email;
                $user->password = Password::generate(8);
                if($profile->user_id = $user->register(Url::to(['/user/login', 'language' => Language::getCurrent()->lang_id], true))) {
                    $profile->save();

                    $this->trigger(self::EVENT_AFTER_REGISTER, new UserRegistrationEvent([
                        'id' => $profile->user_id
                    ]));

                    $account->connect($user);
                    $this->trigger(self::EVENT_AFTER_CONNECT, $event);
                    /*\Yii::$app->user->login($user, $this->module->rememberFor);
                    return $this->goBack();*/


                    Yii::$app->session->setFlash(
                        'info',
                        Yii::t(
                            'user',
                            'Your account has been created and a message with further instructions has been sent to your email'
                        )
                    );

                    return $this->render('/message', [
                        'title'  => \Yii::t('user', 'Your account has been created'),
                        'module' => $this->module,
                        'profile' => $profile
                    ]);
                }
            }
        }

        return $this->render('connect', [
            'model'   => $user,
            'account' => $account,
            'profile' => $profile,
        ]);
    }
1366305
public function getFeatureByCode($code)
    {
        $feature = $this->features()->getEager()->first(function($item) use ($code) {
            return $item->code === $code;
        });

        if (is_null($feature)) {
            throw new InvalidPlanFeatureException($code);
        }

        return $feature;
    }
978860
public function activate($code)
    {
        try {
            // Attempt to fetch the user via the activation code
            $activation = $this->activations
                           ->createModel()
                           ->newQuery()
                           ->where('code', $code)
                           ->where('completed', false)
                           ->where('created_at', '>', Carbon::now()->subSeconds(259200))
                           ->first();

            if (!$activation) {
                $message = $this->translate("activation_problem", "Invalid or expired activation code.");
                throw new InvalidArgumentException($message);
            }
            $user = $this->sentinel->findUserById($activation->user_id);

            // Complete the user's activation
            $this->activations->complete($user, $code);

            // While we are here, lets remove any expired activations
            $this->activations->removeExpired();
        } catch (Exception $e) {
            return $this->returnException($e);
        }

        if ($user) {
            $message = $this->translate("activation_success", "Activation successful.");
            return new SuccessReply($message);
        }

        $message = $this->translate('activation_failed', 'There was a problem activating your account.');
        return new FailureReply($message);
    }
699440
public function attemptActivation($activationCode)
	{
		if ($this->activated)
		{
			throw new UserAlreadyActivatedException('Cannot attempt activation on an already activated user.');
		}

		if ($activationCode == $this->activation_code)
		{
			$this->activation_code = null;
			$this->activated       = true;
			$this->activated_at    = new DateTime;
			return $this->save();
		}

		return false;
	}
506277
public function getResponse(int $code): Response
    {
        if (!isset($this->responses[$code])) {
            if (isset($this->responses[0])) {
                // Return default response
                return $this->responses[0];
            }
            throw new \InvalidArgumentException(
                "Operation '{$this->getId()}' does not have a definition for status '$code'" .
                " (has " . implode(', ', array_keys($this->responses)) . ')'
            );
        }

        return $this->responses[$code];
    }
535897
public function code ( $code = NULL ) {
      if ( $code === NULL )
        return $this->code;
      if ( ResponseCode::find( $code ) === NULL )
        throw new \InvalidArgumentException( "Response: no code $code" );
      $this->code = intval( $code );
      return $this;
    }
1135575
public function onActivate($code = null)
    {
        try {
            $code = post('code', $code);

            $errorFields = ['code' => Lang::get(/*Invalid activation code supplied.*/'rainlab.user::lang.account.invalid_activation_code')];

            /*
             * Break up the code parts
             */
            $parts = explode('!', $code);
            if (count($parts) != 2) {
                throw new ValidationException($errorFields);
            }

            list($userId, $code) = $parts;

            if (!strlen(trim($userId)) || !strlen(trim($code))) {
                throw new ValidationException($errorFields);
            }

            if (!$user = Auth::findUserById($userId)) {
                throw new ValidationException($errorFields);
            }

            if (!$user->attemptActivation($code)) {
                throw new ValidationException($errorFields);
            }

            Flash::success(Lang::get(/*Successfully activated your account.*/'rainlab.user::lang.account.success_activation'));

            /*
             * Sign in the user
             */
            Auth::login($user);

        }
        catch (Exception $ex) {
            if (Request::ajax()) throw $ex;
            else Flash::error($ex->getMessage());
        }
    }
977352
public function remove($code)
    {
        $entityManager = $this->getEntityManager();

        $queryBuilder = $entityManager->createQueryBuilder();
        $queryBuilder
            ->delete($this->entityName(), 'p')
            ->where('p.code = :code')
            ->setParameter('code', $code);

        return $queryBuilder->getQuery()->execute();
    }
1033245
public function confirmByCode($code)
    {
        $identity = ['confirmation_code' => $code];

        $account = $this->findByIdentity($identity);

        if($account){

            return $this->confirm($account);
        } else{

            return false;
        }
    }
450999
public function checkCode(UserInterface $user, $code)
    {
        return $this->authenticator->checkCode($user->getTwoStepVerificationCode(), $code);
    }