5 * gallery.module : gallery_user.inc
6 * User Modification Functions (create, update, delete...)
9 define("G2_USER_EXISTS", 1);
10 define("G2_USER_EXISTS_BUT_NEEDS_MAPPING", 2);
11 define("G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED", 3);
12 define("G2_USER_DOES_NOT_EXIST", 4);
14 $path = drupal_get_path('module', 'gallery');
15 require_once($path .
'/gallery_roles.inc');
20 function gallery_insert_user(&$edit, $user) {
21 list ($success, $ret) = _gallery_init();
23 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
24 configure your embedded Gallery</a>.',
25 array('%link' => url('admin/settings/gallery')));
26 gallery_error($err_msg, $ret);
30 list ($success, $ret) = gallery_modify_user($user, 'create');
32 gallery_error(t('Error creating Gallery user'), $ret);
41 * Update a user with new information
43 function gallery_update_user(&$edit, $user) {
44 list ($success, $ret) = _gallery_init();
46 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
47 configure your embedded Gallery</a>.',
48 array('%link' => url('admin/settings/gallery')));
49 gallery_error($err_msg, $ret);
53 // on update we can't be sure how much info $edit will contain.
54 // $user is a copy, so we can modify it here.
55 $user->name
= ($edit['name']) ?
$edit['name'] : $user->name
;
56 $user->language
= ($edit['language']) ?
$edit['language'] : gallery_get_language($user);
57 $user->pass
= ($edit['pass']) ?
md5($edit['pass']) : $user->pass
;
58 $user->status
= ($edit['status']) ?
$edit['status'] : $user->status
;
59 $user->mail = ($edit['mail']) ?
$edit['mail'] : $user->mail;
60 // Note: $user->roles is organized as [$rid]=>[$role_name], but edit['roles'] is [$rid]=>[$position]
61 $user->roles
= ($edit['roles']) ?
$edit['roles'] : $user->roles
;
62 // Use full name from profile if it exists
63 $fullnamefield = variable_get('gallery_profile_full_name_field', 'profile_full_name');
64 $usefullname = variable_get('gallery_use_full_name', 0) && module_exist('profile');
65 if (($edit[$fullnamefield] || $user->$fullnamefield) && $usefullname) {
66 $user->$fullnamefield = ($edit[$fullnamefield]) ?
$edit[$fullnamefield] : $user->$fullnamefield;
68 $user->$fullnamefield = $name;
71 list ($success, $ret) = gallery_modify_user($user, 'update');
73 gallery_error(t('Error updating Gallery user'), $ret);
81 * Delete the user from the Gallery
83 function gallery_delete_user($user) {
84 list ($success, $ret) = _gallery_init();
86 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
87 configure your embedded Gallery</a>.',
88 array('%link' => url('admin/settings/gallery')));
89 gallery_error($err_msg, $ret);
92 $ret = GalleryEmbed
::deleteUser($user->uid
);
94 gallery_error(t('Error deleting Gallery user'), $ret);
101 * Modify (create/update) a user
103 function gallery_modify_user($user, $action = 'create') {
104 $fullnamefield = variable_get('gallery_profile_full_name_field', 'profile_full_name');
105 $usefullname = variable_get('gallery_use_full_name', 0) && module_exist('profile');
106 $fullname = ($user->$fullnamefield && $usefullname) ?
$user->$fullnamefield : $user->name
;
107 // Generate random password for gallery2 if user is blocked, to avoid them being able to login
108 // to gallery2 if not-embedded operation is allowed.
109 $pass = ($user->status
== 1) ?
$user->pass
: user_password(20);
114 // See if user already exists in Gallery2
115 list ($g2_user_state, $g2_user, $ret) = _gallery_check_user_status($user);
117 // An unmasked error, so exit now
118 return array(false
, $ret);
121 switch ($g2_user_state) {
122 case G2_USER_EXISTS_BUT_NEEDS_MAPPING
:
123 // No mapping found, so add one
124 $ret = GalleryEmbed
::addExternalIdMapEntry($user->uid
, $g2_user->getId(), 'GalleryUser');
126 // mapping the user failed for some reason, so exit
127 return array(false
, $ret);
129 // Continue to update
131 // May need to update the user info with that from Drupal
132 $ret = GalleryEmbed
::updateUser($user->uid
,
133 array('username' => $user->name
,
134 'fullname' => $fullname,
135 'email' => $user->mail,
136 'language' => gallery_get_language($user),
137 'hashedpassword' => $pass,
138 'hashmethod' => 'md5'));
140 return array(false
, $ret);
143 case G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED
:
144 $ret = GalleryCoreApi
::removeMapEntry('ExternalIdMap', array('externalId' => $user->uid
, 'entityType' => 'GalleryUser'));
146 // There was an error on removeMapEntry
147 return array(false
, $ret2);
149 // Continue to creation
150 case G2_USER_DOES_NOT_EXIST
:
151 // Create the new user
152 $ret = GalleryEmbed
::createUser($user->uid
,
153 array('username' => $user->name
,
154 'email' => $user->mail,
155 'fullname' => $fullname,
156 'language' => gallery_get_language($user),
157 'hashedpassword' => $pass,
158 'hashmethod' => 'md5'));
160 // There was an error on user creation
161 return array(false
, $ret);
166 return array(false
, $ret);
169 gallery_sync_groups_for_user($user);
172 return array(true
, null
);
176 function _gallery_create_user_if_necessary($user) {
180 function _gallery_check_user_status($user) {
181 // See if user already exists in Gallery2
182 list ($ret, $g2_user) = GalleryCoreApi
::fetchUserByUsername($user->name
);
184 // The user is in Gallery2, so map the user if needed
185 $ret2 = GalleryEmbed
::isExternalIdMapped($user->uid
, 'GalleryUser');
187 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT
) {
188 return array(G2_USER_EXISTS_BUT_NEEDS_MAPPING
, $g2_user, null
);
190 // Some other error, so exit
191 return array(null
, $g2_user, $ret2);
194 return array(G2_USER_EXISTS
, $g2_user, null
);
196 } elseif ($ret->getErrorCode() & ERROR_MISSING_OBJECT
) {
197 // The user does not yet exist in G2
198 // First, check if the extID was mapped (it should not be)
199 $ret2 = GalleryEmbed
::isExternalIdMapped($user->uid
, 'GalleryUser');
201 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT
) {
202 // This should be missing
203 return array(G2_USER_DOES_NOT_EXIST
, $g2_user, null
);
205 // Some other error, so exit
206 return array(null
, $g2_user, $ret2);
209 // No error, so user is mapped
210 return array(G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED
, $g2_user, null
);
214 return array(G2_USER_DOES_NOT_EXIST, $g2_user, null);
216 if (!($ret2->getErrorCode() & ERROR_MISSING_OBJECT)) {
217 // There is a mapping for this user even though the user does not exist,
218 return array(G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED, $g2_user, null);
220 // Some other error, so exit
221 return array(null, $g2_user, $ret2);
225 // Some other error so exit
226 return array(null
, $g2_user, $ret);
230 /* --------------------------------------------------------------------------
231 * User View Functions (view all users, view specific users,...)
232 * --------------------------------------------------------------------------
236 * View Gallery user details for a specific user
238 function gallery_view_user($user) {
239 $g2_userinfo = gallery_user_info($user, true
);
241 list ($success, $ret) = _gallery_init();
243 gallery_error(t('Unable to log in to Gallery'), $ret);
247 list ($ret, $g2user) = GalleryCoreApi
::loadEntityByExternalId($user->uid
, 'GalleryUser');
249 if (!($ret->getErrorCode() & ERROR_MISSING_OBJECT
)) {
250 gallery_error(t('Unable to load the Gallery user'), $ret);
256 list ($ret, $albumId) =
257 GalleryCoreApi
::getPluginParameter('module', 'useralbum', 'albumId', $g2user->getId());
259 gallery_error(t('Unable to fetch the user album id'), $ret);
264 if (!empty($albumId)) {
266 $urlGenerator =& $gallery->getUrlGenerator();
267 $link = $urlGenerator->generateUrl(
268 array('view' => 'core.ShowItem',
269 'itemId' => $albumId),
270 array('forceFullUrl' => 1));
272 $form['gallery_view_user_album'] = array(
273 'value' => l(t('User Album'), $link),
274 'class' => 'send-message');
276 $form['gallery_view_user_album'] = array(
277 'value' => t('User has not created an album yet'),
278 'class' => 'send-message');
281 if (($g2_userinfo['error_msg']) && (user_access('administer users'))) {
282 $form['gallery_view_user'] = array(
283 'title' => t('Gallery2-Drupal Sync Status'),
284 'value' => implode(',<br />', $g2_userinfo['error_msg']) .
'<br />');
288 return array(t('Gallery2') => $form);
293 * Gallery Users Page - view a set of users
295 function _gallery_users() {
296 // user.module and userlist.module inspired code with some G2 stuff from Mambo embed
298 array('data' => t('ID'), 'field' => 'u.uid', 'sort' => 'asc'),
299 array('data' => t('G2ID')),
300 array('data' => t('Username'), 'field' => 'u.name'),
301 array('data' => t('Status'), 'field' => 'u.status'),
302 array('data' => t('Sync Status')),
306 $sql = 'SELECT u.uid, u.name, u.status, u.mail, u.pass FROM {users} u WHERE uid != 0';
307 $sql .
= tablesort_sql($header);
308 $result = pager_query($sql, 50);
310 $status = array(t('blocked'), t('active'));
311 $destination = drupal_get_destination();
313 list ($success, $ret) = _gallery_init(true
);
315 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
316 configure your embedded Gallery</a>.',
317 array('%link' => url('admin/settings/gallery')));
318 gallery_error($err_msg, $ret);
321 $urlGenerator =& $GLOBALS['gallery']->getUrlGenerator();
322 while ($account = db_fetch_object($result)) {
323 // Check if user exists in G2 database and check its info
324 $g2_userinfo = gallery_user_info($account);
326 $link_url = $urlGenerator->generateUrl(array('view' => 'core.SiteAdmin',
327 'subView' => 'core.AdminEditUser',
328 'userId' => $g2_userinfo['g2_id']));
329 $link_url = '<a href="' .
$link_url .
'">' .
t('edit G2') .
'</a>';
331 $g2_edituserlink = ($g2_userinfo['g2_id']>=0) ?
$link_url : t('N/A');
332 $g2_id = ($g2_userinfo['g2_id']>=0) ?
$g2_userinfo['g2_id'] : t('N/A');
334 $rows[] = array($account->uid
, $g2_id, theme_username($account),
335 $status[$account->status
], implode(',<br />', $g2_userinfo['error_msg']),
336 l(t('edit'), "user/$account->uid/edit", array(), $destination),
340 $pager = theme('pager', NULL
, 50, 0);
341 if (!empty($pager)) {
342 $rows[] = array(array('data' => $pager, 'colspan' => '5'));
344 $output = theme('table', $header, $rows);
345 $output .
= theme('pager', array(), 100);
346 GalleryEmbed
::done();
351 * Helper to get Gallery2 user info
353 function gallery_user_info($user, $full = false
) {
354 list ($success, $ret) = _gallery_init(true
);
356 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
357 configure your embedded Gallery</a>.',
358 array('%link' => url('admin/settings/gallery')));
359 gallery_error($err_msg, $ret);
362 $g2_userinfo['error_msg'] = array();
363 $ret = GalleryEmbed
::isExternalIdMapped($user->uid
, 'GalleryUser');
364 if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT
)) {
365 $g2_userinfo['g2_id'] = -1;
366 $g2_userinfo['error_msg'][] = t('Missing from G2');
367 $g2_userinfo['sync_ok'] = 0;
370 // There is an ExternalId, so load the info
371 list($ret, $g2_user) = GalleryCoreApi
::loadEntityByExternalId($user->uid
, 'GalleryUser');
372 // In some cases the ExternalId may be present, but the user may have been deleted
374 $g2_userinfo['g2_id'] = -1;
375 $g2_userinfo['error_msg'][] = t('Missing from G2');
376 $g2_userinfo['sync_ok'] = 0;
379 // Go through a number of fields in both users and check for differences
380 // The G2 object seems to have changed from $g2_user->_id to $g2_user->id
381 $g2_userinfo['g2_id'] = $g2_user->id
;
382 if ($g2_user->getuserName() != $user->name
){
383 $g2_userinfo['error_msg'][] = t('Different Usernames');
385 $usefullname = variable_get('gallery_use_full_name', 0) && module_exist('profile');
387 $fullnamefield = variable_get('gallery_profile_full_name_field', 'profile_full_name');
388 $fullnameresult = 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
, $fullnamefield);
389 $fullname = db_fetch_object($fullnameresult);
390 $fullname = $fullname->value
;
391 $msg = t('Drupal Full Name: "') .
$fullname .
t('" G2 Full Name: "') .
$g2_user->getfullName().
'"';
393 if ($g2_user->getfullName() != $fullname) {
394 $g2_userinfo['error_msg'][] = t('Different Full Names');
395 } else if (!$fullname) {
396 // If usefullname is turned on, but the field is not completed, this will occur.
397 $g2_userinfo['error_msg'][] = t('Full Name missing');
400 if ($g2_user->getfullName() != $user->name
) {
401 $g2_userinfo['error_msg'][] = t('G2 Full Name incorrect');
404 if ($g2_user->getemail() != $user->mail) {
405 $g2_userinfo['error_msg'][] = t('Different E-Mails'); //FIX
407 if ($g2_user->gethashedPassword() != $user->pass
) {
408 $g2_userinfo['error_msg'][] = ($user->status
) ?
t('Different Passwords') : t('Blocked User');
411 $g2_userinfo['sync_ok'] = (!$g2_userinfo['error_msg']);
412 if ($g2_userinfo['sync_ok']) {
413 $g2_userinfo['error_msg'][] = t('OK');
415 // Get full info if needed
419 list($ret, $all_itemids) = GalleryCoreApi
::fetchAllItemIdsByOwnerId($g2_userinfo['g2_id']);
420 $g2_userinfo['count_total'] = count($all_itemids);
421 $g2_userinfo['count_album'] = 0;
422 $g2_userinfo['album_id'] = array();
423 // Get all albums for this user
424 list($ret, $all_albumids) = GalleryCoreApi
::fetchAllItemIds('GalleryAlbumItem');
425 foreach ($all_itemids as
$id => $name) {
426 if (in_array($name, $all_albumids)) {
427 $g2_userinfo['count_album']++;
428 $g2_userinfo['album_id'][] = $name;