You are here

class SocialAuthSettingsForm in Social Auth 8.2

Same name and namespace in other branches
  1. 8 src/Form/SocialAuthSettingsForm.php \Drupal\social_auth\Form\SocialAuthSettingsForm
  2. 3.x src/Form/SocialAuthSettingsForm.php \Drupal\social_auth\Form\SocialAuthSettingsForm

Defines a form that configures Social Auth settings.

Hierarchy

Expanded class hierarchy of SocialAuthSettingsForm

File

src/Form/SocialAuthSettingsForm.php, line 15

Namespace

Drupal\social_auth\Form
View source
class SocialAuthSettingsForm extends ConfigFormBase {

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   Used to check if route exists.
   */
  public function __construct(ConfigFactoryInterface $config_factory, RouteProviderInterface $route_provider) {
    parent::__construct($config_factory);
    $this->routeProvider = $route_provider;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $social_auth_config = $this
      ->config('social_auth.settings');
    $form['social_auth'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Social Auth Settings'),
      '#open' => TRUE,
      '#description' => $this
        ->t('These settings allow you to configure how Social Auth module behaves on your Drupal site'),
    ];
    $form['social_auth']['post_login'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this
        ->t('Post login path'),
      '#description' => $this
        ->t('Path where the user should be redirected after a successful login. It must begin with <em>/, #</em> or <em>?</em>.'),
      '#default_value' => $social_auth_config
        ->get('post_login'),
    ];
    $form['social_auth']['user_allowed'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('What can users do?'),
      '#default_value' => $social_auth_config
        ->get('user_allowed'),
      '#options' => [
        'register' => $this
          ->t('Register and login'),
        'login' => $this
          ->t('Login only'),
      ],
    ];
    $form['social_auth']['redirect_user_form'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Redirect new users to Drupal user form'),
      '#description' => $this
        ->t('If you check this, new users are redirected to Drupal user form after the user is created. This is useful if you want to encourage users to fill in additional user fields.'),
      '#default_value' => $social_auth_config
        ->get('redirect_user_form'),
    ];
    $form['social_auth']['disable_admin_login'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Disable Social Auth login for administrator'),
      '#description' => $this
        ->t('Disabling Social Auth login for administrator (<em>user 1</em>) can help protect your site if a security vulnerability is ever discovered in some Social Network PHP SDK or this module.'),
      '#default_value' => $social_auth_config
        ->get('disable_admin_login'),
    ];

    // Option to disable Social Auth for specific roles.
    $roles = user_roles();
    $options = [];
    foreach ($roles as $key => $role_object) {
      if ($key != 'anonymous' && $key != 'authenticated') {
        $options[$key] = Html::escape($role_object
          ->get('label'));
      }
    }
    $form['social_auth']['disabled_roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Disable Social Auth login for the following roles'),
      '#options' => $options,
      '#default_value' => $social_auth_config
        ->get('disabled_roles'),
    ];
    if (empty($roles)) {
      $form['social_auth']['disabled_roles']['#description'] = $this
        ->t('No roles found.');
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $post_login = $values['post_login'];

    // If it is not a valid path.
    if (!in_array($post_login[0], [
      "/",
      "#",
      "?",
    ])) {
      $form_state
        ->setErrorByName('post_login', $this
        ->t('The path is not valid. It must begin with <em>/, #</em> or <em>?</em>'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this
      ->config('social_auth.settings')
      ->set('post_login', $values['post_login'])
      ->set('user_allowed', $values['user_allowed'])
      ->set('redirect_user_form', $values['redirect_user_form'])
      ->set('disable_admin_login', $values['disable_admin_login'])
      ->set('disabled_roles', $values['disabled_roles'])
      ->save();
    parent::submitForm($form, $form_state);
  }

}

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.
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.
SocialAuthSettingsForm::$routeProvider protected property The route provider.
SocialAuthSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SocialAuthSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SocialAuthSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SocialAuthSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SocialAuthSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SocialAuthSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SocialAuthSettingsForm::__construct public function 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.