| c15e9178 |
1 | <?php |
| 2 | // $Id$ |
| 3 | /** |
| 4 | * gallery.module : gallery_roles.inc |
| 5 | * Group/Role Functions (sync groups, ...) |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Sync Drupal roles and Gallery groups for a specific user |
| 10 | */ |
| 11 | // Check for remove user. |
| 12 | function gallery_sync_groups_for_user($user) { |
| 13 | // Take this opportunity to sync the Drupal roles and Gallery groups |
| 14 | gallery_sync_groups(); |
| 15 | // Find the Ids for the 2 special Drupal roles (from user.module code) |
| 16 | $authenticated_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'")); |
| 17 | $anonymous_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'anonymous user'")); |
| 18 | |
| 19 | // Find the Ids for the special G2 groups |
| 20 | list ($ret, $g2_gid_everybodyGroup) = GalleryCoreApi::getPluginParameter('module', 'core', |
| 21 | 'id.everybodyGroup'); |
| 22 | if ($ret) { |
| 23 | $msg = t('Error retrieving Gallery group Id for \'Everybody\' group'); |
| 24 | gallery_error($msg, $ret); |
| 25 | } |
| 26 | |
| 27 | // Get the Gallery groups for this user |
| 28 | // First get the G2 Id from the Drupal uid |
| 29 | list ($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser'); |
| 30 | if ($ret) { |
| 31 | $msg = t('Error getting Gallery User info from Drupal Id'); |
| 32 | $msg .= ' ' . t('Drupal User Id: ') . $user->uid; |
| 33 | gallery_error($msg, $ret); |
| 34 | } |
| 35 | // Then get the groups for this user currently set in G2 |
| 36 | list ($ret, $g2_cur_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->getId()); |
| 37 | if ($ret) { |
| 38 | $msg = t('Error getting Gallery group info for user'); |
| 39 | $msg .= ' ' . t('Drupal User Id: ') . $user->uid; |
| 40 | gallery_error($msg, $ret); |
| 41 | } |
| 42 | // Now convert the new Drupal role Ids into Gallery Group Ids(for comparison) |
| 43 | foreach ($user->roles as $rid=>$role_name) { |
| 44 | list ($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup'); |
| 45 | if ($ret) { |
| 46 | $msg = t('Error getting Gallery Group Id from Drupal Role Id'); |
| 47 | $msg .= ' ' . t('Drupal Role Id: ') . $rid; |
| 48 | gallery_error($msg, $ret); |
| 49 | } |
| 50 | $g2_rid[$rid] = $g2_group->getId(); |
| 51 | } |
| 52 | // Find if the user needs to be deleted from any G2 groups (only mapped groups) |
| 53 | $g2_mapped_groups = gallery_get_mapped_groups(); |
| 54 | foreach ($g2_cur_groups as $gid=>$gname) { |
| 55 | if (!in_array($gid, $g2_rid) && ($gid != $g2_gid_everybodyGroup) && in_array($gid, $g2_mapped_groups)) { |
| 56 | $delete_list[] = $gid; |
| 57 | $ret = GalleryCoreApi::removeUserFromGroup($g2_user->getId(), $gid); |
| 58 | if ($ret) { |
| 59 | $msg = t('Error removing user from Gallery group'); |
| 60 | $msg .= ' ' . t('Gallery Group Id: ') . $gid . ' ' . t('Gallery Group Name: ') . $gname; |
| 61 | gallery_error($msg, $ret); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | // Find if the user needs to be added to any G2 groups |
| 66 | foreach ($g2_rid as $rid=>$gid) { |
| 67 | if (!isset($g2_cur_groups[$gid])) { |
| 68 | $add_list[] = $gid; |
| 69 | $ret = GalleryCoreApi::addUserToGroup($g2_user->getId(), $gid); |
| 70 | if ($ret) { |
| 71 | $msg = t('Error adding user to Gallery group'); |
| 72 | $msg .= ' ' . t('Gallery Group Id: ') . $gid; |
| 73 | gallery_error($msg, $ret); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sync Drupal roles and Gallery groups. This will add any mappings that are required |
| 81 | * (eg on first install, or if a group is added). It will also delete groups in Gallery |
| 82 | * that have been deleted from Drupal. |
| 83 | */ |
| 84 | function gallery_sync_groups() { |
| 85 | // Check if the Drupal role <-> G2 group mapping exists |
| 86 | $roles = user_roles(); |
| 87 | // Find the Ids for the 2 special Drupal groups (from user.module code) |
| 88 | $authenticated_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'")); |
| 89 | $anonymous_role = db_result(db_query("SELECT rid FROM {role} WHERE name = 'anonymous user'")); |
| 90 | // Go through each role and add or delete the gallery group if needed |
| 91 | foreach ($roles as $rid => $role_name) { |
| 92 | // Add Drupal<->G2 mapping if needed |
| 93 | $ret = GalleryEmbed::isExternalIdMapped($rid, 'GalleryGroup'); |
| 94 | if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { |
| 95 | // Need to add the mapping |
| 96 | switch ($rid) { |
| 97 | // Add mapping for Anonymous and get the G2 group Id |
| 98 | case $anonymous_role: |
| 99 | list ($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); |
| 100 | if ($ret) { |
| 101 | $msg = t('Error retrieving Gallery group Id for \'Everybody\' group'); |
| 102 | gallery_error($msg, $ret); |
| 103 | } |
| 104 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); |
| 105 | if ($ret) { |
| 106 | $msg = t('Error creating new Drupal role <-> Gallery group mapping (for \'anonymous user\' role)'); |
| 107 | $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Gallery Group Id: ') . $g2_gid; |
| 108 | gallery_error($msg, $ret); |
| 109 | } |
| 110 | break; |
| 111 | // Add mapping for authenticated users role and get the G2 group Id |
| 112 | case $authenticated_role: |
| 113 | list ($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); |
| 114 | if ($ret) { |
| 115 | $msg = t('Error retrieving Gallery group Id for \'Registered Users\' group'); |
| 116 | gallery_error($msg, $ret); |
| 117 | } |
| 118 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); |
| 119 | if ($ret) { |
| 120 | $msg = t('Error creating new Drupal role <-> Gallery group mapping (for \'authenticated user\' role)'); |
| 121 | $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Gallery Group Id: ') . $g2_gid; |
| 122 | gallery_error($msg, $ret); |
| 123 | } |
| 124 | break; |
| 125 | default: |
| 126 | // Is there already a group by this name? If so, map to it. |
| 127 | list ($ret, $g2_group) = GalleryCoreApi::fetchGroupByGroupName($role_name); |
| 128 | if (!$ret) { |
| 129 | $g2_gid = $g2_group->getId(); |
| 130 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); |
| 131 | if ($ret) { |
| 132 | $msg = t('Error creating new Drupal role <-> Gallery group mapping (by name)'); |
| 133 | $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Gallery Group Id: ') . $g2_gid; |
| 134 | gallery_error($msg, $ret); |
| 135 | } |
| 136 | } else { |
| 137 | // If not, create a new group |
| 138 | $ret = GalleryEmbed::createGroup($rid, $role_name); |
| 139 | if ($ret) { |
| 140 | $msg = t('Error creating new Gallery group'); |
| 141 | $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Drupal Role Name: ') . $role_name; |
| 142 | gallery_error($msg, $ret); |
| 143 | } |
| 144 | } |
| 145 | break; |
| 146 | } |
| 147 | } else { |
| 148 | // Update group name if needed (not for $authenticated_role or $anonymous_role) |
| 149 | list($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup'); |
| 150 | // In some cases the ExternalId may be present, but the user may have been deleted |
| 151 | if ($ret) { |
| 152 | $msg = t('Error retrieving Gallery Group Id from Drupal Role Id'); |
| 153 | $msg .= ' ' . t('Drupal Role Id: ') . $rid; |
| 154 | gallery_error($msg, $ret); |
| 155 | } |
| 156 | if (($rid != $authenticated_role) && ($rid != $anonymous_role) && ($role_name != $g2_group->getGroupName())) { |
| 157 | $ret = GalleryEmbed::updateGroup($rid, array('groupname'=>$role_name)); |
| 158 | if ($ret) { |
| 159 | $msg = t('Error updating Gallery group'); |
| 160 | $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Drupal Role Name: ') . $role_name; |
| 161 | gallery_error($msg, $ret); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | // Now check for any deleted Drupal roles. Only delete those G2 groups that were mapped to Drupal roles |
| 167 | // (just in case other groups have been defined which are not meant to be sync'd with Drupal) |
| 168 | list ($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId'); |
| 169 | if ($ret) { |
| 170 | $msg = t('Error retrieving all Drupal<->Gallery Map Ids'); |
| 171 | gallery_error($msg, $ret); |
| 172 | } |
| 173 | $g2_mapped_groups = gallery_get_mapped_groups(); |
| 174 | foreach ($g2_mapped_groups as $rid=>$g2_gid) { |
| 175 | // Delete if needed |
| 176 | if (!isset($roles[$rid])) { |
| 177 | $msg = t('Deleting G2 group') . ' (' . t('Gallery Group Id: ') . $g2_gid .')'; |
| 178 | $ret = GalleryEmbed::deleteGroup($rid); |
| 179 | if ($ret) { |
| 180 | $msg = t('Error deleting Gallery group'); |
| 181 | $msg .= ' ' . t('Gallery Group Id: ') . $g2_gid; |
| 182 | gallery_error($msg, $ret); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get G2 Groups that have been mapped to Drupal Roles |
| 190 | */ |
| 191 | function gallery_get_mapped_groups() { |
| 192 | list ($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId'); |
| 193 | if ($ret) { |
| 194 | $msg = t('Error retrieving all Drupal<->Gallery Map Ids'); |
| 195 | gallery_error($msg, $ret); |
| 196 | } |
| 197 | /* |
| 198 | * getExternalIdMap returns groups and user mappings. |
| 199 | * Cannot use 'externalId' as key as is not unique between users & groups |
| 200 | */ |
| 201 | foreach ($g2_map as $g2_gid => $g2_data) { |
| 202 | if ($g2_data['entityType'] == 'GalleryGroup') { |
| 203 | $g2_mapped_groups[$g2_data['externalId']] = $g2_gid; |
| 204 | } |
| 205 | } |
| 206 | return $g2_mapped_groups; |
| 207 | } |
| 208 | ?> |