name that user_save() is expecting. if (!empty($roles)) { foreach ($roles as $name) { $rid = install_get_rid($name); $role_assignments[$rid] = $name; } } return user_save( new stdClass(), array( 'name' => $username, 'pass' => $password, 'mail' => $email, 'roles' => $role_assignments, 'status' => $status ) ); } /** * Add a user to a role. */ function install_add_user_to_role($uid, $rid) { if ($uid <= 0 || $rid <= 0) { return FALSE; } $count = db_result(db_query("SELECT COUNT(uid) FROM {users_roles} WHERE uid = %d AND rid = %d", $uid, $rid)); if ($count == 0) { return db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $uid, $rid); } return FALSE; } /** * Add a role to the roles table. */ function install_add_role($name) { // Check to see if role already exists. If not, create it. $rid = install_get_rid($name); if (!$rid) { db_query("INSERT INTO {role} (name) VALUES ('%s')", $name); } return ($rid) ? $rid : install_get_rid($name); } /** * Remove a role from the roles table and delete any user associations. */ function install_remove_role($name) { $rid = install_get_rid($name); if (!$rid) { return FALSE; } // Next 4 lines taken from core (user_admin_role_submit in user.admin.inc) db_query('DELETE FROM {role} WHERE rid = %d', $rid); db_query('DELETE FROM {permission} WHERE rid = %d', $rid); // Update the users who have this role set: db_query('DELETE FROM {users_roles} WHERE rid = %d', $rid); return TRUE; } /** * Get the role ID for the role name. */ function install_get_rid($name) { static $roles = array(); if (empty($roles[$name])) { $roles[$name] = db_result(db_query_range("SELECT rid FROM {role} WHERE name ='%s'", $name, 0, 1)); } return $roles[$name]; } /** * Add the permission for a certain role. */ function install_add_permissions($rid, $perms) { // Retrieve the currently set permissions. $result = db_query("SELECT p.perm FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid = %d ", $rid); $existing_perms = array(); while ($row = db_fetch_object($result)) { $existing_perms += explode(', ', $row->perm); } // If this role already has permissions, merge them with the new permissions being set. if (count($existing_perms) > 0) { $perms = array_unique(array_merge($perms, (array)$existing_perms)); } // Update the permissions. db_query('DELETE FROM {permission} WHERE rid = %d', $rid); db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $perms)); } /** * Remove permissions for a certain role. */ function install_remove_permissions($rid, $perms) { // Retrieve the currently set permissions. $result = db_query("SELECT p.perm FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid = %d ", $rid); $existing_perms = array(); while ($row = db_fetch_object($result)) { $existing_perms += explode(', ', $row->perm); } $new_perms = array_diff($existing_perms, $perms); // Update the permissions. db_query('DELETE FROM {permission} WHERE rid = %d', $rid); db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $new_perms)); }