You are here

public function UserEmailVerificationVerifyExtended::verify in User email verification 8

Callback to handle user's Email verification.

Parameters

int $uid: User ID to verify email for.

int $timestamp: The timestamp when verification link was generated.

string $hashed_pass: Hashed pass.

1 string reference to 'UserEmailVerificationVerifyExtended::verify'
user_email_verification.routing.yml in ./user_email_verification.routing.yml
user_email_verification.routing.yml

File

src/Controller/UserEmailVerificationVerifyExtended.php, line 64

Class

UserEmailVerificationVerifyExtended
Email verificationVerify controller.

Namespace

Drupal\user_email_verification\Controller

Code

public function verify($uid, $timestamp, $hashed_pass) {
  $uid = (int) $uid;
  $timestamp = (int) $timestamp;
  $extended_timeout = $this->userEmailVerification
    ->getExtendedValidateInterval();
  $current = $this->time
    ->getRequestTime();
  if ($current - $timestamp > $extended_timeout) {
    $this
      ->messenger()
      ->addError($this
      ->t('Your verify link has been expired and account has been deleted. Register a new one, or contact site administration.'));
    return $this
      ->redirect('<front>');
  }
  $verification = $this->userEmailVerification
    ->loadVerificationByUserId($uid);

  // User tries to use verification link that doesn't belong to him
  // or link was created for user which doesn't exist.
  if ($this
    ->currentUser()
    ->isAuthenticated() && $this
    ->currentUser()
    ->id() != $uid || !$verification) {
    $this
      ->messenger()
      ->addError($this
      ->t('Your verification link is incorrect. Request a new one using the form below.'));
    return $this
      ->redirect('user_email_verification.request');
  }

  // Email for requested user was already verified.
  if ($verification['verified']) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Email is already verified.'));
    return $this
      ->redirect('<front>');
  }
  $user = $this
    ->entityTypeManager()
    ->getStorage('user')
    ->load($uid);

  // User exists and requested hash is correct.
  if ($user instanceof UserInterface && $hashed_pass === $this->userEmailVerification
    ->buildHmac($user
    ->id(), $timestamp)) {
    $this->userEmailVerification
      ->setEmailVerifiedByUserId($user
      ->id());
    $this
      ->messenger()
      ->addStatus($this
      ->t('Thank you for verifying your Email address.'));

    // Activate blocked account.
    if ($user
      ->isBlocked()) {
      $user
        ->activate();
      $user
        ->save();
      $this
        ->messenger()
        ->addStatus($this
        ->t('Your account is activated.'));
    }
    if ($this
      ->currentUser()
      ->isAuthenticated()) {
      return $this
        ->redirect('entity.user.canonical', [
        'user' => $this
          ->currentUser()
          ->id(),
      ]);
    }
    else {
      return $this
        ->redirect('<front>');
    }
  }
  $this
    ->messenger()
    ->addError($this
    ->t('Your verification link is incorrect. Request a new one using the form below.'));
  return $this
    ->redirect('user_email_verification.request');
}