| Commit | Line | Data |
|---|---|---|
| 088bea1c TW |
1 | <?php |
| 2 | // $Id$ | |
| 3 | ||
| 4 | /** | |
| 5 | * gallery.module : gallery_groups.inc | |
| 6 | * Group/Role Functions (sync groups, ...) | |
| 7 | */ | |
| 8 | ||
| 9 | /** | |
| 10 | * Sync Drupal roles and Gallery groups for a specific user | |
| 11 | */ | |
| 72d7f2e1 | 12 | function _gallery_groups_user($user, $groups = FALSE) { |
| 088bea1c TW |
13 | // sync the Drupal roles and Gallery groups |
| 14 | if ($groups) { | |
| 15 | _gallery_groups_sync(); | |
| 16 | } | |
| 17 | ||
| 18 | // get the Gallery groups for this user | |
| 19 | // first get the G2 Id from the Drupal uid | |
| 6758036d | 20 | list($ret, $g2_user) = GalleryCoreApi::loadEntityByExternalId($user->uid, 'GalleryUser'); |
| 088bea1c TW |
21 | if ($ret) { |
| 22 | gallery_error(t('Error loading Gallery user from Drupal user id (:uid)', | |
| 23 | array(':uid' => $user->uid)), $ret); | |
| 24 | return; | |
| 25 | } | |
| 26 | // then get the groups for this user currently set in G2 | |
| 6758036d | 27 | list($ret, $g2_user_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->getId()); |
| 088bea1c TW |
28 | if ($ret) { |
| 29 | gallery_error(t('Error getting Gallery group info for user (:uid)', | |
| 30 | array(':uid' => $user->uid)), $ret); | |
| 31 | return; | |
| 32 | } | |
| 33 | // convert the new Drupal role Ids into Gallery Group Ids | |
| 34 | $user->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID; | |
| 35 | $user->roles[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID; | |
| 36 | gallery_debug($user->roles, t('Drupal roles for user (uid: :uid)', array(':uid' => $user->uid))); | |
| 37 | gallery_debug($g2_user_groups, t('G2 groups for G2 user (uid: :g2uid)', array(':g2uid' => $g2_user->getId()))); | |
| 38 | foreach ($user->roles as $rid => $role_name) { | |
| 6758036d | 39 | list($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup'); |
| 088bea1c TW |
40 | if ($ret) { |
| 41 | gallery_error(t('Error getting Gallery Id from Drupal Role Id (:rid)', | |
| 42 | array(':uid' => $rid)), $ret); | |
| 43 | return; | |
| 44 | } | |
| 45 | $g2_rid_map[$rid] = $g2_group->getId(); | |
| 46 | } | |
| 47 | gallery_debug($g2_rid_map, t('Drupal roles <> G2 groups map (currently mapped)')); | |
| 48 | // find if the user needs to be deleted from any G2 groups (only mapped groups) | |
| 49 | $delete_list = array(); | |
| 50 | $g2_groups_map = _gallery_groups_map(); | |
| 51 | gallery_debug($g2_groups_map, t('Drupal roles <> G2 groups map (all existing)')); | |
| 52 | foreach ($g2_user_groups as $gid => $gname) { | |
| 53 | if (!in_array($gid, $g2_rid_map) && in_array($gid, $g2_groups_map)) { | |
| 54 | $delete_list[] = $gid; | |
| 55 | $ret = GalleryCoreApi::removeUserFromGroup($g2_user->getId(), $gid); | |
| 56 | if ($ret) { | |
| 57 | gallery_error(t('Error removing user from Gallery group (Gallery Group Id: :gid, Gallery Group Name: :gname)', | |
| 58 | array(':gid' => $gid, ':gname' => $gname)), $ret); | |
| 59 | return; | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | gallery_debug($delete_list, t('Remove user from these groups')); | |
| 64 | // find if the user needs to be added to any G2 groups | |
| 65 | $add_list = array(); | |
| 66 | foreach ($g2_rid_map as $rid => $gid) { | |
| 67 | if (!isset($g2_user_groups[$gid])) { | |
| 68 | $add_list[] = $gid; | |
| 69 | $ret = GalleryCoreApi::addUserToGroup($g2_user->getId(), $gid); | |
| 70 | if ($ret) { | |
| 71 | gallery_error(t('Error adding user to Gallery group (:gid)', | |
| 72 | array(':gid' => $gid)), $ret); | |
| 73 | return; | |
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | gallery_debug($add_list, t('Add user to these groups')); | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Sync Drupal roles and Gallery groups | |
| 82 | */ | |
| 83 | function _gallery_groups_sync() { | |
| 84 | // check if the Drupal role <> G2 group mapping exists | |
| 85 | $roles = user_roles(); | |
| 647a600b | 86 | $admin_role = variable_get('gallery_user_admin_role', 0); |
| 088bea1c TW |
87 | foreach ($roles as $rid => $role_name) { |
| 88 | // add Drupal <> G2 mapping if needed | |
| 89 | $ret = GalleryEmbed::isExternalIdMapped($rid, 'GalleryGroup'); | |
| 90 | if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { | |
| 91 | switch ($rid) { | |
| 92 | case DRUPAL_ANONYMOUS_RID: | |
| 6758036d | 93 | list($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); |
| 088bea1c TW |
94 | if ($ret) { |
| 95 | gallery_error(t('Error retrieving Gallery group Id for \'Everybody\' group'), $ret); | |
| 96 | return; | |
| 97 | } | |
| 98 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); | |
| 99 | if ($ret) { | |
| 100 | gallery_error(t('Error creating Drupal role <> Gallery group mapping for \'anonymous user\' role (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 101 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); | |
| 102 | return; | |
| 103 | } | |
| 104 | break; | |
| 105 | case DRUPAL_AUTHENTICATED_RID: | |
| 6758036d | 106 | list($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); |
| 088bea1c TW |
107 | if ($ret) { |
| 108 | gallery_error(t('Error retrieving Gallery group Id for \'Registered Users\' group'), $ret); | |
| 109 | return; | |
| 110 | } | |
| 111 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); | |
| 112 | if ($ret) { | |
| 113 | gallery_error(t('Error creating Drupal role <> Gallery group mapping for \'authenticated user\' role (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 114 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); | |
| 115 | return; | |
| 116 | } | |
| 117 | break; | |
| 118 | default: | |
| 647a600b TW |
119 | // special handling of the 'admin' role |
| 120 | if ($rid == $admin_role) { | |
| 121 | // get G2 admin group id | |
| 6758036d | 122 | list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); |
| 123 | if ($ret) { | |
| 647a600b TW |
124 | gallery_error(t('Error getting \'adminGroup\' id'), $ret); |
| 125 | return FALSE; | |
| 6758036d | 126 | } |
| 127 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_admin_gid, 'GalleryGroup'); | |
| 088bea1c | 128 | if ($ret) { |
| 647a600b | 129 | gallery_error(t('Error creating Drupal role <> Gallery \'Site Admin\' group mapping (Drupal Role Id: :rid, Gallery Group Id: :gid)', |
| 088bea1c TW |
130 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); |
| 131 | return; | |
| 132 | } | |
| 133 | } | |
| 134 | else { | |
| 647a600b | 135 | // is there a group with this name already |
| 6758036d | 136 | list($ret, $g2_group) = GalleryCoreApi::fetchGroupByGroupName($role_name); |
| 647a600b TW |
137 | if (!$ret) { |
| 138 | $g2_gid = $g2_group->getId(); | |
| 139 | $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); | |
| 140 | if ($ret) { | |
| 141 | gallery_error(t('Error creating Drupal role <> Gallery group mapping (Drupal Role Id: :rid, Gallery Group Id: :gid)', | |
| 142 | array(':rid' => $rid, ':gid' => $g2_gid)), $ret); | |
| 143 | return; | |
| 144 | } | |
| 145 | } | |
| 146 | else { | |
| 147 | $ret = GalleryEmbed::createGroup($rid, $role_name); | |
| 148 | if ($ret) { | |
| 149 | gallery_error(t('Error creating Gallery group (Drupal Role Id: :rid, Drupal Role Name: :rname)', | |
| 150 | array(':rid' => $rid, ':rname' => $role_name)), $ret); | |
| 151 | return; | |
| 152 | } | |
| 088bea1c TW |
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 | } | |
| 647a600b | 166 | if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID, $admin_role)) && ($role_name != $g2_group->getGroupName())) { |
| 088bea1c TW |
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) | |
| 6758036d | 178 | list($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId'); |
| 088bea1c TW |
179 | if ($ret) { |
| 180 | gallery_error(t('Error retrieving all Drupal <> Gallery Map Ids'), $ret); | |
| 181 | return; | |
| 182 | } | |
| 183 | $g2_groups_map = _gallery_groups_map(); | |
| 184 | foreach ($g2_groups_map as $rid => $g2_gid) { | |
| 185 | if (!isset($roles[$rid])) { | |
| 186 | $ret = GalleryEmbed::deleteGroup($rid); | |
| 187 | if ($ret) { | |
| 188 | gallery_error(t('Error deleting Gallery group (Gallery Group Id: :gid)', | |
| 189 | array(':gid' => $g2_gid)), $ret); | |
| 190 | return; | |
| 191 | } | |
| 192 | } | |
| 193 | } | |
| 194 | } | |
| 195 | ||
| 196 | /** | |
| 197 | * Get Gallery groups that have been mapped to Drupal roles | |
| 198 | */ | |
| 199 | function _gallery_groups_map() { | |
| 6758036d | 200 | list($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId'); |
| 088bea1c TW |
201 | if ($ret) { |
| 202 | gallery_error(t('Error retrieving all Drupal <> Gallery Map Ids'), $ret); | |
| 203 | return; | |
| 204 | } | |
| 205 | // ! getExternalIdMap returns groups and user mappings ! | |
| 206 | foreach ($g2_map as $g2_gid => $g2_data) { | |
| 207 | if ($g2_data['entityType'] == 'GalleryGroup') { | |
| 208 | $g2_groups_map[$g2_data['externalId']] = $g2_gid; | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | return $g2_groups_map; | |
| 213 | } | |
| 214 | ||
| 215 | /** | |
| 216 | * Import Gallery groups into Drupal | |
| 217 | */ | |
| 218 | function _gallery_groups_import() { | |
| 219 | // fetch G2 album names | |
| 220 | list($ret, $g2_groups) = GalleryCoreApi::fetchGroupNames(); | |
| 221 | if ($ret) { | |
| 222 | gallery_error(t('Error fetching Gallery Group names'), $ret); | |
| 223 | return FALSE; | |
| 224 | } | |
| 647a600b | 225 | // exlude 'Everybody' and 'Registered Users' groups |
| 6758036d | 226 | list($ret, $g2_everybody_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); |
| 647a600b TW |
227 | if ($ret) { |
| 228 | gallery_error(t('Error retrieving Gallery group Id for \'Everybody\' group'), $ret); | |
| 229 | return; | |
| 230 | } | |
| 6758036d | 231 | list($ret, $g2_users_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); |
| 647a600b TW |
232 | if ($ret) { |
| 233 | gallery_error(t('Error retrieving Gallery group Id for \'Registered Users\' group'), $ret); | |
| 234 | return; | |
| 235 | } | |
| 236 | unset($g2_groups[$g2_everybody_gid], $g2_groups[$g2_users_gid]); | |
| 237 | // check for admin roles mapping | |
| 238 | if (variable_get('gallery_user_admin_role', 0)) { | |
| 6758036d | 239 | list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); |
| 240 | if ($ret) { | |
| 647a600b TW |
241 | gallery_error(t('Error getting \'adminGroup\' id'), $ret); |
| 242 | return FALSE; | |
| 6758036d | 243 | } |
| 244 | unset($g2_groups[$g2_admin_gid]); | |
| 647a600b | 245 | } |
| 088bea1c TW |
246 | // create missing Drupal roles (using the G2 groupname) |
| 247 | $roles = user_roles(); | |
| 248 | $g2_import_groups = array_diff($g2_groups, $roles); | |
| 249 | foreach ($g2_import_groups as $g2_groupname) { | |
| 250 | db_query("INSERT INTO {role} (name) VALUES ('%s')", $g2_groupname); | |
| 251 | } | |
| 252 | // map Drupal roles <> Gallery2 group | |
| 647a600b TW |
253 | _gallery_groups_sync(); |
| 254 | ||
| 088bea1c TW |
255 | return TRUE; |
| 256 | } | |
| 257 | ||
| 258 | /** | |
| 259 | * Sync Drupal role <> G2 group when Drupal role has changed | |
| 260 | */ | |
| 261 | function _gallery_groups_submit($form_id, &$form_values) { | |
| 647a600b TW |
262 | if (!_gallery_init(TRUE)) { |
| 263 | return; | |
| 264 | } | |
| 265 | // Drupal roles have changed => resync | |
| 088bea1c | 266 | _gallery_groups_sync(); |
| 647a600b TW |
267 | |
| 268 | GalleryEmbed::done(); | |
| 088bea1c TW |
269 | } |
| 270 | ||
| 271 | ?> |