You are here

public function CasAttributesSubscriber::onPreLogin in CAS Attributes 8

Same name and namespace in other branches
  1. 2.x src/Subscriber/CasAttributesSubscriber.php \Drupal\cas_attributes\Subscriber\CasAttributesSubscriber::onPreLogin()

Subscribe to the CasPreLoginEvent.

Parameters

\Drupal\cas\Event\CasPreLoginEvent $event: The CasPreAuthEvent containing account and property information.

File

src/Subscriber/CasAttributesSubscriber.php, line 111

Class

CasAttributesSubscriber
Provides a CasAttributesSubscriber.

Namespace

Drupal\cas_attributes\Subscriber

Code

public function onPreLogin(CasPreLoginEvent $event) {
  $account = $event
    ->getAccount();

  // Map fields.
  if ($this->settings
    ->get('field.sync_frequency') === CasAttributesSettings::SYNC_FREQUENCY_EVERY_LOGIN) {
    $field_mappings = $this
      ->getFieldMappings($event
      ->getCasPropertyBag()
      ->getAttributes());
    if (!empty($field_mappings)) {

      // If field already has data, only set new value if configured to
      // overwrite existing data.
      $overwrite = $this->settings
        ->get('field.overwrite');
      foreach ($field_mappings as $field_name => $field_value) {
        if ($overwrite || empty($account
          ->get($field_name))) {
          $account
            ->set($field_name, $field_value);
        }
      }
    }
  }

  // Map roles.
  $roleMappingResults = $this
    ->doRoleMapCheck($event
    ->getCasPropertyBag()
    ->getAttributes());
  if ($this->settings
    ->get('role.sync_frequency') === CasAttributesSettings::SYNC_FREQUENCY_EVERY_LOGIN) {
    foreach ($roleMappingResults['remove'] as $rid) {
      $account
        ->removeRole($rid);
    }
    foreach ($roleMappingResults['add'] as $rid) {
      $account
        ->addRole($rid);
    }
  }
  if (empty($roleMappingResults['add']) && $this->settings
    ->get('role.deny_login_no_match')) {
    $event
      ->cancelLogin();
  }
}