You are here

BakerySettingsForm.php in Bakery Single Sign-On System 8.2

File

src/Forms/BakerySettingsForm.php
View source
<?php

namespace Drupal\bakery\Forms;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configure bakery settings for this site.
 */
class BakerySettingsForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'bakery.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('bakery.settings');
    $form['bakery_is_master'] = [
      '#type' => 'checkbox',
      '#title' => 'Is this the master site?',
      '#default_value' => $config
        ->get('bakery_is_master'),
      '#description' => $this
        ->t('On the master site, accounts need to be created by traditional processes, i.e by a user registering or an admin creating them.'),
    ];
    $form['subsite_login'] = [
      '#type' => 'radios',
      '#title' => 'Subsite log in & registration',
      '#options' => [
        0 => $this
          ->t('Only log in & register on master site'),
        1 => $this
          ->t('Allow log in & register on any site (deprecated)'),
      ],
      '#default_value' => (int) $config
        ->get('subsite_login'),
      '#description' => $this
        ->t('Limiting log ins and registration to the master site gives users a consistent experience and reduces the surface area available to attackers.'),
    ];
    $form['bakery_master'] = [
      '#type' => 'textfield',
      '#title' => 'Master site',
      '#default_value' => $config
        ->get('bakery_master'),
      '#description' => $this
        ->t('Specify the master site for your bakery network.'),
    ];
    $form['bakery_slaves'] = [
      '#type' => 'textarea',
      '#title' => 'Slave sites',
      '#default_value' => implode("\n", $config
        ->get('bakery_slaves') ?: []),
      '#description' => $this
        ->t('Specify any slave sites in your bakery network that you want to update if a user changes email or username on the master. Enter one site per line, in the form "http://sub.example.com/".'),
    ];
    $form['bakery_help_text'] = [
      '#type' => 'textarea',
      '#title' => 'Help text for users with synch problems.',
      '#default_value' => $config
        ->get('bakery_help_text'),
      '#description' => $this
        ->t('This message will be shown to users if/when they have problems synching their accounts. It is an alternative to the "self repair" option and can be blank.'),
    ];
    $form['bakery_freshness'] = [
      '#type' => 'textfield',
      '#title' => 'Seconds of age before a cookie is old',
      '#default_value' => $config
        ->get('bakery_freshness'),
    ];
    $form['bakery_key'] = [
      '#type' => 'textfield',
      '#title' => 'Private key for cookie validation',
      '#default_value' => $config
        ->get('bakery_key'),
    ];
    $form['bakery_domain'] = [
      '#type' => 'textfield',
      '#title' => 'Cookie domain',
      '#default_value' => $config
        ->get('bakery_domain'),
    ];
    $default = $config
      ->get('bakery_supported_fields');
    $default['mail'] = 'mail';
    $default['name'] = 'name';
    $options = [
      'name' => $this
        ->t('username'),
      'mail' => $this
        ->t('e-mail'),
      'status' => $this
        ->t('status'),
      'picture' => $this
        ->t('user picture'),
      'language' => $this
        ->t('language'),
      'signature' => $this
        ->t('signature'),
    ];

    // TODO: need to add profile fileds

    /*
        if (module_exists('profile')) {
        $result = db_query('SELECT name, title FROM {profile_field}
        ORDER BY category, weight');
        foreach ($result as $field) {
        $options[$field->name] = check_plain($field->title);
        }
        }
    */
    $form['bakery_supported_fields'] = [
      '#type' => 'checkboxes',
      '#title' => 'Supported profile fields',
      '#default_value' => $default,
      '#options' => $options,
      '#description' => $this
        ->t('Choose the profile fields that should be exported by the master and imported on the slaves. Username and E-mail are always exported. The correct export of individual fields may depend on the appropriate settings for other modules on both master and slaves. You need to configure this setting on both the master and the slaves.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('bakery.settings');
    $config
      ->set('bakery_is_master', $form_state
      ->getValue('bakery_is_master'))
      ->set('bakery_master', trim($form_state
      ->getValue('bakery_master'), '/') . '/')
      ->set('subsite_login', (bool) $form_state
      ->getValue('subsite_login'))
      ->set('bakery_help_text', $form_state
      ->getValue('bakery_help_text'))
      ->set('bakery_freshness', $form_state
      ->getValue('bakery_freshness'))
      ->set('bakery_key', $form_state
      ->getValue('bakery_key'))
      ->set('bakery_domain', $form_state
      ->getValue('bakery_domain'))
      ->set('bakery_supported_fields', $form_state
      ->getValue('bakery_supported_fields'));
    if ($form_state
      ->getValue('bakery_slaves') && !empty($form_state
      ->getValue('bakery_slaves'))) {

      // Transform the text string into an array.
      $slaves = explode("\n", trim(str_replace("\r", '', $form_state
        ->getValue('bakery_slaves'))));

      // For each entry, remove the trailing slash
      // (if present) and concatenate with a new trailing slash.
      foreach ($slaves as &$slave) {
        $slave = trim($slave, '/') . '/';
      }
      $config
        ->set('bakery_slaves', $slaves);
    }
    else {
      $config
        ->set('bakery_slaves', []);
    }
    $config
      ->save();

    // Trigger some rebuilds that might be required by main/child changes.
    \Drupal::service('router.builder')
      ->setRebuildNeeded();
    parent::submitForm($form, $form_state);
  }

}

Classes

Namesort descending Description
BakerySettingsForm Configure bakery settings for this site.