diff --git a/config/install/social_auth.settings.yml b/config/install/social_auth.settings.yml index 378d77a..ff8b006 100644 --- a/config/install/social_auth.settings.yml +++ b/config/install/social_auth.settings.yml @@ -1 +1,5 @@ auth: [] +post_login_path: user +redirect_user_form: 0 +disable_admin_login: 1 +disabled_roles: [] diff --git a/config/schema/social_auth.schema.yml b/config/schema/social_auth.schema.yml new file mode 100644 index 0000000..97d6cf7 --- /dev/null +++ b/config/schema/social_auth.schema.yml @@ -0,0 +1,16 @@ +social_auth.settings: + type: config_object + label: 'Social Auth Config' + mapping: + post_login_path: + type: string + label: 'Drupal path where the user should be redirected after successful login.' + redirect_user_form: + type: integer + label: 'Redirects to Drupal user form after the user is created if checked.' + disable_admin_login: + type: integer + label: 'Allows to Disable Social Auth login for administrator' + disabled_roles: + type: array + label: 'Roles for which Social Auth Login is disabled' diff --git a/social_auth.module b/social_auth.module index 192f606..3eae4f5 100644 --- a/social_auth.module +++ b/social_auth.module @@ -22,3 +22,15 @@ function social_auth_theme() { function social_auth_preprocess_login_with(&$variables) { $variables['base_path'] = base_path(); } + +/** + * Implements hook_update_N(). + */ +function social_auth_update_8001(&$sandbox) { + $config = \Drupal::service('config.factory')->getEditable('social_auth.settings'); + // Set and save new message value. + $config->set('post_login_path', 'user')->save(); + $config->set('redirect_user_form', 0)->save(); + $config->set('disable_admin_login', 1)->save(); + $config->set('disabled_roles', array())->save(); +} diff --git a/src/Form/SocialAuthSettingsForm.php b/src/Form/SocialAuthSettingsForm.php new file mode 100644 index 0000000..7cfae81 --- /dev/null +++ b/src/Form/SocialAuthSettingsForm.php @@ -0,0 +1,102 @@ +config('social_auth.settings'); + + $form['social_auth'] = array( + '#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_path'] = array( + '#type' => 'textfield', + '#required' => TRUE, + '#title' => $this->t('Post login path'), + '#description' => $this->t('Drupal path where the user should be redirected after successful login. Use <front> to redirect user to your front page. Leave it empty to set the path to page where the process started.'), + '#default_value' => $social_auth_config->get('post_login_path'), + ); + + $form['social_auth']['redirect_user_form'] = array( + '#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'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Disable Social Auth login for administrator'), + '#description' => $this->t('Disabling Social Auth login for administrator (user 1) 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 = array(); + foreach ($roles as $key => $role_object) { + if ($key != 'anonymous' && $key != 'authenticated') { + $options[$key] = Html::escape($role_object->get('label')); + } + } + + $form['social_auth']['disabled_roles'] = array( + '#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'] = t('No roles found.'); + } + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValues(); + $this->config('social_auth.settings') + ->set('post_login_path', $values['post_login_path']) + ->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); + } + +}