UserEmailVerificationVerify.php in User email verification 8
File
src/Controller/UserEmailVerificationVerify.php
View source
<?php
namespace Drupal\user_email_verification\Controller;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\user\UserInterface;
use Drupal\user_email_verification\Event\UserEmailVerificationEvents;
use Drupal\user_email_verification\Event\UserEmailVerificationVerifyEvent;
use Drupal\user_email_verification\UserEmailVerificationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class UserEmailVerificationVerify extends ControllerBase implements ContainerInjectionInterface {
protected $userEmailVerification;
protected $time;
protected $eventDispatcher;
public function __construct(UserEmailVerificationInterface $user_email_verification_service, TimeInterface $datetime_time, EventDispatcherInterface $event_dispatcher) {
$this->userEmailVerification = $user_email_verification_service;
$this->time = $datetime_time;
$this->eventDispatcher = $event_dispatcher;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user_email_verification.service'), $container
->get('datetime.time'), $container
->get('event_dispatcher'));
}
public function verify($uid, $timestamp, $hashed_pass) {
$uid = (int) $uid;
$timestamp = (int) $timestamp;
$timeout = $this->userEmailVerification
->getValidateInterval();
$current = $this->time
->getRequestTime();
if ($current - $timestamp > $timeout) {
$this
->messenger()
->addError($this
->t('Your verification link was expired. Request a new one using the form below.'));
return $this
->redirect('user_email_verification.request');
}
$verification = $this->userEmailVerification
->loadVerificationByUserId($uid);
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');
}
if ($verification['verified']) {
$this
->messenger()
->addStatus($this
->t('Email is already verified.'));
return $this
->redirect('<front>');
}
$user = $this
->entityTypeManager()
->getStorage('user')
->load($uid);
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.'));
$event = new UserEmailVerificationVerifyEvent($user, $user
->isBlocked());
$this->eventDispatcher
->dispatch(UserEmailVerificationEvents::VERIFY, $event);
if ($event
->notifyAsBlocked()) {
$this->userEmailVerification
->sendVerifyBlockedMail($user);
$this
->messenger()
->addWarning($this
->t('Your account has been blocked before the verification of the Email. An administrator will make an audit and unblock your account if the reason for the blocking was the Email verification.'));
return $this
->redirect('<front>');
}
elseif ($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');
}
}