5 * gallery.module : gallery_user.inc
6 * User Functions (insert, update, delete, view, details, ...)
9 define(G2MAP_UNKNOWN
, 0);
10 define(G2MAP_USER_EXISTS
, 1);
11 define(G2MAP_USER_EXISTS_BUT_NEEDS_MAPPING
, 2);
12 define(G2MAP_USER_DOES_NOT_EXIST_BUT_IS_MAPPED
, 3);
13 define(G2MAP_USER_DOES_NOT_EXIST
, 4);
15 define(G2USERINFO_NOERROR
, 1);
16 define(G2USERINFO_ERROR
, 2);
17 define(G2USERINFO_ERROR_MISSING
, 3);
18 define(G2USERINFO_ERROR_USERNAME
, 4);
19 define(G2USERINFO_ERROR_FULLNAME
, 5);
20 define(G2USERINFO_ERROR_FULLNAME_MISSING
, 6);
21 define(G2USERINFO_ERROR_FULLNAME_INCORRECT
, 7);
22 define(G2USERINFO_ERROR_EMAIL
, 8);
23 define(G2USERINFO_ERROR_PASSWORD
, 9);
24 define(G2USERINFO_ERROR_GROUPS
, 10);
26 require_once(drupal_get_path('module', 'gallery') .
'/gallery_groups.inc');
31 function gallery_user_insert(&$edit, $user) {
32 $user->roles
= isset($edit['roles']) ?
$edit['roles'] : $user->roles
;
33 $user->status
= isset($edit['status']) ?
$edit['status'] : TRUE
;
35 gallery_user_modify($user, 'create');
39 * Update a user with new information
41 function gallery_user_update(&$edit, $user) {
42 $user->language
= isset($edit['language']) ?
$edit['language'] : gallery_get_language($user);
43 $user->pass
= !empty($edit['pass']) ?
md5($edit['pass']) : $user->pass
;
44 $user->status
= isset($edit['status']) ?
$edit['status'] : $user->status
;
45 $user->mail = !empty($edit['mail']) ?
$edit['mail'] : $user->mail;
46 $user->roles
= isset($edit['roles']) ?
$edit['roles'] : $user->roles
;
49 if (module_exists('profile') && variable_get('gallery_use_fullname', 0)) {
50 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
51 $user->$fullname_field = isset($edit[$fullname_field]) ?
$edit[$fullname_field] : $user->$fullname_field;
54 // Username is about to change
55 if ($namechange = (isset($edit['name']) && ($edit['name'] != $user->name
))) {
56 // Make sure the original user is up to date
57 gallery_user_modify($user, 'update', TRUE
);
60 $user->name
= isset($edit['name']) ?
$edit['name'] : $user->name
;
64 gallery_user_modify($user, 'username');
68 gallery_user_modify($user, 'update', TRUE
);
73 * Delete the user from the Gallery
75 function gallery_user_delete($user) {
76 gallery_user_modify($user, 'delete');
80 * Modify (create/update/delete) a user
82 function gallery_user_modify($user, $action = 'create', $groups = FALSE
, $vars = NULL
) {
83 if (!_gallery_init(TRUE
, $vars)) {
87 // Check for fullname support
88 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
89 $usefullname = module_exists('profile') && variable_get('gallery_use_fullname', 0);
90 $fullname = $usefullname ?
(isset($user->$fullname_field) ?
$user->$fullname_field : '') : $user->name
;
92 // Generate random password for G2 if user is blocked
93 $pass = ($user->status
) ?
$user->pass
: user_password(20);
97 $ret = GalleryEmbed
::updateUser($user->uid
,
98 array('username' => $user->name
,
99 'fullname' => $fullname,
100 'email' => $user->mail,
101 'language' => gallery_get_language($user),
102 'hashedpassword' => $pass,
103 'hashmethod' => 'md5'));
105 gallery_error(t('Error updating Gallery user (username changed)'), $ret);
111 // Get map state of the user
112 list($g2_user_state, $g2_user, $ret) = gallery_user_map_state($user);
114 gallery_error(t('Error determining user map state'), $ret);
118 // Complete user mapping
119 switch ($g2_user_state) {
120 case G2MAP_USER_EXISTS_BUT_NEEDS_MAPPING
:
121 // create map entry for the user
122 $ret = GalleryEmbed
::addExternalIdMapEntry($user->uid
, $g2_user->id
, 'GalleryUser');
124 gallery_error(t('Error creating map entry (ExternlIdMapEntry)'), $ret);
127 case G2MAP_USER_EXISTS
:
128 // Update user (Drupal -> G2)
129 $ret = GalleryEmbed
::updateUser($user->uid
,
130 array('username' => $user->name
,
131 'fullname' => $fullname,
132 'email' => $user->mail,
133 'language' => gallery_get_language($user),
134 'hashedpassword' => $pass,
135 'hashmethod' => 'md5'));
137 gallery_error(t('Error updating Gallery user'), $ret);
141 case G2MAP_USER_DOES_NOT_EXIST_BUT_IS_MAPPED
:
142 // Remove mapping for non-existing user
143 // (also happens if gallery_user_modify() is called with a changed username)
144 $ret = GalleryCoreApi
::removeMapEntry('ExternalIdMap', array('externalId' => $user->uid
, 'entityType' => 'GalleryUser'));
146 gallery_error(t('Error removing map entry (ExternlIdMapEntry)'), $ret);
149 case G2MAP_USER_DOES_NOT_EXIST
:
153 $ret = GalleryEmbed
::createUser($user->uid
,
154 array('username' => $user->name
,
155 'email' => $user->mail,
156 'fullname' => $fullname,
157 'language' => gallery_get_language($user),
158 'hashedpassword' => $pass,
159 'hashmethod' => 'md5'));
161 gallery_error(t('Error creating Gallery user'), $ret);
164 list($ret, $g2_user) = GalleryCoreApi
::loadEntityByExternalId($user->uid
, 'GalleryUser');
166 gallery_error(t('Error loading newly created Gallery user'), $ret);
172 _gallery_groups_user($user, $groups);
173 // Admin role mapping
174 $admin_role = variable_get('gallery_user_admin_role', 0);
175 if (($admin_role && in_array($admin_role, array_keys($user->roles
))) || ($user->uid
== 1)) {
176 // Get G2 admin group id
177 list($ret, $g2_admin_gid) = GalleryCoreApi
::getPluginParameter('module', 'core', 'id.adminGroup');
179 gallery_error(t('Error getting \'adminGroup\' id'), $ret);
182 // Add user to admin group
183 $ret = GalleryCoreApi
::addUserToGroup($g2_user->id
, $g2_admin_gid);
185 gallery_error(t('Error adding user to Gallery group (:gid)',
186 array(':gid' => $g2_admin_gid)), $ret);
192 $ret = GalleryEmbed
::deleteUser($user->uid
);
194 gallery_error(t('Error deleting Gallery user'), $ret);
200 // Set the 'locked' property for the user
201 if (variable_get('gallery_user_locked', 0) && ($action != 'delete') && ($user->uid
> 1)) {
202 list($ret, $g2_user) = GalleryCoreApi
::loadEntityByExternalId($user->uid
, 'GalleryUser');
204 gallery_error(t('Error loading Gallery user'), $ret);
207 if (!$g2_user->locked
) {
208 list($ret, $lock_id) = GalleryCoreApi
::acquireWriteLock($g2_user->id
);
210 gallery_error(t('Error acquiring write lock'), $ret);
213 $g2_user->setLocked(TRUE
);
214 if ($g2_user->save()) {
215 gallery_error(t('Locking user account failed'), $ret);
221 GalleryEmbed
::done();
226 * Sync user info for $uid
228 function _gallery_user_sync($uid) {
229 $user = user_load(array('uid' => $uid));
230 gallery_user_modify($user, 'update', TRUE
);
234 * View Gallery user details for a specific user
236 function gallery_user_view($user) {
237 if (variable_get('gallery_user_hide_profile', 0) || !_gallery_init(TRUE
)) {
241 if ($useralbum = gallery_user_useralbum($user->uid
)) {
242 $form['gallery_view_user_album'] = array(
243 'value' => l(t('User Album'), $useralbum),
244 'class' => 'send-message');
247 $form['gallery_view_user_album'] = array(
248 'value' => t('User has not created an album yet'),
249 'class' => 'send-message');
251 // Sync/Map status info
252 $g2_userinfo = gallery_user_map_info($user);
253 if (($g2_userinfo['status']) && (user_access('administer users'))) {
254 $form['gallery_view_user'] = array(
255 'title' => t('Gallery2-Drupal Sync Status'),
256 'value' => implode(',<br />', gallery_user_map_info_status($g2_userinfo['status']))
260 GalleryEmbed
::done();
262 return array(t('Gallery2') => $form);
267 * Create link to user album
269 function gallery_user_useralbum($uid = NULL
, $link = TRUE
) {
270 if (!_gallery_init(TRUE
)) {
275 list($ret, $g2_user) = GalleryCoreApi
::loadEntityByExternalId(isset($uid) ?
$uid : $user->uid
, 'GalleryUser');
277 if (!($ret->getErrorCode() & ERROR_MISSING_OBJECT
)) {
278 gallery_error(t('Error loading Gallery user'), $ret);
283 if ($g2_user && (gallery_single_plugin_status('useralbum') == GALLERY_PLUGIN_ENABLED
)) {
284 // Fetch user album id
285 list($ret, $album_id) = GalleryCoreApi
::getPluginParameter('module', 'useralbum', 'albumId', $g2_user->id
);
287 gallery_error(t('Error fetching user album id'), $ret);
290 // Generate link to user album
291 if (is_numeric($album_id) && $album_id > 0) {
292 return $link ?
gallery_generate_url(array('view' => 'core.ShowItem', 'itemId' => $album_id), FALSE
) : $album_id;
300 * Get info about user map status
302 function gallery_user_map_info($user, $noerror_status = TRUE
) {
303 $g2_userinfo = array('g2_id' => -1, 'status' => array());
305 $ret = GalleryEmbed
::isExternalIdMapped($user->uid
, 'GalleryUser');
306 if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT
)) {
307 $g2_userinfo['status'][] = G2USERINFO_ERROR_MISSING
;
310 // But user not available
311 list($ret, $g2_user) = GalleryCoreApi
::loadEntityByExternalId($user->uid
, 'GalleryUser');
313 $g2_userinfo['status'][] = G2USERINFO_ERROR_MISSING
;
317 $g2_userinfo['g2_id'] = $g2_user->id
;
318 if ($g2_user->userName
!= $user->name
) {
319 $g2_userinfo['status'][] = G2USERINFO_ERROR_USERNAME
;
322 if (module_exists('profile') && variable_get('gallery_use_fullname', 0)) {
323 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
324 $fullname_result = db_query("SELECT v.value FROM {profile_values} v INNER JOIN {profile_fields} f ON v.fid = f.fid AND v.uid = %d WHERE f.name = '%s'", $user->uid
, $fullname_field);
325 $fullname = db_fetch_object($fullname_result);
326 $fullname = $fullname->value
;
327 if ($g2_user->fullName
!= $fullname) {
328 $g2_userinfo['status'][] = G2USERINFO_ERROR_FULLNAME
;
330 elseif (!$fullname) {
331 $g2_userinfo['status'][] = G2USERINFO_ERROR_FULLNAME_MISSING
;
335 if ($g2_user->fullName
!= $user->name
) {
336 $g2_userinfo['status'][] = G2USERINFO_ERROR_FULLNAME_INCORRECT
;
340 if ($g2_user->email
!= $user->mail) {
341 $g2_userinfo['status'][] = G2USERINFO_ERROR_EMAIL
;
344 if ($user->status
&& ($g2_user->hashedPassword
!= $user->pass
)) {
345 $g2_userinfo['status'][] = G2USERINFO_ERROR_PASSWORD
;
348 if (!gallery_groups_map_info($g2_user, $user)) {
349 $g2_userinfo['status'][] = G2USERINFO_ERROR_GROUPS
;
351 // Overall sync status
352 if ($noerror_status && !count($g2_userinfo['status'])) {
353 $g2_userinfo['status'][] = G2USERINFO_NOERROR
;
360 * Get string representation of the use map status
362 function gallery_user_map_info_status($info = array(), $format = TRUE
) {
364 G2USERINFO_NOERROR
=> t('OK'),
365 G2USERINFO_ERROR
=> t('Any Error'),
366 G2USERINFO_ERROR_MISSING
=> t('Missing from G2'),
367 G2USERINFO_ERROR_USERNAME
=> t('Different Usernames'),
368 G2USERINFO_ERROR_FULLNAME
=> t('Different Full Names'),
369 G2USERINFO_ERROR_FULLNAME_MISSING
=> t('G2 Full Name missing'),
370 G2USERINFO_ERROR_FULLNAME_INCORRECT
=> t('G2 Full Name incorrect'),
371 G2USERINFO_ERROR_EMAIL
=> t('Different E-Mails'),
372 G2USERINFO_ERROR_PASSWORD
=> t('Different Passwords'),
373 G2USERINFO_ERROR_GROUPS
=> t('Roles <> Groups')
379 $info[] = G2USERINFO_NOERROR
;
381 foreach ($info as
$key => $value) {
382 $status[] = $info_map[$value];
391 * Get state of user mapping
393 function gallery_user_map_state($user) {
394 // See if user already exists in G2
395 list($ret, $g2_user) = GalleryCoreApi
::fetchUserByUsername($user->name
);
397 // User is in G2, so map the user if needed
398 $ret2 = GalleryEmbed
::isExternalIdMapped($user->uid
, 'GalleryUser');
400 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT
) {
401 return array(G2MAP_USER_EXISTS_BUT_NEEDS_MAPPING
, $g2_user, NULL
);
405 return array(G2MAP_UNKNOWN
, $g2_user, $ret2);
409 return array(G2MAP_USER_EXISTS
, $g2_user, NULL
);
412 elseif ($ret->getErrorCode() & ERROR_MISSING_OBJECT
) {
413 // User does not yet exist in G2
414 // Check if the externalID was mapped (it should not be)
415 $ret2 = GalleryEmbed
::isExternalIdMapped($user->uid
, 'GalleryUser');
417 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT
) {
418 return array(G2MAP_USER_DOES_NOT_EXIST
, $g2_user, NULL
);
422 return array(G2MAP_UNKNOWN
, $g2_user, $ret2);
427 return array(G2MAP_USER_DOES_NOT_EXIST_BUT_IS_MAPPED
, $g2_user, NULL
);
431 return array(NULL
, $g2_user, $ret);
436 * Import Gallery users into Drupal
438 function _gallery_user_import($g2_users = array()) {
441 list($ret, $guest) = GalleryCoreApi
::getPluginParameter('module', 'core', 'id.anonymousUser');
443 unset($g2_users[$guest]);
445 $g2_groups_map = _gallery_groups_map();
446 if (!($g2_extIdMap = _gallery_user_map(array_keys($g2_users)))) {
449 gallery_debug($g2_users, t('G2 Users'));
450 gallery_debug($g2_extIdMap, t('G2 ExternalIdMap'));
451 // Iterate over G2 users
452 foreach ($g2_users as
$g2_id => $g2_username) {
453 $new_user = !array_key_exists($g2_id, $g2_extIdMap);
454 list($ret, $g2_user) = GalleryCoreApi
::fetchUserByUsername($g2_username);
456 gallery_error(t('Error fetching user by username (:name)',
457 array(':name' => $g2_username)), $ret);
460 // Collect user details and validate the values (name and mail)
462 $values['name'] = $g2_user->userName
;
463 if ($error = user_validate_name($values['name'])) {
464 $errors[] = t('G2 User Import (uid: :uid, name: \':name\'): !error',
465 array(':uid' => $g2_id, ':name' => $values['name'], '!error' => $error));
468 $values['fullname'] = $g2_user->fullName
;
469 $values['pass'] = $g2_user->hashedPassword
;
470 $values['mail'] = $g2_user->email
;
471 if ($error = user_validate_mail($values['mail'])) {
472 $errors[] = t('G2 User Import (uid: :uid, mail: \':mail\'): !error',
473 array(':mail' => $values['mail'], ':uid' => $g2_id, '!error' => $error));
476 else if ($new_user && db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != 0 AND LOWER(mail) = LOWER('%s')", $values['mail'])) > 0) {
477 $errors[] = t('G2 User Import (uid: :uid): The e-mail address :mail is already registered.',
478 array(':uid' => $g2_id, ':mail' => $values['mail']));
481 $values['language'] = $g2_user->language
;
482 $values['created'] = $g2_user->creationTimestamp
;
483 $values['roles'] = array();
484 list($ret, $g2_groups) = GalleryCoreApi
::fetchGroupsForUser($g2_user->id
);
486 gallery_error(t('Error fetching groups for user (uid: :uid)',
487 array(':uid' => $g2_id)), $ret);
490 foreach ($g2_groups as
$g2_gid => $g2_groupname) {
491 if (isset($g2_groups_map[$g2_gid])) {
492 $values['roles'][$g2_groups_map[$g2_gid]] = $g2_groupname;
495 // Is the user blocked in G2
496 list($ret, $g2_user_blocked) = GalleryCoreApi
::isDisabledUsername($g2_username);
498 gallery_error(t('Error calling isDisabledUsername() for \':name\'',
499 array(':name' => $g2_username)), $ret);
502 $values['status'] = !$g2_user_blocked;
503 // Create new Drupal user (this will also override the G2 user
504 // during hook_user, but there is no 'clean' way to avoid this)
506 $values['notify'] = FALSE
;
507 if (!($user = user_save('', $values))) {
508 $errors[] = t('Error creating Drupal user for G2 user (uid: :uid)',
509 array(':uid' => $g2_id));
514 $user = new
stdClass();
515 $user->uid
= $g2_extIdMap[$g2_id];
517 // Override user details if requested (or for new users)
518 if ($user->uid
&& ($new_user || variable_get('gallery_user_import_override', 0))) {
520 if (module_exists('profile') && variable_get('gallery_use_fullname', 0)) {
521 $fullname_category = variable_get('gallery_profile_fullname_category', 'Personal Information');
522 $fullname_field = variable_get('gallery_profile_fullname_field', 'profile_fullname');
523 $values[$fullname_field] = $values['fullname'];
524 profile_save_profile($values, $user, $fullname_category);
526 // Override password directly (needed because G2 only provides the md5 hash)
527 _gallery_user_drupal_password($user->uid
, $values['pass']);
528 unset($values['pass']);
529 // Update Drupal user with G2 user details (invokes hook_user)
530 user_save(user_load(array('uid' => $user->uid
)), $values);
534 if (count($errors)) {
535 if (isset($_SESSION['gallery_user_progress_messages'])) {
536 $_SESSION['gallery_user_progress_messages'] += $errors;
539 drupal_set_message(theme('item_list', $errors, t('The following invalid user items were skipped:')), 'error');
547 * Override Drupal user password
549 function _gallery_user_drupal_password($uid, $pass) {
550 db_query("UPDATE {users} SET pass = '%s' WHERE uid = %d", $pass, $uid);
554 * Fetch 'GalleryUser' entries from G2 'ExternalIdMap'
555 * g2Id => externalId (default)
557 function _gallery_user_map($ids = array(), $inverse = FALSE
) {
558 $match = array('entityType' => 'GalleryUser');
559 if (count($ids) > 0) {
561 $match['externalId'] = $ids;
564 $match['entityId'] = $ids;
567 // Fetch the map entries
568 list($ret, $resultMap) = GalleryCoreApi
::getMapEntry('ExternalIdMap', array('externalId', 'entityId'), $match);
570 gallery_error(t('Error fetching \'GalleryUser\' entries from \'ExternalIdMap\''), $ret);
573 // Iterate over the results
574 $g2_extIdMap = array();
575 while (($row = $resultMap->nextResult()) !== FALSE
) {
576 $g2_extIdMap[($inverse ?
$row[0] : $row[1])] = ($inverse ?
$row[1] : $row[0]);
583 * Fetch all existing Drupal users (uid => username)
585 function _gallery_user_drupal_users() {
587 $result = db_query("SELECT uid, name FROM {users} WHERE uid > 0");
588 while ($user = db_fetch_object($result)) {
589 $users[$user->uid
] = $user->name
;