Fixed capitalization. Now displays gallery user page.
[project/gallery.git] / gallery_user.inc
CommitLineData
c15e9178 1<?php
2// $Id$
3
4/**
5 * gallery.module : gallery_user.inc
6 * User Modification Functions (create, update, delete...)
7 */
748c3845 8
9define("G2_USER_EXISTS", 1);
10define("G2_USER_EXISTS_BUT_NEEDS_MAPPING", 2);
11define("G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED", 3);
12define("G2_USER_DOES_NOT_EXIST", 4);
13
c15e9178 14$path = drupal_get_path('module', 'gallery');
15require_once($path . '/gallery_roles.inc');
16
17/**
18 * Insert new user
19 */
20function gallery_insert_user(&$edit, $user) {
21 list ($success, $ret) = _gallery_init();
22 if (!$success) {
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);
27 return;
28 }
29
30 list ($success, $ret) = gallery_modify_user($user, 'create');
31 if ($ret) {
32 gallery_error(t('Error creating Gallery user'), $ret);
33 return;
34 }
35
36 GalleryEmbed::done();
37 return;
38}
39
40/**
41 * Update a user with new information
42 */
43function gallery_update_user(&$edit, $user) {
44 list ($success, $ret) = _gallery_init();
45 if (!$success) {
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);
50 return;
51 }
52
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;
f829e0d6 59 $user->mail = ($edit['mail']) ? $edit['mail'] : $user->mail;
c15e9178 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');
037e15be 64 $usefullname = variable_get('gallery_use_full_name', 0) && module_exists('profile');
c15e9178 65 if (($edit[$fullnamefield] || $user->$fullnamefield) && $usefullname) {
66 $user->$fullnamefield = ($edit[$fullnamefield]) ? $edit[$fullnamefield] : $user->$fullnamefield;
67 } else {
68 $user->$fullnamefield = $name;
69 }
70
71 list ($success, $ret) = gallery_modify_user($user, 'update');
72 if ($ret) {
73 gallery_error(t('Error updating Gallery user'), $ret);
74 return;
75 }
76 GalleryEmbed::done();
77 return;
78}
79
80/**
81 * Delete the user from the Gallery
82 */
83function gallery_delete_user($user) {
84 list ($success, $ret) = _gallery_init();
85 if (!$success) {
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);
90 return;
91 }
92 $ret = GalleryEmbed::deleteUser($user->uid);
93 if ($ret) {
94 gallery_error(t('Error deleting Gallery user'), $ret);
95 }
96 GalleryEmbed::done();
97 return;
98}
99
100/**
101 * Modify (create/update) a user
102 */
103function gallery_modify_user($user, $action = 'create') {
104 $fullnamefield = variable_get('gallery_profile_full_name_field', 'profile_full_name');
037e15be 105 $usefullname = variable_get('gallery_use_full_name', 0) && module_exists('profile');
c15e9178 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);
110
111 switch ($action) {
748c3845 112 case 'create' :
113 case 'update' :
114 // See if user already exists in Gallery2
115 list ($g2_user_state, $g2_user, $ret) = _gallery_check_user_status($user);
c15e9178 116 if ($ret) {
748c3845 117 // An unmasked error, so exit now
c15e9178 118 return array(false, $ret);
748c3845 119 }
120
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');
125 if ($ret) {
126 // mapping the user failed for some reason, so exit
127 return array(false, $ret);
128 }
129 // Continue to update
130 case G2_USER_EXISTS:
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'));
139 if ($ret) {
140 return array(false, $ret);
141 }
142 break;
143 case G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED:
144 $ret = GalleryCoreApi::removeMapEntry('ExternalIdMap', array('externalId' => $user->uid, 'entityType' => 'GalleryUser'));
145 if ($ret) {
146 // There was an error on removeMapEntry
147 return array(false, $ret2);
148 }
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'));
159 if ($ret) {
160 // There was an error on user creation
161 return array(false, $ret);
162 }
163 break;
164 }
c15e9178 165 if ($ret) {
748c3845 166 return array(false, $ret);
c15e9178 167 }
168 // Add group info
169 gallery_sync_groups_for_user($user);
170 break;
171 }
172 return array(true, null);
173}
174
748c3845 175
176function _gallery_create_user_if_necessary($user) {
177
178}
179
180function _gallery_check_user_status($user) {
181 // See if user already exists in Gallery2
182 list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($user->name);
183 if (!$ret) {
184 // The user is in Gallery2, so map the user if needed
185 $ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
186 if ($ret2) {
187 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
188 return array(G2_USER_EXISTS_BUT_NEEDS_MAPPING, $g2_user, null);
189 } else {
190 // Some other error, so exit
191 return array(null, $g2_user, $ret2);
192 }
193 } else {
194 return array(G2_USER_EXISTS, $g2_user, null);
195 }
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');
200 if ($ret2) {
201 if ($ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
202 // This should be missing
203 return array(G2_USER_DOES_NOT_EXIST, $g2_user, null);
204 } else {
205 // Some other error, so exit
206 return array(null, $g2_user, $ret2);
207 }
208 } else {
209 // No error, so user is mapped
210 return array(G2_USER_DOES_NOT_EXIST_BUT_IS_MAPPED, $g2_user, null);
211 }
212
213 /* if (!$ret2) {
214 return array(G2_USER_DOES_NOT_EXIST, $g2_user, null);
215 } else {
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);
219 } else {
220 // Some other error, so exit
221 return array(null, $g2_user, $ret2);
222 }
223 }*/
224 } else {
225 // Some other error so exit
226 return array(null, $g2_user, $ret);
227 }
228}
229
c15e9178 230/* --------------------------------------------------------------------------
231 * User View Functions (view all users, view specific users,...)
232 * --------------------------------------------------------------------------
233 */
234
235/**
236 * View Gallery user details for a specific user
237 */
238function gallery_view_user($user) {
239 $g2_userinfo = gallery_user_info($user, true);
a2e83850 240
241 list ($success, $ret) = _gallery_init();
242 if (!$success) {
243 gallery_error(t('Unable to log in to Gallery'), $ret);
244 return;
245 }
246
247 list ($ret, $g2user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser');
248 if ($ret) {
249 if (!($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
250 gallery_error(t('Unable to load the Gallery user'), $ret);
251 return;
252 }
253 }
254
255 if ($g2user) {
256 list ($ret, $albumId) =
257 GalleryCoreApi::getPluginParameter('module', 'useralbum', 'albumId', $g2user->getId());
258 if ($ret) {
259 gallery_error(t('Unable to fetch the user album id'), $ret);
260 return;
261 }
262 }
263
264 if (!empty($albumId)) {
265 global $gallery;
266 $urlGenerator =& $gallery->getUrlGenerator();
267 $link = $urlGenerator->generateUrl(
268 array('view' => 'core.ShowItem',
269 'itemId' => $albumId),
270 array('forceFullUrl' => 1));
271
272 $form['gallery_view_user_album'] = array(
273 'value' => l(t('User Album'), $link),
274 'class' => 'send-message');
275 } else {
276 $form['gallery_view_user_album'] = array(
277 'value' => t('User has not created an album yet'),
278 'class' => 'send-message');
279 }
280
30da76b6 281 if (($g2_userinfo['error_msg']) && (user_access('administer users'))) {
c15e9178 282 $form['gallery_view_user'] = array(
283 'title' => t('Gallery2-Drupal Sync Status'),
284 'value' => implode(',<br />', $g2_userinfo['error_msg']) . '<br />');
a2e83850 285 }
286
287 if (!empty($form)) {
c15e9178 288 return array(t('Gallery2') => $form);
289 }
290}
291
292/**
293 * Gallery Users Page - view a set of users
294 */
295function _gallery_users() {
296// user.module and userlist.module inspired code with some G2 stuff from Mambo embed
297 $header = array(
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')),
303 t('Operations'),
304 t('G2 Operations')
305 );
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);
309
310 $status = array(t('blocked'), t('active'));
311 $destination = drupal_get_destination();
312
313 list ($success, $ret) = _gallery_init(true);
314 if (!$success) {
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);
319 return;
320 }
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);
325
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>';
330
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');
333
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),
337 $g2_edituserlink);
338 }
339
43f9c3c1 340 $pager = theme('pager', NULL, 50, 0);
c15e9178 341 if (!empty($pager)) {
342 $rows[] = array(array('data' => $pager, 'colspan' => '5'));
343 }
344 $output = theme('table', $header, $rows);
345 $output .= theme('pager', array(), 100);
346 GalleryEmbed::done();
347 return $output;
348}
349
350/**
351 * Helper to get Gallery2 user info
352 */
353function gallery_user_info($user, $full = false) {
354 list ($success, $ret) = _gallery_init(true);
355 if (!$success) {
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);
360 return;
361 }
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;
368 return $g2_userinfo;
369 }
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
373 if ($ret) {
374 $g2_userinfo['g2_id'] = -1;
375 $g2_userinfo['error_msg'][] = t('Missing from G2');
376 $g2_userinfo['sync_ok'] = 0;
377 return $g2_userinfo;
378 }
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');
384 }
037e15be 385 $usefullname = variable_get('gallery_use_full_name', 0) && module_exists('profile');
c15e9178 386 if ($usefullname) {
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().'"';
392
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');
398 }
399 } else {
400 if ($g2_user->getfullName() != $user->name) {
401 $g2_userinfo['error_msg'][] = t('G2 Full Name incorrect');
402 }
403 }
404 if ($g2_user->getemail() != $user->mail) {
405 $g2_userinfo['error_msg'][] = t('Different E-Mails'); //FIX
406 }
407 if ($g2_user->gethashedPassword() != $user->pass) {
408 $g2_userinfo['error_msg'][] = ($user->status) ? t('Different Passwords') : t('Blocked User');
409 }
410
411 $g2_userinfo['sync_ok'] = (!$g2_userinfo['error_msg']);
412 if ($g2_userinfo['sync_ok']) {
413 $g2_userinfo['error_msg'][] = t('OK');
414 }
415 // Get full info if needed
416 if (!$full) {
417 return $g2_userinfo;
418 }
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;
429 }
430 }
431 return $g2_userinfo;
432}
037e15be 433?>