uid, 'GalleryUser'); if ($ret) { gallery_error(t('Error loading Gallery user from Drupal user id (:uid)', array(':uid' => $user->uid)), $ret); return; } // then get the groups for this user currently set in G2 list($ret, $g2_user_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->id); if ($ret) { gallery_error(t('Error getting Gallery group info for user (:uid)', array(':uid' => $user->uid)), $ret); return; } // convert the new Drupal role Ids into Gallery Group Ids $user->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID; $user->roles[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID; gallery_debug($user->roles, t('Drupal roles for user (uid: :uid)', array(':uid' => $user->uid))); gallery_debug($g2_user_groups, t('G2 groups for G2 user (uid: :g2uid)', array(':g2uid' => $g2_user->id))); foreach ($user->roles as $rid => $role_name) { list($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup'); if ($ret) { gallery_error(t('Error getting Gallery Id from Drupal Role Id (:rid)', array(':uid' => $rid)), $ret); return; } $g2_rid_map[$rid] = $g2_group->getId(); } gallery_debug($g2_rid_map, t('Drupal roles <> G2 groups map (currently mapped)')); // find if the user needs to be deleted from any G2 groups (only mapped groups) $delete_list = array(); $g2_groups_map = _gallery_groups_map(); gallery_debug($g2_groups_map, t('Drupal roles <> G2 groups map (all existing)')); foreach ($g2_user_groups as $gid => $gname) { if (!in_array($gid, $g2_rid_map) && in_array($gid, $g2_groups_map)) { $delete_list[] = $gid; $ret = GalleryCoreApi::removeUserFromGroup($g2_user->id, $gid); if ($ret) { gallery_error(t('Error removing user from Gallery group (Gallery Group Id: :gid, Gallery Group Name: :gname)', array(':gid' => $gid, ':gname' => $gname)), $ret); return; } } } gallery_debug($delete_list, t('Remove user from these groups')); // find if the user needs to be added to any G2 groups $add_list = array(); foreach ($g2_rid_map as $rid => $gid) { if (!isset($g2_user_groups[$gid])) { $add_list[] = $gid; $ret = GalleryCoreApi::addUserToGroup($g2_user->id, $gid); if ($ret) { gallery_error(t('Error adding user to Gallery group (:gid)', array(':gid' => $gid)), $ret); return; } } } gallery_debug($add_list, t('Add user to these groups')); } /** * Sync Drupal roles and Gallery groups */ function _gallery_groups_sync() { // check if the Drupal role <> G2 group mapping exists $roles = user_roles(); $admin_role = variable_get('gallery_user_admin_role', 0); foreach ($roles as $rid => $role_name) { // add Drupal <> G2 mapping if needed $ret = GalleryEmbed::isExternalIdMapped($rid, 'GalleryGroup'); if ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { switch ($rid) { case DRUPAL_ANONYMOUS_RID: list($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); if ($ret) { gallery_error(t('Error retrieving Gallery group Id for \'Everybody\' group'), $ret); return; } $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); if ($ret) { gallery_error(t('Error creating Drupal role <> Gallery group mapping for \'anonymous user\' role (Drupal Role Id: :rid, Gallery Group Id: :gid)', array(':rid' => $rid, ':gid' => $g2_gid)), $ret); return; } break; case DRUPAL_AUTHENTICATED_RID: list($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); if ($ret) { gallery_error(t('Error retrieving Gallery group Id for \'Registered Users\' group'), $ret); return; } $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); if ($ret) { gallery_error(t('Error creating Drupal role <> Gallery group mapping for \'authenticated user\' role (Drupal Role Id: :rid, Gallery Group Id: :gid)', array(':rid' => $rid, ':gid' => $g2_gid)), $ret); return; } break; default: // special handling of the 'admin' role if ($rid == $admin_role) { // get G2 admin group id list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); if ($ret) { gallery_error(t('Error getting \'adminGroup\' id'), $ret); return FALSE; } $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_admin_gid, 'GalleryGroup'); if ($ret) { gallery_error(t('Error creating Drupal role <> Gallery \'Site Admin\' group mapping (Drupal Role Id: :rid, Gallery Group Id: :gid)', array(':rid' => $rid, ':gid' => $g2_gid)), $ret); return; } } else { // is there a group with this name already list($ret, $g2_group) = GalleryCoreApi::fetchGroupByGroupName($role_name); if (!$ret) { $g2_gid = $g2_group->getId(); $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup'); if ($ret) { gallery_error(t('Error creating Drupal role <> Gallery group mapping (Drupal Role Id: :rid, Gallery Group Id: :gid)', array(':rid' => $rid, ':gid' => $g2_gid)), $ret); return; } } else { $ret = GalleryEmbed::createGroup($rid, $role_name); if ($ret) { gallery_error(t('Error creating Gallery group (Drupal Role Id: :rid, Drupal Role Name: :rname)', array(':rid' => $rid, ':rname' => $role_name)), $ret); return; } } } break; } } else { // update group name if needed list($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup'); if ($ret) { gallery_error(t('Error retrieving Gallery Group Id from Drupal Role Id (Drupal Role Id: :rid)', array(':rid' => $rid)), $ret); return; } if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID, $admin_role)) && ($role_name != $g2_group->getGroupName())) { $ret = GalleryEmbed::updateGroup($rid, array('groupname' => $role_name)); if ($ret) { gallery_error(t('Error updating Gallery group (Drupal Role Id: :rid, Drupal Role Name: :rname)', array(':rid' => $rid, ':rname' => $role_name)), $ret); return; } } } } // now check for any deleted Drupal roles. Only delete those G2 groups that were mapped to Drupal roles // (just in case other groups have been defined which are not meant to be sync'd with Drupal) list($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId'); if ($ret) { gallery_error(t('Error retrieving all Drupal <> Gallery Map Ids'), $ret); return; } $g2_groups_map = _gallery_groups_map(); foreach ($g2_groups_map as $rid => $g2_gid) { if (!isset($roles[$rid])) { $ret = GalleryEmbed::deleteGroup($rid); if ($ret) { gallery_error(t('Error deleting Gallery group (Gallery Group Id: :gid)', array(':gid' => $g2_gid)), $ret); return; } } } } /** * Get Gallery groups that have been mapped to Drupal roles */ function _gallery_groups_map() { list($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId'); if ($ret) { gallery_error(t('Error retrieving all Drupal <> Gallery Map Ids'), $ret); return; } // ! getExternalIdMap returns groups and user mappings ! foreach ($g2_map as $g2_gid => $g2_data) { if ($g2_data['entityType'] == 'GalleryGroup') { $g2_groups_map[$g2_data['externalId']] = $g2_gid; } } return $g2_groups_map; } /** * Import Gallery groups into Drupal */ function _gallery_groups_import() { // fetch G2 album names list($ret, $g2_groups) = GalleryCoreApi::fetchGroupNames(); if ($ret) { gallery_error(t('Error fetching Gallery Group names'), $ret); return FALSE; } // exlude 'Everybody' and 'Registered Users' groups list($ret, $g2_everybody_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup'); if ($ret) { gallery_error(t('Error retrieving Gallery group Id for \'Everybody\' group'), $ret); return; } list($ret, $g2_users_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup'); if ($ret) { gallery_error(t('Error retrieving Gallery group Id for \'Registered Users\' group'), $ret); return; } unset($g2_groups[$g2_everybody_gid], $g2_groups[$g2_users_gid]); // check for admin roles mapping if (variable_get('gallery_user_admin_role', 0)) { list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); if ($ret) { gallery_error(t('Error getting \'adminGroup\' id'), $ret); return FALSE; } unset($g2_groups[$g2_admin_gid]); } // create missing Drupal roles (using the G2 groupname) $roles = user_roles(); $g2_import_groups = array_diff($g2_groups, $roles); foreach ($g2_import_groups as $g2_groupname) { db_query("INSERT INTO {role} (name) VALUES ('%s')", $g2_groupname); } // map Drupal roles <> Gallery2 group _gallery_groups_sync(); return TRUE; } /** * Sync Drupal role <> G2 group when Drupal role has changed */ function _gallery_groups_submit($form_id, &$form_values) { if (!_gallery_init(TRUE)) { return; } // Drupal roles have changed => resync _gallery_groups_sync(); GalleryEmbed::done(); } ?>