You are here

class EmailTemplateService in Lightweight Directory Access Protocol (LDAP) 8.3

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

Class EmailTemplateService.

@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 18

Namespace

Drupal\ldap_authentication\Routing
View source
class EmailTemplateService implements EventSubscriberInterface {
  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) {
    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() {
    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.
   */
  public static function profileNeedsUpdate() {
    $proxy = \Drupal::currentUser();
    $result = FALSE;

    // We only want non-anonymous and non-1 users.
    if ($proxy
      ->id() != 1 && $proxy
      ->isAuthenticated()) {
      $user = User::load($proxy
        ->id());
      $regex = \Drupal::config('ldap_authentication.settings')
        ->get('emailTemplateUsagePromptRegex');
      $regex = '`' . $regex . '`i';
      if (preg_match($regex, $user
        ->get('mail')->value)) {
        $result = TRUE;
      }
    }
    return $result;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
EmailTemplateService::$config private property
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.