View source
<?php
namespace Drupal\samlauth_user_fields\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Url;
use Drupal\samlauth\Controller\SamlController;
use Drupal\samlauth_user_fields\EventSubscriber\UserFieldsEventSubscriber;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SamlauthMappingListForm extends ConfigFormBase {
protected $entityFieldManager;
public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($config_factory);
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_field.manager'));
}
protected function getEditableConfigNames() {
return [];
}
public function getFormId() {
return 'samlauth_user_fields_edit_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $mapping_id = NULL) {
$config = $this
->configFactory()
->get(UserFieldsEventSubscriber::CONFIG_OBJECT_NAME);
$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'),
];
}
return parent::buildForm($form, $form_state);
}
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);
}
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');
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;
}
}