function simple_fb_connect_create_user in Simple FB Connect 7.2
Creates a new user account for a Facebook user.
Parameters
Facebook\GraphObject $fb_profile: Previously loaded FB user profile object.
Facebook\FacebookSession $fb_session: FacebookSession object so that we can load user profile pic.
Return value
Drupal user account. FALSE on errors.
1 call to simple_fb_connect_create_user()
- simple_fb_connect_return_from_fb in ./
simple_fb_connect.module - Page callback for /user/simple-fb-connect/return.
File
- ./
simple_fb_connect.module, line 674 - Simple Facebook Login Module for Drupal Sites.
Code
function simple_fb_connect_create_user(GraphObject $fb_profile, FacebookSession $fb_session) {
// Check if Drupal account settings allow users to register.
if (!variable_get('user_register', 1)) {
drupal_set_message(t('Creation of new accounts on this site is disabled.'), 'error');
watchdog('simple_fb_connect', 'Failed to create user. User registration is disabled in Drupal account settings.', array(), WATCHDOG_WARNING);
return FALSE;
}
// Check if module settings allow users to register.
if (variable_get('simple_fb_connect_login_only', 0)) {
drupal_set_message(t('Only registered users can log in with Facebook.'), 'error');
watchdog('simple_fb_connect', 'Failed to create user. User registration is disabled in Simple FB Connect settings.', array(), WATCHDOG_WARNING);
return FALSE;
}
if ($fb_profile && $fb_session) {
$real_name = $fb_profile
->getProperty('name');
$email = $fb_profile
->getProperty('email');
// Make sure username will be unique.
$drupal_username_generated = simple_fb_connect_unique_user_name($real_name);
if ($drupal_username_generated === FALSE) {
watchdog('simple_fb_connect', 'Could not create new user, username contains illegal characters.', array(), WATCHDOG_ERROR);
drupal_set_message(t("Username contains illegal characters."), 'error');
return FALSE;
}
// This will generate a random password. Since the user will never see this
// password, we can use long password to make it stronger.
$password = user_password(32);
// Set up the user fields.
$fields = array(
'name' => $drupal_username_generated,
'mail' => $email,
'pass' => $password,
'status' => variable_get('user_register', 1) == 1 ? 1 : 0,
'init' => $email,
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
);
// Set user language to current UI language.
if (drupal_multilingual()) {
global $language;
$fields['language'] = $language->language;
}
// Allow other modules to modify $fields array before the new user is saved.
drupal_alter("simple_fb_connect_register", $fields, $fb_profile);
// The first parameter is left blank so a new user is created.
$account = user_save('', $fields);
if ($account) {
// If user pictures are enabled, try to get the profile picture from FB.
if (variable_get('user_pictures', 0)) {
if ($fid = simple_fb_connect_get_fb_profile_pic($fb_session)) {
// Set owner of profile picture.
$file = file_load($fid);
$file->uid = $account->uid;
file_save($file);
// Add the file as user profile picture.
$edit = array(
'picture' => $file,
);
$account = user_save($account, $edit);
}
}
// Log new user creation.
watchdog('simple_fb_connect', 'New user created: @username', array(
'@username' => $drupal_username_generated,
), WATCHDOG_NOTICE);
drupal_set_message(t("New user account %username created.", array(
'%username' => $drupal_username_generated,
)));
// Invoke a registration event if Rules module is enabled.
if (module_exists('rules')) {
rules_invoke_event('simple_fb_connect_registration', $account);
}
// Invoke user creation event if some module implements hook_simple_fb_connect_registration.
module_invoke_all('simple_fb_connect_registration', $account);
return $account;
}
}
// Something went wrong.
drupal_set_message(t("Error while creating a new user account."), 'error');
watchdog('simple_fb_connect', 'Could not create new user.', array(), WATCHDOG_ERROR);
return FALSE;
}