--- userplus/userplus.module	2006-10-21 23:27:18.000000000 -0500
+++ userplus_mod/userplus.module	2006-11-19 01:37:37.000000000 -0600
@@ -1,5 +1,5 @@
 <?php
-// $Id: userplus.module,v 1.5 2006/10/22 04:27:18 marcp Exp $
+// $Id: userplus.module,v 1.1.2.4 2006/10/22 04:26:25 marcp Exp $
 
 /**
  * @file
@@ -273,25 +273,24 @@ function theme_userplus_add_users($edit)
 
 // Assumes $edit['user'][uid][rid]...
 function userplus_admin_userperms_submit($form_id, $edit) {
-  // Figure out all the users who are on this page so we can bulk
-  // delete their old user/role associations before inserting the
-  // new ones...
+  // Figure out all the users who are on this page so we can load
+  // their user object and replace the old roles with the new
+  // roles and then save the user object
   $users = $edit['user'];
-  $uids = array_keys($users);
-  
-  if (count($uids) > 0) {
-    $instring = '(' . implode(',', $uids) . ')';
-    
-    // Clear old user/role associations for these users...
-    db_query('DELETE FROM {users_roles} WHERE uid IN %s', $instring);
-  }
-  
+
+  $roles_values = user_roles();   // get all possible user roles as [rid] => 'role name'
+
   foreach ($users as $uid => $values) {
-    // Insert the new user/role associations for this user...
-    foreach ($values as $role => $checked) {
-      if ($checked == 1) {
-        db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, $role);
+    $user = user_load(array('uid' => $uid));
+    if ($user) {
+      $new_roles = array();         // array to hold roles in form [rid] => 'role name'
+      $userroles = $users[$uid];    // array with new user roles in form [rid] => 1 or 0
+      foreach ($userroles as $rid => $role_grant) {
+        if ($role_grant == 1) {     // add this role to $new_roles
+          $new_roles[$rid] = $roles_values[$rid];
+        }
       }
+      $user = user_save($user, array('roles' => $new_roles));
     }
   }
   
