| Commit | Line | Data |
|---|---|---|
| e65698fe TW |
1 | <?php |
| 2 | // $Id$ | |
| 3 | ||
| 4 | /** | |
| 5 | * gallery.module : gallery_groups.inc | |
| 6 | * Group/Role Functions (sync groups, ...) | |
| 7 | */ | |
| 8 | ||
| 9 | /** | |
| 10 | * Function _gallery_groups_user(). | |
| 11 | * (sync Drupal roles and Gallery groups for a specific user) | |
| 12 | */ | |
| 13 | function _gallery_groups_user($user, $groups = FALSE) { | |
| 14 | // Sync the Drupal roles and Gallery groups | |
| 15 | if ($groups) { | |
| 16 | _gallery_groups_sync(); | |
| 17 | } | |
| 18 | ||
| 19 | // Get the Gallery groups for this user | |
| 20 | // First get the G2 Id from the Drupal uid | |
| 21 | list($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser'); | |
| 22 | if ($ret) { | |
| 23 | gallery_error(t('Error loading Gallery user from Drupal user id (:uid)', | |
| 24 | array(':uid' => $user->uid)), $ret); | |
| 25 | return; | |
| 26 | } | |
| 27 | // Then get the groups for this user currently set in G2 | |
| 28 | list($ret, $g2_user_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->id); | |
| 29 | if ($ret) { | |
| 30 | gallery_error(t('Error getting Gallery group info for user (:uid)', | |
| 31 | array(':uid' => $g2_user->id)), $ret); | |
| 32 | return; | |
| 33 | } | |
| 34 | gallery_debug($g2_user_groups, t('G2 groups for G2 user (uid: :g2uid)', array(':g2uid' => $g2_user->id))); | |
| 35 | ||
| 36 | // Convert the Drupal role Ids into Gallery Group Ids | |
| 37 | $user->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID; | |
| 38 | $user->roles[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID; | |
| 39 | gallery_debug($user->roles, t('Drupal roles for Drupal user (uid: :uid)', array(':uid' => $user->uid))); | |
| 40 | if (($g2_rid_map = _gallery_groups_map(array_keys($user->roles), TRUE)) === FALSE) { | |
| 41 | return; | |
| 42 | } | |
| 43 | gallery_debug($g2_rid_map, t('Drupal roles <> G2 groups map (for Drupal user)')); | |
| 44 | if (($g2_groups_map = array_flip(_gallery_groups_map(array_keys($g2_user_groups), FALSE))) === FALSE) { | |
| 45 | return; | |
| 46 | } | |
| 47 | gallery_debug($g2_groups_map, t('Drupal roles <> G2 groups map (for G2 user)')); | |
| 48 | ||
| 49 | // Find if the user needs to be deleted from any G2 groups (only mapped groups) | |
| 50 | $delete_list = array_diff($g2_groups_map, $g2_rid_map); | |
| 51 | gallery_debug($delete_list, t('Remove user from these groups')); | |
| 52 | foreach ($delete_list as $rid => $gid) { | |
| 53 | $ret = GalleryCoreApi::removeUserFromGroup($g2_user->id, $gid); | |
| 54 | if ($ret) { | |
| 55 | gallery_error(t('Error removing user from Gallery group (Gallery Group Id: :gid)', | |
| 56 | array(':gid' => $gid)), $ret); | |
| 57 | return; | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | // Find if the user needs to be added to any G2 groups | |
| 62 | $add_list = array_diff($g2_rid_map, $g2_groups_map); | |
| 63 | gallery_debug($add_list, t('Add user to these groups')); | |
| 64 | foreach ($add_list as $rid => $gid) { | |
| 65 | $ret = GalleryCoreApi::addUserToGroup($g2_user->id, $gid); | |
| 66 | if ($ret) { | |
| 67 | gallery_error(t('Error adding user to Gallery group (:gid)', | |
| 68 | array(':gid' => $gid)), $ret); | |
| 69 | return; | |
| 70 | } | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * Function _gallery_groups_sync(). | |
| 76 | * (sync Drupal roles and Gallery groups) | |
| 77 | */ | |
| 78 | function _gallery_groups_sync() { | |
| 79 | static $sync_groups = TRUE; | |
| 80 | // Sync groups only once | |
| 81 | if (!$sync_groups) { | |
| 82 | return; | |
| 83 | } | |
| 84 | $sync_groups = FALSE; | |
| 85 | // Check if the Drupal role <> G2 group mapping exists | |
| 86 | $roles = user_roles(); | |
| 87 | $admin_role = variable_get('gallery_user_admin_role', 0); | |
| 88 | foreach ($roles as $rid => $role_name) { | |
| 89 | // Add Drupal <> G2 mapping if needed | |
| 90 | $ret = GalleryEmbed::isExternalIdMapped($rid, 'GalleryGroup'); | |
| 91 | if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { | |
| 92 | switch ($rid) { | |
| 93 | case DRUPAL_ANONYMOUS_RID: | |
| 94 | list($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); | |
| 95 | if ($ret) { | |
| 96 | gallery_error(t('Error retrieving Gallery group Id for \'Everybody\' group'), $ret); | |
| 97 | return; | |
| 98 | } | |
| 99 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); | |
| 100 | if ($ret) { | |
| 101 | gallery_error(t('Error creating Drupal role <> Gallery group mapping for \'anonymous user\' role (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 102 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); | |
| 103 | return; | |
| 104 | } | |
| 105 | break; | |
| 106 | case DRUPAL_AUTHENTICATED_RID: | |
| 107 | list($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); | |
| 108 | if ($ret) { | |
| 109 | gallery_error(t('Error retrieving Gallery group Id for \'Registered Users\' group'), $ret); | |
| 110 | return; | |
| 111 | } | |
| 112 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); | |
| 113 | if ($ret) { | |
| 114 | gallery_error(t('Error creating Drupal role <> Gallery group mapping for \'authenticated user\' role (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 115 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); | |
| 116 | return; | |
| 117 | } | |
| 118 | break; | |
| 119 | default: | |
| 120 | // Special handling of the 'admin' role | |
| 121 | if ($rid == $admin_role) { | |
| 122 | // Get G2 admin group id | |
| 123 | list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); | |
| 124 | if ($ret) { | |
| 125 | gallery_error(t('Error getting \'adminGroup\' id'), $ret); | |
| 126 | return; | |
| 127 | } | |
| 128 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_admin_gid, 'GalleryGroup'); | |
| 129 | if ($ret) { | |
| 130 | gallery_error(t('Error creating Drupal role <> Gallery \'Site Admin\' group mapping (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 131 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); | |
| 132 | return; | |
| 133 | } | |
| 134 | } | |
| 135 | else { | |
| 136 | // Is there a group with this name already? | |
| 137 | list($ret, $g2_group) = GalleryCoreApi::fetchGroupByGroupName($role_name); | |
| 138 | if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { | |
| 139 | $ret = GalleryEmbed::createGroup($rid, $role_name); | |
| 140 | if ($ret) { | |
| 141 | gallery_error(t('Error creating Gallery group (Drupal Role Id: :rid, Drupal Role Name: :rname)', | |
| 142 | array(':rid' => $rid, ':rname' => $role_name)), $ret); | |
| 143 | return; | |
| 144 | } | |
| 145 | } | |
| 146 | else { | |
| 147 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_group->id, 'GalleryGroup'); | |
| 148 | if ($ret) { | |
| 149 | gallery_error(t('Error creating Drupal role <> Gallery group mapping (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 150 | array(':rid' => $rid, ':gid' => $g2_group->id)), $ret); | |
| 151 | return; | |
| 152 | } | |
| 153 | } | |
| 154 | } | |
| 155 | break; | |
| 156 | } | |
| 157 | } | |
| 158 | else { | |
| 159 | // Update group name if needed | |
| 160 | list($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup'); | |
| 161 | if ($ret) { | |
| 162 | gallery_error(t('Error retrieving Gallery Group Id from Drupal Role Id (Drupal Role Id: :rid)', | |
| 163 | array(':rid' => $rid)), $ret); | |
| 164 | return; | |
| 165 | } | |
| 166 | if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID, $admin_role)) && ($role_name != $g2_group->getGroupName())) { | |
| 167 | $ret = GalleryEmbed::updateGroup($rid, array('groupname' => $role_name)); | |
| 168 | if ($ret) { | |
| 169 | gallery_error(t('Error updating Gallery group (Drupal Role Id: :rid, Drupal Role Name: :rname)', | |
| 170 | array(':rid' => $rid, ':rname' => $role_name)), $ret); | |
| 171 | return; | |
| 172 | } | |
| 173 | } | |
| 174 | } | |
| 175 | } | |
| 176 | // Now check for any deleted Drupal roles. Only delete those G2 groups that were mapped to Drupal roles | |
| 177 | // (just in case other groups have been defined which are not meant to be sync'd with Drupal) | |
| 178 | if (($g2_groups_map = _gallery_groups_map(array(), TRUE)) === FALSE) { | |
| 179 | return; | |
| 180 | } | |
| 181 | foreach ($g2_groups_map as $rid => $g2_gid) { | |
| 182 | if (!isset($roles[$rid])) { | |
| 183 | $ret = GalleryEmbed::deleteGroup($rid); | |
| 184 | if ($ret) { | |
| 185 | gallery_error(t('Error deleting Gallery group (Gallery Group Id: :gid)', | |
| 186 | array(':gid' => $g2_gid)), $ret); | |
| 187 | return; | |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | } | |
| 192 | ||
| 193 | /** | |
| 194 | * Function _gallery_groups_map(). | |
| 195 | * (fetch 'GalleryGroup' entries from G2 'ExternalIdMap') | |
| 196 | */ | |
| 197 | function _gallery_groups_map($ids = array(), $inverse = FALSE) { | |
| 198 | // g2Id => externalId (default) | |
| 199 | $match = array('entityType' => 'GalleryGroup'); | |
| 200 | if (count($ids) > 0) { | |
| 201 | if ($inverse) { | |
| 202 | $match['externalId'] = $ids; | |
| 203 | } | |
| 204 | else { | |
| 205 | $match['entityId'] = $ids; | |
| 206 | } | |
| 207 | } | |
| 208 | // Fetch the map entries | |
| 209 | list($ret, $resultMap) = GalleryCoreApi::getMapEntry('ExternalIdMap', array('externalId', 'entityId'), $match); | |
| 210 | if ($ret) { | |
| 211 | gallery_error(t('Error fetching \'GalleryGroup\' entries from \'ExternalIdMap\''), $ret); | |
| 212 | return FALSE; | |
| 213 | } | |
| 214 | // Iterate over the results | |
| 215 | $g2_extIdMap = array(); | |
| 216 | while (($row = $resultMap->nextResult()) !== FALSE) { | |
| 217 | $g2_extIdMap[($inverse ? $row[0] : $row[1])] = ($inverse ? $row[1] : $row[0]); | |
| 218 | } | |
| 219 | ||
| 220 | return $g2_extIdMap; | |
| 221 | } | |
| 222 | ||
| 223 | /** | |
| 224 | * Function gallery_groups_map_info(). | |
| 225 | * (get info about groups map status) | |
| 226 | */ | |
| 227 | function gallery_groups_map_info($g2_user, $user) { | |
| 228 | // Get the groups for this G2 user | |
| 229 | list($ret, $g2_user_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->id); | |
| 230 | if ($ret) { | |
| 231 | gallery_error(t('Error getting Gallery group info for user (:uid)', | |
| 232 | array(':uid' => $g2_user->id)), $ret); | |
| 233 | return; | |
| 234 | } | |
| 235 | gallery_debug($g2_user_groups, t('G2 groups for G2 user (uid: :g2uid)', array(':g2uid' => $g2_user->id))); | |
| 236 | if (($g2_user_groups = _gallery_groups_map(array_keys($g2_user_groups))) === FALSE) { | |
| 237 | return; | |
| 238 | } | |
| 239 | gallery_debug($g2_user_groups, t('Mapped Drupal roles for G2 user (uid: :g2uid)', array(':g2uid' => $g2_user->id))); | |
| 240 | ||
| 241 | $user->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID; | |
| 242 | $user->roles[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID; | |
| 243 | gallery_debug($user->roles, t('Drupal roles for Drupal user (uid: :uid)', array(':uid' => $user->uid))); | |
| 244 | ||
| 245 | // Compare number of G2 groups and Drupal roles (of the user) | |
| 246 | $count_g2_groups = count($g2_user_groups); | |
| 247 | $count_roles = variable_get('gallery_user_admin_role', 0) ? count($user->roles) : count($user->roles) + 1; | |
| 248 | return ($count_g2_groups == $count_roles); | |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * Function _gallery_groups_import(). | |
| 253 | * (import Gallery groups into Drupal) | |
| 254 | */ | |
| 255 | function _gallery_groups_import() { | |
| 256 | // Fetch G2 album names | |
| 257 | list($ret, $g2_groups) = GalleryCoreApi::fetchGroupNames(); | |
| 258 | if ($ret) { | |
| 259 | gallery_error(t('Error fetching Gallery Group names'), $ret); | |
| 260 | return FALSE; | |
| 261 | } | |
| 262 | // Exlude 'Everybody' and 'Registered Users' groups | |
| 263 | list($ret, $g2_everybody_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); | |
| 264 | if ($ret) { | |
| 265 | gallery_error(t('Error retrieving Gallery group Id for \'Everybody\' group'), $ret); | |
| 266 | return; | |
| 267 | } | |
| 268 | list($ret, $g2_users_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); | |
| 269 | if ($ret) { | |
| 270 | gallery_error(t('Error retrieving Gallery group Id for \'Registered Users\' group'), $ret); | |
| 271 | return; | |
| 272 | } | |
| 273 | unset($g2_groups[$g2_everybody_gid], $g2_groups[$g2_users_gid]); | |
| 274 | // Check for admin roles mapping | |
| 275 | if (variable_get('gallery_user_admin_role', 0)) { | |
| 276 | list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); | |
| 277 | if ($ret) { | |
| 278 | gallery_error(t('Error getting \'adminGroup\' id'), $ret); | |
| 279 | return FALSE; | |
| 280 | } | |
| 281 | unset($g2_groups[$g2_admin_gid]); | |
| 282 | } | |
| 283 | // Create missing Drupal roles (using the G2 groupname) | |
| 284 | $roles = user_roles(); | |
| 285 | $g2_import_groups = array_diff($g2_groups, $roles); | |
| 286 | foreach ($g2_import_groups as $g2_groupname) { | |
| 287 | db_query("INSERT INTO {role} (name) VALUES ('%s')", $g2_groupname); | |
| 288 | } | |
| 289 | // Map Drupal roles <> Gallery2 group | |
| 290 | _gallery_groups_sync(); | |
| 291 | ||
| 292 | return TRUE; | |
| 293 | } | |
| 294 | ||
| 295 | /** | |
| 296 | * Function _gallery_groups_submit(). | |
| 297 | * (sync Drupal role <> G2 group when Drupal role has changed) | |
| 298 | */ | |
| 299 | function _gallery_groups_submit($form_id, &$form_values) { | |
| 300 | if (!_gallery_init(TRUE)) { | |
| 301 | return; | |
| 302 | } | |
| 303 | // Drupal roles have changed => resync | |
| 304 | _gallery_groups_sync(); | |
| 305 | ||
| 306 | GalleryEmbed::done(); | |
| 307 | } |