View source
<?php
namespace Drupal\simple_ldap_user\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_ldap\SimpleLdapServer;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\simple_ldap\SimpleLdapServerSchema;
use Drupal\Component\Utility\Unicode;
use Drupal\simple_ldap\SimpleLdapException;
use Drupal\Core\Url;
class SimpleLdapUserSettingsForm extends ConfigFormBase {
protected $server;
protected $schema;
public function __construct(ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
$this->server = \Drupal::service('simple_ldap.server');
}
public function getFormId() {
return 'simple_ldap_user_settings';
}
protected function getEditableConfigNames() {
return [
'simple_ldap.user',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('simple_ldap.user');
if ($this->server
->bind()) {
$this->schema = \Drupal::service('simple_ldap.schema');
$object_classes = $this
->getObjectClassOptions();
$schema_defaults = $this->schema
->getDefaultAttributeSettings();
$selected_object_classes = $config
->get('object_class') ? $config
->get('object_class') : $schema_defaults['object_class'];
$readonly = $this->server
->getServerType() == 'Active Directory' ? TRUE : FALSE;
if ($readonly) {
$this
->messenger()
->addWarning($this
->t('Your server is Active Directory, so some settings have been disabled.'));
}
$selected_object_classes = $form_state
->getValue('object_class') ? $form_state
->getValue('object_class') : $selected_object_classes;
$form['users'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('LDAP Users'),
'#open' => TRUE,
);
$form['users']['basedn'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Base DN'),
'#description' => $this
->t('The Base DN that will be searched for user accounts. Ex: dc=example,dc=com'),
'#required' => TRUE,
'#default_value' => $config
->get('basedn'),
);
$form['users']['user_scope'] = array(
'#type' => 'radios',
'#title' => $this
->t('Search scope'),
'#options' => array(
'sub' => $this
->t('Subtree') . ' - ' . t('Search the base DN and all of its children for user accounts.'),
'one' => $this
->t('One-level') . ' - ' . t('Do not include children of the base DN while searching for user accounts.'),
),
'#required' => TRUE,
'#default_value' => $config
->get('user_scope'),
);
$form['users']['object_class'] = array(
'#type' => 'select',
'#title' => $this
->t('User ObjectClass'),
'#options' => $object_classes,
'#default_value' => $selected_object_classes,
'#required' => TRUE,
'#multiple' => TRUE,
'#size' => 10,
'#description' => $this
->t('Which LDAP objectClass should be used when searching for a user. This also determines which attributes you have available to map below.'),
'#ajax' => array(
'callback' => array(
$this,
'populateObjectClassAttributes',
),
'wrapper' => 'user-attribute-mapping-wrapper',
),
'#disabled' => $readonly,
);
$attributes = $this
->getAttributeOptions($selected_object_classes);
$form['users']['attribute_mapping'] = array(
'#type' => 'container',
'#attributes' => array(
'id' => 'user-attribute-mapping-wrapper',
),
);
$form['users']['attribute_mapping']['name_attribute'] = array(
'#type' => 'select',
'#title' => t('Username attribute'),
'#options' => $attributes,
'#required' => TRUE,
'#description' => $this
->t('Which LDAP attribute should be mapped to a Drupal username. This is commonly "cn" or "uid".'),
'#default_value' => $config
->get('name_attribute') ? $config
->get('name_attribute') : $schema_defaults['name_attribute'],
'#disabled' => $readonly,
);
$form['users']['attribute_mapping']['mail_attribute'] = array(
'#type' => 'select',
'#title' => t('Mail attribute'),
'#options' => $attributes,
'#required' => TRUE,
'#description' => $this
->t('Which LDAP attribute should be mapped to a Drupal email. This is commonly "mail".'),
'#default_value' => $config
->get('mail_attribute') ? $config
->get('mail_attribute') : $schema_defaults['mail_attribute'],
'#disabled' => $readonly,
);
$frequencies = [
86400 => t('Every day'),
21600 => t('Every 6 hours'),
3600 => t('Every hour'),
0 => t('Every cron run'),
];
$form['users']['cron_frequency'] = array(
'#type' => 'select',
'#title' => t('Cron frequency'),
'#options' => $frequencies,
'#required' => TRUE,
'#description' => $this
->t('How frequently should active/blocked users be checked against the LDAP server? This value is only checked when cron runs, so the frequency is constrained by how frequently cron runs.'),
'#default_value' => $config
->get('cron_frequency') ? $config
->get('cron_frequency') : 0,
'#disabled' => $readonly,
);
}
else {
$this
->messenger()
->addWarning($this
->t('There is a problem with your LDAP Server connection. As a result, this form has been disabled. Please <a href="@url">check your settings</a>.', array(
'@url' => Url::fromRoute('simple_ldap.server')
->toString(),
)));
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->configFactory()
->getEditable('simple_ldap.user');
$config
->set('basedn', $form_state
->getValue('basedn'))
->set('user_scope', $form_state
->getValue('user_scope'))
->set('object_class', $form_state
->getValue('object_class'))
->set('name_attribute', $form_state
->getValue('name_attribute'))
->set('mail_attribute', $form_state
->getValue('mail_attribute'))
->set('cron_frequency', $form_state
->getValue('cron_frequency'))
->save();
parent::submitForm($form, $form_state);
}
private function getObjectClassOptions() {
$object_classes = $this->schema
->getSchemaItem('objectClasses');
foreach ($object_classes as $key => $object_class) {
$object_classes[$key] = $object_class['name'];
}
asort($object_classes);
return $object_classes;
}
private function getAttributeOptions($object_classes) {
$attributes = array();
foreach ($object_classes as $object_class) {
try {
$result = $this->schema
->getAttributesByObjectClass($object_class);
foreach ($result as $attribute) {
$attributes[mb_strtolower($attribute)] = $attribute;
}
} catch (SimpleLdapException $e) {
}
}
asort($attributes);
return $attributes;
}
public function populateObjectClassAttributes(array &$form, FormStateInterface $form_state) {
return array(
$form['users']['attribute_mapping'],
);
}
}