You are here

class EmailTemplateService in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_authentication/src/Routing/EmailTemplateService.php \Drupal\ldap_authentication\Routing\EmailTemplateService

Email template service.

@package Drupal\ldap_authentication

Hierarchy

  • class \Drupal\ldap_authentication\Routing\EmailTemplateService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EmailTemplateService

1 file declares its use of EmailTemplateService
LdapAuthenticationProfileUpdateForm.php in ldap_authentication/src/Form/LdapAuthenticationProfileUpdateForm.php
1 string reference to 'EmailTemplateService'
ldap_authentication.services.yml in ldap_authentication/ldap_authentication.services.yml
ldap_authentication/ldap_authentication.services.yml
1 service uses EmailTemplateService
ldap_authentication.email_template in ldap_authentication/ldap_authentication.services.yml
Drupal\ldap_authentication\Routing\EmailTemplateService

File

ldap_authentication/src/Routing/EmailTemplateService.php, line 20

Namespace

Drupal\ldap_authentication\Routing
View source
class EmailTemplateService implements EventSubscriberInterface {

  /**
   * Config.
   *
   * @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  private $config;

  /**
   * Constructor.
   */
  public function __construct(ConfigFactory $config_factory) {
    $this->config = $config_factory
      ->get('ldap_authentication.settings');
  }

  /**
   * Check for template if enabled.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Response event.
   */
  public function checkTemplate(GetResponseEvent $event) : void {
    if ($this->config
      ->get('emailTemplateUsagePromptUser') === TRUE) {
      $this
        ->checkForEmailTemplate();
    }
  }

  /**
   * Form submit callback to check for an email template and redirect if needed.
   */
  public static function checkForEmailTemplate() : void {
    if (self::profileNeedsUpdate()) {
      $url = Url::fromRoute('ldap_authentication.profile_update_form');

      // Not injected since we need to have this callback be static.
      $currentRoute = \Drupal::service('path.current')
        ->getPath();
      if ($currentRoute !== '/user/ldap-profile-update' && $currentRoute !== '/user/logout') {
        $response = new RedirectResponse($url
          ->toString());
        $response
          ->send();
      }
    }
  }

  /**
   * Whether or not the user's profile is valid or needs to be updated on login.
   *
   * Currently this only checks if mail is valid or not according to the
   * authentication settings.
   *
   * @return bool
   *   TRUE if the user's profile is valid, otherwise FALSE.
   *
   * @todo This should not be called statically, so that we don't call
   * all these services without DI.
   */
  public static function profileNeedsUpdate() : bool {
    $proxy = \Drupal::currentUser();
    $result = FALSE;

    // We only want non-anonymous and non-1 users.
    // @todo Role exclusion needs to be checked, user 1 special case removed.
    if ($proxy
      ->id() !== 1 && $proxy
      ->isAuthenticated()) {
      $user = User::load($proxy
        ->id());
      $regex = \Drupal::config('ldap_authentication.settings')
        ->get('emailTemplateUsagePromptRegex');
      $regex = sprintf('`%s`i', $regex);
      $mail = $user
        ->get('mail')->value ?: '';
      if (preg_match($regex, $mail)) {
        $result = TRUE;
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events[KernelEvents::REQUEST][] = [
      'checkTemplate',
      30,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmailTemplateService::$config private property Config.
EmailTemplateService::checkForEmailTemplate public static function Form submit callback to check for an email template and redirect if needed.
EmailTemplateService::checkTemplate public function Check for template if enabled.
EmailTemplateService::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EmailTemplateService::profileNeedsUpdate public static function Whether or not the user's profile is valid or needs to be updated on login.
EmailTemplateService::__construct public function Constructor.