You are here

class CasPreLoginEvent in CAS 8

Same name and namespace in other branches
  1. 2.x src/Event/CasPreLoginEvent.php \Drupal\cas\Event\CasPreLoginEvent

Class CasPreLoginEvent.

CAS dispatches this event during the authentication process after a local Drupal user account has been loaded for the user attempting login, but before the user is actually authenticated to Drupal.

Subscribe to this event to:

  • Prevent the user from logging in by calling ::cancelLogin(). Additionally, a reason could be set when calling this method. If the reason was set, this message will be shown as error message to the user.
  • Change properties on the Drupal user account (like adding or removing roles). The CAS module saves the user entity after dispatching the event, so subscribers do not need to save it themselves.

Any CAS attributes will be available via the $casPropertyBag data object.

Hierarchy

  • class \Drupal\cas\Event\CasPreLoginEvent extends \Symfony\Component\EventDispatcher\Event

Expanded class hierarchy of CasPreLoginEvent

3 files declare their use of CasPreLoginEvent
CasTestSubscriber.php in tests/modules/cas_test/src/CasTestSubscriber.php
CasUserManager.php in src/Service/CasUserManager.php
CasUserManagerTest.php in tests/src/Unit/Service/CasUserManagerTest.php

File

src/Event/CasPreLoginEvent.php, line 26

Namespace

Drupal\cas\Event
View source
class CasPreLoginEvent extends Event {

  /**
   * Store the CAS property bag.
   *
   * @var \Drupal\cas\CasPropertyBag
   */
  protected $casPropertyBag;

  /**
   * The drupal user entity about to be logged in.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $account;

  /**
   * Controls whether or not the user will be allowed to login.
   *
   * @var bool
   */
  protected $allowLogin = TRUE;

  /**
   * The user message why logging-in has been canceled.
   *
   * @var \Drupal\Component\Render\MarkupInterface|string|null
   */
  protected $cancelLoginReason;

  /**
   * Constructor.
   *
   * @param \Drupal\user\UserInterface $account
   *   The drupal user entity about to be logged in.
   * @param \Drupal\cas\CasPropertyBag $cas_property_bag
   *   The CasPropertyBag of the current login cycle.
   */
  public function __construct(UserInterface $account, CasPropertyBag $cas_property_bag) {
    $this->account = $account;
    $this->casPropertyBag = $cas_property_bag;
  }

  /**
   * CasPropertyBag getter.
   *
   * @return \Drupal\cas\CasPropertyBag
   *   The casPropertyBag property.
   */
  public function getCasPropertyBag() {
    return $this->casPropertyBag;
  }

  /**
   * Return the user account entity.
   *
   * @return \Drupal\user\UserInterface
   *   The user account entity.
   */
  public function getAccount() {
    return $this->account;
  }

  /**
   * Allows the login operation.
   *
   * @return $this
   */
  public function allowLogin() {
    $this->allowLogin = TRUE;
    $this->cancelLoginReason = NULL;
    return $this;
  }

  /**
   * Cancels the login operation.
   *
   * @param \Drupal\Component\Render\MarkupInterface|string|null $reason
   *   (optional) A user message explaining why the login has been canceled. If
   *   passed, this value will be used to show a message to the user that tries
   *   to login. If omitted, a standard message will be displayed.
   *
   * @return $this
   */
  public function cancelLogin($reason = NULL) {
    $this->allowLogin = FALSE;
    $this->cancelLoginReason = $reason;
    return $this;
  }

  /**
   * Set the $allowLogin property.
   *
   * @param bool $allow_login
   *   TRUE to allow login, FALSE otherwise.
   *
   * @deprecated in cas:8.x-1.7 and is removed from cas:8.x-2.0. Use
   *   ::allowLogin() or ::cancelLogin() instead.
   */
  public function setAllowLogin($allow_login) {
    @trigger_error('Using ' . __METHOD__ . '() is deprecated in cas:8.x-1.7 and is removed from cas:8.x-2.0. Use ::allowLogin() or ::cancelLogin() instead.', E_USER_DEPRECATED);
    if ($allow_login) {
      $this
        ->allowLogin();
    }
    else {
      $this
        ->cancelLogin();
    }
  }

  /**
   * Return if this user is allowed to login.
   *
   * @return bool
   *   TRUE if the user is allowed to login, FALSE otherwise.
   */
  public function getAllowLogin() {
    return $this->allowLogin;
  }

  /**
   * Returns a user message explaining why the login process is asked to cancel.
   *
   * @return \Drupal\Component\Render\MarkupInterface|string|null
   *   The reason why the login process is asked to cancel, if any has been set.
   */
  public function getCancelLoginReason() {
    return $this->cancelLoginReason;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CasPreLoginEvent::$account protected property The drupal user entity about to be logged in.
CasPreLoginEvent::$allowLogin protected property Controls whether or not the user will be allowed to login.
CasPreLoginEvent::$cancelLoginReason protected property The user message why logging-in has been canceled.
CasPreLoginEvent::$casPropertyBag protected property Store the CAS property bag.
CasPreLoginEvent::allowLogin public function Allows the login operation.
CasPreLoginEvent::cancelLogin public function Cancels the login operation.
CasPreLoginEvent::getAccount public function Return the user account entity.
CasPreLoginEvent::getAllowLogin public function Return if this user is allowed to login.
CasPreLoginEvent::getCancelLoginReason public function Returns a user message explaining why the login process is asked to cancel.
CasPreLoginEvent::getCasPropertyBag public function CasPropertyBag getter.
CasPreLoginEvent::setAllowLogin Deprecated public function Set the $allowLogin property.
CasPreLoginEvent::__construct public function Constructor.