Index: gallery_roles.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/gallery/gallery_roles.inc,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 gallery_roles.inc
--- gallery_roles.inc	17 Jul 2006 11:09:37 -0000	1.1.2.1
+++ gallery_roles.inc	30 Jan 2007 14:10:25 -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,7 +23,7 @@
     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;        
@@ -43,6 +31,8 @@
     return;
   }
   // Now convert the new Drupal role Ids into Gallery Group Ids(for comparison)
+  $user->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID;
+  $user->roles[DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID;
   foreach ($user->roles as $rid=>$role_name) { 
     list ($ret, $g2_group) = GalleryCoreApi::loadEntityByExternalId($rid, 'GalleryGroup');
     if ($ret) {
@@ -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,7 +58,7 @@
     }
   }
   // Find if the user needs to be added to any G2 groups
-  foreach ($g2_rid as $rid=>$gid) {
+  foreach ($g2_rid_map as $rid=>$gid) {
     if (!isset($g2_cur_groups[$gid])) {
       $add_list[] = $gid;
       $ret = GalleryCoreApi::addUserToGroup($g2_user->getId(), $gid);
@@ -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');
@@ -110,14 +97,14 @@
           }
           $ret = GalleryEmbed::addExternalIdMapEntry($rid, $g2_gid, 'GalleryGroup');
           if ($ret) {
-              $msg = t('Error creating new Drupal role <-> Gallery group mapping (for \'anonymous user\' role)');
-              $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Gallery Group Id: ') . $g2_gid;
-              gallery_error($msg, $ret);
-              return;
+            $msg = t('Error creating new Drupal role <-> Gallery group mapping (for \'anonymous user\' role)');
+            $msg .= ' ' . t('Drupal Role Id: ') . $rid . ' ' . t('Gallery Group Id: ') . $g2_gid;
+            gallery_error($msg, $ret);
+            return;
           }
           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');
@@ -181,9 +168,9 @@
   // (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) {
-    $msg = t('Error retrieving all Drupal<->Gallery Map Ids');
-    gallery_error($msg, $ret);
-    return;
+      $msg = t('Error retrieving all Drupal<->Gallery Map Ids');
+      gallery_error($msg, $ret);
+      return;
   }
   $g2_mapped_groups = gallery_get_mapped_groups();
   foreach ($g2_mapped_groups as $rid=>$g2_gid) {
@@ -207,9 +194,9 @@
 function gallery_get_mapped_groups() {
   list ($ret, $g2_map) = GalleryEmbed::getExternalIdMap('entityId');
   if ($ret) {
-    $msg = t('Error retrieving all Drupal<->Gallery Map Ids');
-    gallery_error($msg, $ret);
-    return;
+      $msg = t('Error retrieving all Drupal<->Gallery Map Ids');
+      gallery_error($msg, $ret);
+      return;
   }
   /* 
    * getExternalIdMap returns groups and user mappings. 
@@ -222,4 +209,4 @@
   }
   return $g2_mapped_groups;
 }
-?>
\ No newline at end of file
+?>
