Index: gallery_roles.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/gallery/gallery_roles.inc,v
retrieving revision 1.2
diff -u -r1.2 gallery_roles.inc
--- gallery_roles.inc	5 Jun 2006 11:02:32 -0000	1.2
+++ gallery_roles.inc	30 Jan 2007 13:47:06 -0000
@@ -12,18 +12,6 @@
 function gallery_sync_groups_for_user($user) {
   // Take this opportunity to sync the Drupal roles and Gallery groups
   gallery_sync_groups();
-  // Find the Ids for the 2 special Drupal roles (from user.module code)
-  $authenticated_role =  db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'"));
-  $anonymous_role =  db_result(db_query("SELECT rid FROM {role} WHERE name = 'anonymous user'"));
-  
-  // Find the Ids for the special G2 groups 
-  list ($ret, $g2_gid_everybodyGroup) = GalleryCoreApi::getPluginParameter('module', 'core',
-    'id.everybodyGroup');
-  if ($ret) {
-    $msg = t('Error retrieving Gallery group Id for \'Everybody\' group');
-    gallery_error($msg, $ret);
-    return;
-  }
   
   // Get the Gallery groups for this user
   // First get the G2 Id from the Drupal uid
@@ -35,13 +23,15 @@
     return;
   }
   // Then get the groups for this user currently set in G2
-  list ($ret, $g2_cur_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->getId());
+  list ($ret, $g2_user_groups) = GalleryCoreApi::fetchGroupsForUser($g2_user->getId());
   if ($ret) {
     $msg = t('Error getting Gallery group info for user');
     $msg .= ' ' . t('Drupal User Id: ') . $user->uid;        
     gallery_error($msg, $ret);
     return;
   }
+  $user->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID;
+  $user->roles[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID;
   // Now convert the new Drupal role Ids into Gallery Group Ids(for comparison)
   foreach ($user->roles as $rid=>$role_name) { 
     list ($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup');
@@ -51,12 +41,12 @@
       gallery_error($msg, $ret);
       return;
     }
-    $g2_rid[$rid] = $g2_group->getId();
+    $g2_rid_map[$rid] = $g2_group->getId();
   }
   // Find if the user needs to be deleted from any G2 groups (only mapped groups)
   $g2_mapped_groups = gallery_get_mapped_groups();
-  foreach ($g2_cur_groups as $gid=>$gname) {
-    if (!in_array($gid, $g2_rid) && ($gid != $g2_gid_everybodyGroup) && in_array($gid, $g2_mapped_groups)) {
+  foreach ($g2_user_groups as $gid=>$gname) {
+    if (!in_array($gid, $g2_rid_map) && in_array($gid, $g2_mapped_groups)) {
       $delete_list[] = $gid;
       $ret = GalleryCoreApi::removeUserFromGroup($g2_user->getId(), $gid);
       if ($ret) {
@@ -68,8 +58,8 @@
     }
   }
   // Find if the user needs to be added to any G2 groups
-  foreach ($g2_rid as $rid=>$gid) {
-    if (!isset($g2_cur_groups[$gid])) {
+  foreach ($g2_rid_map as $rid=>$gid) {
+    if (!isset($g2_user_groups[$gid])) {
       $add_list[] = $gid;
       $ret = GalleryCoreApi::addUserToGroup($g2_user->getId(), $gid);
       if ($ret) {
@@ -79,7 +69,7 @@
         return;
       }
     }
-  }  
+  }
 }
 
 /**
@@ -90,9 +80,6 @@
 function gallery_sync_groups() {
   // Check if the Drupal role <-> G2 group mapping exists
   $roles = user_roles();
-  // Find the Ids for the 2 special Drupal groups (from user.module code)
-  $authenticated_role =  db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'"));
-  $anonymous_role =  db_result(db_query("SELECT rid FROM {role} WHERE name = 'anonymous user'"));
   // Go through each role and add or delete the gallery group if needed
   foreach ($roles as $rid => $role_name) {
     // Add Drupal<->G2 mapping if needed
@@ -101,7 +88,7 @@
     // Need to add the mapping
       switch ($rid) {
         // Add mapping for Anonymous and get the G2 group Id
-        case $anonymous_role:
+        case DRUPAL_ANONYMOUS_RID:
           list ($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup');
           if ($ret) {
             $msg = t('Error retrieving Gallery group Id for \'Everybody\' group');
@@ -117,7 +104,7 @@
           }
           break;
         // Add mapping for authenticated users role and get the G2 group Id
-        case $authenticated_role:
+        case DRUPAL_AUTHENTICATED_RID:
           list ($ret, $g2_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.allUserGroup');
           if ($ret) {
             $msg = t('Error retrieving Gallery group Id for \'Registered Users\' group');
@@ -222,4 +209,4 @@
   }
   return $g2_mapped_groups;
 }
-?>
\ No newline at end of file
+?>
