--- /home/merlin/drupalmodules/taxonomy_access/taxonomy_access.module	2005-06-13 19:09:11.000000000 -0700
@@ -443,7 +446,8 @@
       else {      
 	      // Query the term_access table to get the permissions for the new node's assigned category/categories
 	      // then make appropriate changes to the node_access table.
-	      $result = db_query("SELECT * from {term_access} where tid in ('" . implode("','",array_values($tids)) . "')");
+        // HACK HACK HACK
+	      $result = db_query("SELECT rid, bit_or(grant_view) as grant_view, bit_or(grant_update) as grant_update, bit_or(grant_delete) as grant_delete from {term_access} where tid in ('" . implode("','",array_values($tids)) . "') group by rid");
 	      while($row = db_fetch_object($result)) {
 	        db_query("DELETE FROM {node_access} WHERE nid=%d AND gid=%d AND realm='term_access'", $node->nid, $row->rid);
 	      	db_query('INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, \'term_access\', %d, %d, %d)', $node->nid, $row->rid, $row->grant_view, $row->grant_update, $row->grant_delete);
@@ -786,18 +790,54 @@
 	
 	// Use the array structure of permissions we just created to make the updates to the node_access table
 	// for nodes that are in a category.
-	$result = db_query("SELECT tn.nid, tn.tid FROM {term_node} as tn, {term_data} as td WHERE td.tid = tn.tid");
-	$category_nids = array();
+	$result = db_query("SELECT tn.nid, tn.tid FROM {term_node} as tn ORDER BY tn.nid");
+  $count = db_num_rows($result);
+
+  // set up
+  $lastnid = 0;
+  $rownum = 0;
+  $nidperms = array();
 	while($row = db_fetch_object($result)) {
+    $rownum++;
  		$nid = $row->nid;
  		$tid = $row->tid;
- 		$category_nids[] = $nid; // created now for use later on in code handling nodes without categories
- 		if(array_key_exists($tid, $permissions)) {
- 			foreach($permissions[$tid] as $key => $value) {
-			  db_query("DELETE FROM {node_access} WHERE nid=%d AND gid=%d AND realm='term_access'", $nid, $key);
-			  db_query('INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, \'term_access\', %d, %d, %d)', $nid, $key, $value['grant_view'], $value['grant_update'], $value['grant_delete']);
- 			}
- 		}
+    // check to see if we changed nids or are at the last one.
+    if ($nid != $lastnid || $rownum == $count)
+    {
+      if ($rownum != $count && $lastnid == 0)
+      {
+        $lastnid = $nid;
+        continue; // this'll happen on the very first row but we gotta watch for 1 row result sets.
+      }
+
+      $loop = 1;
+      if ($rownum == $count && $nid != $lastnid)
+        $loop = 2;
+
+      // In the obnoxious case where there's only 1 category for the very 
+      // last one, we have to run this twice.
+      for ($i = 0; $i < $loop; $i++) {
+        foreach ($nidperms as $rid => $perm) {
+          db_query("DELETE FROM {node_access} WHERE nid=%d AND gid=%d AND realm='term_access'", $lastnid, $rid);
+          db_query('INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, \'term_access\', %d, %d, %d)', $lastnid, $rid, $perm['gv'], $perm['gu'], $perm['gd']);
+        }
+        $lastnid = $nid;
+        $nidperms = array();
+      }
+    }
+
+    if(array_key_exists($tid, $permissions)) {
+      foreach($permissions[$tid] as $rid => $value) {
+        if (!is_array($nidperms[$rid])) {
+          $nidperms[$rid] = array('gv' => 0, 'gu' => 0, 'gd' => 0);
+        }
+
+        $nidperms[$rid]['gv'] |= $value['grant_view'];
+        $nidperms[$rid]['gu'] |= $value['grant_update'];
+        $nidperms[$rid]['gd'] |= $value['grant_delete'];
+      }
+    }
+
 	}
 	
 	// Determine which nodes don't belong to a category
