You are here

class SamlauthMappingListForm in SAML Authentication 8.3

Same name and namespace in other branches
  1. 4.x modules/samlauth_user_fields/src/Form/SamlauthMappingListForm.php \Drupal\samlauth_user_fields\Form\SamlauthMappingListForm

Displays the list of attribute-field mappings; edits related configuration.

Hierarchy

Expanded class hierarchy of SamlauthMappingListForm

1 string reference to 'SamlauthMappingListForm'
samlauth_user_fields.routing.yml in modules/samlauth_user_fields/samlauth_user_fields.routing.yml
modules/samlauth_user_fields/samlauth_user_fields.routing.yml

File

modules/samlauth_user_fields/src/Form/SamlauthMappingListForm.php, line 18

Namespace

Drupal\samlauth_user_fields\Form
View source
class SamlauthMappingListForm extends ConfigFormBase {

  /**
   * The entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * SamlauthMappingListForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManagerInterface $entity_field_manager) {
    parent::__construct($config_factory);
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_field.manager'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {

    // I'm using ConfigFormBase for the unified save button / message, but
    // don't want to use ConfigFormBase::config(), to keep a unified way of
    // getting config values in forms / not obfuscate call structures and get
    // confused later. So this method/value is unneeded, but ConfigFormBase
    // requires it. Let's make it empty.
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'samlauth_user_fields_edit_form';
  }

  /**
   * Form for adding or editing a mapping.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param int $mapping_id
   *   (optional) The numeric ID of the mapping.
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $mapping_id = NULL) {
    $config = $this
      ->configFactory()
      ->get(UserFieldsEventSubscriber::CONFIG_OBJECT_NAME);

    // The bulk of this page is not a form at all, but a table. We're putting
    // that on the same page as the form options, because we have only two
    // checkboxes - which govern behavior related to the total of those table
    // rows. If this configuration form somehow grows, we'll split the table +
    // form off into separate pages/routes.
    $mappings = $config
      ->get('field_mappings');
    $form = $this
      ->listMappings(is_array($mappings) ? $mappings : []);
    if ($this
      ->configFactory()
      ->get(SamlController::CONFIG_OBJECT_NAME)
      ->get('map_users')) {
      $form['config'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Configuration for linking'),
      ];
      $form['config']['link_first_user'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Link first user if multiple found'),
        '#description' => $this
          ->t("If a link attempt matches multiple/'duplicate' users, link the first one and ignore the others. By default, login is denied and a Drupal administrator needs to decide what to do. (This never happens if matching is done on unique fields only, which is hopefully the case.)"),
        '#default_value' => $config
          ->get('link_first_user'),
      ];
      $form['config']['ignore_blocked'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Ignore blocked users'),
        '#description' => $this
          ->t("Never match/link blocked users. This may result in creating new users equal to a blocked user and granting them access - but enabling it (temporarily?) could help linking a correct user if 'duplicates' are matched. By default, if a blocked user is matched, it is linked then denied access."),
        '#default_value' => $config
          ->get('ignore_blocked'),
      ];
    }

    // @todo Do we also want a "Configuration for synchronization" section with
    //   one checkbox "Only take action on first login", like we have for roles?
    //   We also have separate checkboxes (but the inverse) for the name and
    //   email values. We could implement this option per field, but would that
    //   be overkill?
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->configFactory()
      ->getEditable(UserFieldsEventSubscriber::CONFIG_OBJECT_NAME)
      ->set('link_first_user', $form_state
      ->getValue('link_first_user'))
      ->set('ignore_blocked', $form_state
      ->getValue('ignore_blocked'))
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Returns the list of attribute-field mappings.
   *
   * @param array $mappings
   *   The attribute-field mappings.
   *
   * @return array
   *   A renderable content array.
   */
  public function listMappings(array $mappings) {
    $linking_enabled = $this
      ->configFactory()
      ->get(SamlController::CONFIG_OBJECT_NAME)
      ->get('map_users');
    $output['table'] = [
      '#theme' => 'table',
      '#header' => [
        $this
          ->t('SAML Attribute'),
        $this
          ->t('User Field'),
        $this
          ->t('Operations'),
      ],
      '#sticky' => TRUE,
      '#empty' => $this
        ->t("There are no mappings. You can add one using the link above."),
    ];
    if ($linking_enabled) {
      array_splice($output['table']['#header'], 2, 0, [
        $this
          ->t('Use for linking'),
      ]);
    }
    if ($mappings) {
      $fields = $this->entityFieldManager
        ->getFieldDefinitions('user', 'user');

      // We're identifying individual mappings by their numeric indexes in the
      // configuration value (which is defined as a 'sequence' in the config
      // schema). These are not renumbered while saving a mapping, so the
      // danger of using them is acceptable. (URLs would only pointing to a
      // different mapping if we delete the highest numbered mapping and re-add
      // one. Maybe things are renumbered arter exporting configuration, I
      // haven't tested, but that's also an acceptable risk.)
      foreach ($mappings as $id => $mapping) {
        $operations = [
          '#type' => 'dropbutton',
          '#links' => [
            'edit' => [
              'title' => $this
                ->t('edit'),
              'url' => Url::fromRoute('samlauth_user_fields.edit', [
                'mapping_id' => $id,
              ]),
            ],
            'delete' => [
              'title' => $this
                ->t('delete'),
              'url' => Url::fromRoute('samlauth_user_fields.delete', [
                'mapping_id' => $id,
              ]),
            ],
          ],
        ];
        $real_field_name = strstr($mapping['field_name'], ':', TRUE);
        if ($real_field_name) {
          $sub_field_name = substr($mapping['field_name'], strlen($real_field_name) + 1);
          if (isset($fields[$real_field_name])) {
            $property_definitions = $fields[$real_field_name]
              ->getFieldStorageDefinition()
              ->getPropertyDefinitions();
            if (isset($property_definitions[$sub_field_name]) && $property_definitions[$sub_field_name] instanceof DataDefinition) {
              $sub_field_name = $property_definitions[$sub_field_name]
                ->getLabel();
            }
          }
        }
        else {
          $real_field_name = $mapping['field_name'];
          $sub_field_name = '';
        }
        $user_field = (isset($fields[$real_field_name]) ? $fields[$real_field_name]
          ->getLabel() : $this
          ->t('Unknown field %name', [
          '%name' => $real_field_name,
        ])) . ($sub_field_name ? ": {$sub_field_name}" : '');
        $output['table']['#rows'][$id] = [
          $mapping['attribute_name'],
          $user_field,
          render($operations),
        ];
        if ($linking_enabled) {
          array_splice($output['table']['#rows'][$id], 2, 0, [
            $mapping['link_user_order'] ?? '',
          ]);
        }
      }
    }
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SamlauthMappingListForm::$entityFieldManager protected property The entity field manager service.
SamlauthMappingListForm::buildForm public function Form for adding or editing a mapping. Overrides ConfigFormBase::buildForm
SamlauthMappingListForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SamlauthMappingListForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SamlauthMappingListForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SamlauthMappingListForm::listMappings public function Returns the list of attribute-field mappings.
SamlauthMappingListForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SamlauthMappingListForm::__construct public function SamlauthMappingListForm constructor. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.