You are here

trait SettingsTrait in Social Auth 3.x

Same name and namespace in other branches
  1. 8.2 src/SettingsTrait.php \Drupal\social_auth\SettingsTrait

Helper methods for Social Auth and Drupal settings.

Hierarchy

2 files declare their use of SettingsTrait
UserAuthenticator.php in src/User/UserAuthenticator.php
UserManager.php in src/User/UserManager.php

File

src/SettingsTrait.php, line 12

Namespace

Drupal\social_auth
View source
trait SettingsTrait {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Used to check if route path exists.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The Social Auth data handler.
   *
   * @var \Drupal\social_auth\SocialAuthDataHandler
   */
  protected $dataHandler;

  /**
   * Checks if user registration is disabled.
   *
   * @return bool
   *   True if registration is disabled
   *   False if registration is not disabled
   */
  protected function isRegistrationDisabled() {

    // Check if Drupal account registration settings is Administrators only
    // OR if it is disabled in Social Auth Settings.
    return $this->configFactory
      ->get('user.settings')
      ->get('register') == 'admin_only' || $this->configFactory
      ->get('social_auth.settings')
      ->get('user_allowed') == 'login';
  }

  /**
   * Checks if admin approval is required for new users.
   *
   * @return bool
   *   True if approval is required
   *   False if approval is not required
   */
  protected function isApprovalRequired() {
    return $this->configFactory
      ->get('user.settings')
      ->get('register') == 'visitors_admin_approval';
  }

  /**
   * Checks if Admin (user 1) can login.
   *
   * @param \Drupal\user\UserInterface $drupal_user
   *   User object to check if user is admin.
   *
   * @return bool
   *   True if user 1 can't login.
   *   False otherwise
   */
  protected function isAdminDisabled(UserInterface $drupal_user) {
    return $this->configFactory
      ->get('social_auth.settings')
      ->get('disable_admin_login') && $drupal_user
      ->id() == 1;
  }

  /**
   * Checks if User with specific roles is allowed to login.
   *
   * @param \Drupal\user\UserInterface $drupal_user
   *   User object to check if user has a specific role.
   *
   * @return string|false
   *   The role that can't login.
   *   False if the user roles are not disabled.
   */
  protected function isUserRoleDisabled(UserInterface $drupal_user) {
    foreach ($this->configFactory
      ->get('social_auth.settings')
      ->get('disabled_roles') as $role) {
      if ($drupal_user
        ->hasRole($role)) {
        return $role;
      }
    }
    return FALSE;
  }

  /**
   * Checks if User should be redirected to User Form after creation.
   *
   * @param \Drupal\user\UserInterface $drupal_user
   *   User object to get the id of user.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse|false
   *   A redirect response to user form, if option is enabled.
   *   False otherwise
   */
  protected function redirectToUserForm(UserInterface $drupal_user) {
    if ($this->configFactory
      ->get('social_auth.settings')
      ->get('redirect_user_form')) {
      $redirection = Url::fromRoute('entity.user.edit_form', [
        'user' => $drupal_user
          ->id(),
      ]);
      return new RedirectResponse($redirection
        ->toString());
    }
    return FALSE;
  }

  /**
   * Returns the status for new users.
   *
   * @return int
   *   Value 0 means that new accounts remain blocked and require approval.
   *   Value 1 means that visitors can register new accounts without approval.
   */
  protected function getNewUserStatus() {
    $allowed = $this->configFactory
      ->get('user.settings')
      ->get('register');
    return (int) ($allowed === 'visitors');
  }

  /**
   * Returns the Post Login redirection.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Post Login Path to which the user would be redirected after login.
   */
  protected function getPostLoginRedirection() {

    // Gets destination parameter previously stored in session.
    $destination = $this->dataHandler
      ->get('login_destination');

    // If there was a destination parameter.
    if ($destination) {

      // Deletes the session key.
      $this->dataHandler
        ->set('login_destination', NULL);
      return new RedirectResponse(Url::fromUserInput($destination)
        ->toString());
    }
    $post_login = $this->configFactory
      ->get('social_auth.settings')
      ->get('post_login');
    return new RedirectResponse(Url::fromUserInput($post_login)
      ->toString());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SettingsTrait::$configFactory protected property The config factory.
SettingsTrait::$dataHandler protected property The Social Auth data handler.
SettingsTrait::$routeProvider protected property Used to check if route path exists.
SettingsTrait::getNewUserStatus protected function Returns the status for new users.
SettingsTrait::getPostLoginRedirection protected function Returns the Post Login redirection.
SettingsTrait::isAdminDisabled protected function Checks if Admin (user 1) can login.
SettingsTrait::isApprovalRequired protected function Checks if admin approval is required for new users.
SettingsTrait::isRegistrationDisabled protected function Checks if user registration is disabled.
SettingsTrait::isUserRoleDisabled protected function Checks if User with specific roles is allowed to login.
SettingsTrait::redirectToUserForm protected function Checks if User should be redirected to User Form after creation.