Commit 748c3845 authored by kiz_0987's avatar kiz_0987

#68096 - Major change to user sync function to make it more robust. Also, will...

#68096 - Major change to user sync function to make it more robust. Also, will now allow for cases when there are users already defined in Gallery2 (links the G2 user with the Drupal equivalent). Sync still only operates when a user is modified or created.
parent 9028ba95
......@@ -5,7 +5,12 @@
* gallery.module : gallery_user.inc
* User Modification Functions (create, update, delete...)
*/
define("G2_USER_EXISTS", 1);
define("G2_USER_EXISTS_BUT_NEEDS_MAPPING", 2);
define("G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED", 3);
define("G2_USER_DOES_NOT_EXIST", 4);
$path = drupal_get_path('module', 'gallery');
require_once($path . '/gallery_roles.inc');
......@@ -104,40 +109,61 @@ function gallery_modify_user($user, $action = 'create') {
$pass = ($user->status == 1) ? $user->pass : user_password(20);
switch ($action) {
case 'create' :
$ret = GalleryEmbed::createUser($user->uid,
array('username' => $user->name,
'email' => $user->mail,
'fullname' => $fullname,
'language' => gallery_get_language($user),
'hashedpassword' => $pass,
'hashmethod' => 'md5'));
case 'create' :
case 'update' :
// See if user already exists in Gallery2
list ($g2_user_state, $g2_user, $ret) = _gallery_check_user_status($user);
if ($ret) {
// An unmasked error, so exit now
return array(false, $ret);
}
// Add group info
gallery_sync_groups_for_user($user);
break;
case 'update' :
$ret = GalleryEmbed::updateUser($user->uid,
array('username' => $user->name,
'fullname' => $fullname,
'email' => $user->mail,
'language' => gallery_get_language($user),
'hashedpassword' => $pass,
'hashmethod' => 'md5'));
}
switch ($g2_user_state) {
case G2_USER_EXISTS_BUT_NEEDS_MAPPING:
// No mapping found, so add one
$ret = GalleryEmbed::addExternalIdMapEntry($user->uid, $g2_user->getId(), 'GalleryUser');
if ($ret) {
// mapping the user failed for some reason, so exit
return array(false, $ret);
}
// Continue to update
case G2_USER_EXISTS:
// May need to update the user info with that from Drupal
$ret = GalleryEmbed::updateUser($user->uid,
array('username' => $user->name,
'fullname' => $fullname,
'email' => $user->mail,
'language' => gallery_get_language($user),
'hashedpassword' => $pass,
'hashmethod' => 'md5'));
if ($ret) {
return array(false, $ret);
}
break;
case G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED:
$ret = GalleryCoreApi::removeMapEntry('ExternalIdMap', array('externalId' => $user->uid, 'entityType' => 'GalleryUser'));
if ($ret) {
// There was an error on removeMapEntry
return array(false, $ret2);
}
// Continue to creation
case G2_USER_DOES_NOT_EXIST:
// Create the new user
$ret = GalleryEmbed::createUser($user->uid,
array('username' => $user->name,
'email' => $user->mail,
'fullname' => $fullname,
'language' => gallery_get_language($user),
'hashedpassword' => $pass,
'hashmethod' => 'md5'));
if ($ret) {
// There was an error on user creation
return array(false, $ret);
}
break;
}
if ($ret) {
// try to create user then.
$ret = GalleryEmbed::createUser($user->uid,
array('username' => $user->name,
'fullname' => $fullname,
'email' => $user->mail,
'language' => gallery_get_language($user),
'hashedpassword' => $pass,
'hashmethod' => 'md5'));
if ($ret) {
return array(false, $ret);
}
return array(false, $ret);
}
// Add group info
gallery_sync_groups_for_user($user);
......@@ -146,6 +172,61 @@ function gallery_modify_user($user, $action = 'create') {
return array(true, null);
}
function _gallery_create_user_if_necessary($user) {
}
function _gallery_check_user_status($user) {
// See if user already exists in Gallery2
list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($user->name);
if (!$ret) {
// The user is in Gallery2, so map the user if needed
$ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
if ($ret2) {
if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
return array(G2_USER_EXISTS_BUT_NEEDS_MAPPING, $g2_user, null);
} else {
// Some other error, so exit
return array(null, $g2_user, $ret2);
}
} else {
return array(G2_USER_EXISTS, $g2_user, null);
}
} elseif ($ret->getErrorCode() & ERROR_MISSING_OBJECT) {
// The user does not yet exist in G2
// First, check if the extID was mapped (it should not be)
$ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
if ($ret2) {
if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
// This should be missing
return array(G2_USER_DOES_NOT_EXIST, $g2_user, null);
} else {
// Some other error, so exit
return array(null, $g2_user, $ret2);
}
} else {
// No error, so user is mapped
return array(G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED, $g2_user, null);
}
/* if (!$ret2) {
return array(G2_USER_DOES_NOT_EXIST, $g2_user, null);
} else {
if (!($ret2->getErrorCode() & ERROR_MISSING_OBJECT)) {
// There is a mapping for this user even though the user does not exist,
return array(G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED, $g2_user, null);
} else {
// Some other error, so exit
return array(null, $g2_user, $ret2);
}
}*/
} else {
// Some other error so exit
return array(null, $g2_user, $ret);
}
}
/* --------------------------------------------------------------------------
* User View Functions (view all users, view specific users,...)
* --------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment