public function SimpleFbConnectUserManager::createUser in Simple FB Connect 8.3
Same name and namespace in other branches
- 8.2 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.
int $fbid: User's Facebook ID.
\Facebook\GraphNodes\GraphNode $fb_profile_pic: GraphNode object representing user's Facebook profile picture.
Return value
\Drupal\user\Entity\User|false Drupal user account if user was created False otherwise
File
- src/
SimpleFbConnectUserManager.php, line 116
Class
- SimpleFbConnectUserManager
- Contains all logic that is related to Drupal user management.
Namespace
Drupal\simple_fb_connectCode
public function createUser($name, $email, $fbid, GraphNode $fb_profile_pic) {
// Make sure we have everything we need.
if (!$name || !$email || !$fb_profile_pic) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Failed to create user. Name: @name, email: @email', [
'@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.', [
'@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.
// There are three different language fields.
// - preferred_language
// - preferred_admin_langcode
// - langcode of the user entity i.e. the language of the profile fields
// - We use the same logic as core and populate the current UI language to
// all of these. Other modules can subscribe to the triggered event and
// change the languages if they will.
// Get the current UI language.
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$fields = [
'name' => $this
->generateUniqueUsername($name),
'mail' => $email,
'init' => $email,
'pass' => $this
->userPassword(32),
'status' => $this
->getNewUserStatus(),
'langcode' => $langcode,
'preferred_langcode' => $langcode,
'preferred_admin_langcode' => $langcode,
];
// Check if user's picture should be downloaded from FB. We don't download
// the default silhouette unless Drupal user picture is a required field.
$file = FALSE;
$is_silhouette = (bool) $fb_profile_pic
->getField('is_silhouette');
if ($this
->userPictureEnabled() && ($this
->userPictureRequired() || !$is_silhouette)) {
$file = $this
->downloadProfilePic($fb_profile_pic
->getField('url'), $fbid);
if (!$file) {
$this->loggerFactory
->get('simple_fb_connect')
->error('Failed to create user. Profile picture could not be downloaded. Name: @name, email: @email', [
'@name' => $name,
'@email' => $email,
]);
$this
->drupalSetMessage($this
->t('Error while creating user account. Please contact site administrator.'), 'error');
return FALSE;
}
$file
->save();
$fields['user_picture'] = $file
->id();
}
// Create new user account.
$new_user = $this->entityTypeManager
->getStorage('user')
->create($fields);
// 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,
'fbid' => $fbid,
]);
$this->eventDispatcher
->dispatch('simple_fb_connect.user_created', $event);
// Validate the new user.
$violations = $new_user
->validate();
if (count($violations) > 0) {
$property = $violations[0]
->getPropertyPath();
$msg = $violations[0]
->getMessage();
$this
->drupalSetMessage($this
->t('Error while creating user account. Please contact site administrator.'), 'error');
$this->loggerFactory
->get('simple_fb_connect')
->error('Could not create new user, validation failed. Property: @property. Message: @message', [
'@property' => $property,
'@message' => $msg,
]);
return FALSE;
}
// 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', [
'@username' => $new_user
->getAccountName(),
'@uid' => $new_user
->id(),
]);
$this
->drupalSetMessage($this
->t('New user account %username created.', [
'%username' => $new_user
->getAccountName(),
]));
// Set the owner of the profile picture file if it was downloaded.
if ($file) {
$file
->setOwner($new_user);
$file
->save();
}
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', [
'@message' => $ex
->getMessage(),
]);
}
return FALSE;
}