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