You are here

public function SimpleFbConnectUserManager::loginUser in Simple FB Connect 8.3

Same name and namespace in other branches
  1. 8.2 src/SimpleFbConnectUserManager.php \Drupal\simple_fb_connect\SimpleFbConnectUserManager::loginUser()

Logs the user in.

@todo Add Boost integtraion when Boost is available for D8 https://www.drupal.org/node/2524372

Parameters

\Drupal\user\Entity\User $drupal_user: User object.

Return value

bool True if login was successful False if the login was blocked

File

src/SimpleFbConnectUserManager.php, line 242

Class

SimpleFbConnectUserManager
Contains all logic that is related to Drupal user management.

Namespace

Drupal\simple_fb_connect

Code

public function loginUser(User $drupal_user) {

  // Prevent admin login if defined in module settings.
  if ($this
    ->loginDisabledForAdmin($drupal_user)) {
    $this
      ->drupalSetMessage($this
      ->t('Facebook login is disabled for site administrator. Login with your local user account.'), 'error');
    return FALSE;
  }

  // Prevent login if user has one of the roles defined in module settings.
  if ($this
    ->loginDisabledByRole($drupal_user)) {
    $this
      ->drupalSetMessage($this
      ->t('Facebook login is disabled for your role. Please login with your local user account.'), 'error');
    return FALSE;
  }

  // Check that the account is active and log the user in.
  if ($drupal_user
    ->isActive()) {
    $this
      ->userLoginFinalize($drupal_user);

    // Dispatch an event so that other modules can react to the user login.
    // Set the account twice on the event: as the main subject but also in the
    // list of arguments.
    $event = new GenericEvent($drupal_user, [
      'account' => $drupal_user,
    ]);
    $this->eventDispatcher
      ->dispatch('simple_fb_connect.user_login', $event);

    // TODO: Add Boost cookie if Boost module is enabled
    // https://www.drupal.org/node/2524372
    return TRUE;
  }

  // If we are still here, account is blocked.
  $this
    ->drupalSetMessage($this
    ->t('You could not be logged in because your user account %username is not active.', [
    '%username' => $drupal_user
      ->getAccountName(),
  ]), 'warning');
  $this->loggerFactory
    ->get('simple_fb_connect')
    ->warning('Facebook login for user @user prevented. Account is blocked.', [
    '@user' => $drupal_user
      ->getAccountName(),
  ]);
  return FALSE;
}