You are here

public function SimpleFbConnectUserManager::createUser in Simple FB Connect 8.2

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

Create a new user account.

Parameters

string $name: User's name on Facebook.

string $email: User's email address.

Return value

\Drupal\user\Entity\User|false Drupal user account if user was created False otherwise

File

src/SimpleFbConnectUserManager.php, line 93
Contains \Drupal\simple_fb_connect\SimpleFbConnectUserManager.

Class

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

Namespace

Drupal\simple_fb_connect

Code

public function createUser($name, $email) {

  // Make sure we have everyting we need.
  if (!$name || !$email) {
    $this->loggerFactory
      ->get('simple_fb_connect')
      ->error('Failed to create user. Name: @name, email: @email', array(
      '@name' => $name,
      '@email' => $email,
    ));
    $this
      ->drupalSetMessage($this
      ->t('Error while creating user account. Please contact site administrator.'), 'error');
    return FALSE;
  }

  // Check if site configuration allows new users to register.
  if ($this
    ->registrationBlocked()) {
    $this->loggerFactory
      ->get('simple_fb_connect')
      ->warning('Failed to create user. User registration is disabled in Drupal account settings. Name: @name, email: @email.', array(
      '@name' => $name,
      '@email' => $email,
    ));
    $this
      ->drupalSetMessage($this
      ->t('Only existing users can log in with Facebook. Contact system administrator.'), 'error');
    return FALSE;
  }

  // Set up the user fields.
  // - Username will be user's name on Facebook.
  // - Password can be very long since the user doesn't see this.
  $fields = array(
    'name' => $this
      ->generateUniqueUsername($name),
    'mail' => $email,
    'init' => $email,
    'pass' => $this
      ->userPassword(32),
    'status' => $this
      ->getNewUserStatus(),
  );

  // Create new user account.
  $new_user = $this->entityTypeManager
    ->getStorage('user')
    ->create($fields);

  // Try to save the new user account.
  try {
    $new_user
      ->save();
    $this->loggerFactory
      ->get('simple_fb_connect')
      ->notice('New user created. Username @username, UID: @uid', array(
      '@username' => $new_user
        ->getAccountName(),
      '@uid' => $new_user
        ->id(),
    ));

    // Dispatch an event so that other modules can react to the user creation.
    // Set the account twice on the event: as the main subject but also in the
    // list of arguments.
    $event = new GenericEvent($new_user, [
      'account' => $new_user,
    ]);
    $this->eventDispatcher
      ->dispatch('simple_fb_connect.user_created', $event);
    return $new_user;
  } catch (EntityStorageException $ex) {
    $this
      ->drupalSetMessage($this
      ->t('Creation of user account failed. Please contact site administrator.'), 'error');
    $this->loggerFactory
      ->get('simple_fb_connect')
      ->error('Could not create new user. Exception: @message', array(
      '@message' => $ex
        ->getMessage(),
    ));
  }
  return FALSE;
}