You are here

public function CasAttributesSubscriber::onPreRegister in CAS Attributes 8

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

Subscribe to the CasPreRegisterEvent.

Parameters

\Drupal\cas\Event\CasPreRegisterEvent $event: The CasPreAuthEvent containing property information.

File

src/Subscriber/CasAttributesSubscriber.php, line 73

Class

CasAttributesSubscriber
Provides a CasAttributesSubscriber.

Namespace

Drupal\cas_attributes\Subscriber

Code

public function onPreRegister(CasPreRegisterEvent $event) {
  if ($this->settings
    ->get('field.sync_frequency') !== CasAttributesSettings::SYNC_FREQUENCY_NEVER) {

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

    // Map roles.
    $roleMappingResults = $this
      ->doRoleMapCheck($event
      ->getCasPropertyBag()
      ->getAttributes());
    if (empty($roleMappingResults['add']) && $this->settings
      ->get('role.deny_registration_no_match')) {
      $event
        ->setAllowAutomaticRegistration(FALSE);
    }
    else {

      // Add/remove roles from the user.
      $existingProperties = $event
        ->getPropertyValues();
      $rolesForUser = [];
      if (!empty($existingProperties['roles'])) {
        $rolesForUser = $existingProperties['roles'];
      }
      $rolesForUser = array_diff($rolesForUser, $roleMappingResults['remove']);
      $rolesForUser = array_merge($rolesForUser, $roleMappingResults['add']);
      $rolesForUser = array_unique($rolesForUser);
      $event
        ->setPropertyValue('roles', $rolesForUser);
    }
  }
}