diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 60d86a7c22e..fe3cb56783b 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -8,6 +8,8 @@ Drupal x.x.x, xxxx-xx-xx (development version) - poll module: * optionally allow people to inspect all votes. * optionally allow people to cancel their vote. +- user module: + * made it possible to instantly assign roles to newly created user accounts. - distributed authentication: * added default server option. - fixed critical SQL issue, see SA-2006-005 diff --git a/modules/user.module b/modules/user.module index ee69debd9bf..94a473495a4 100644 --- a/modules/user.module +++ b/modules/user.module @@ -190,12 +190,6 @@ function user_save($account, $array = array(), $category = 'account') { } db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values); - // Reload user roles (delete just to be safe). - db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']); - foreach ((array)$array['roles'] as $rid) { - db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid); - } - // Build the initial user object. $user = user_load(array('uid' => $array['uid'])); @@ -210,6 +204,14 @@ function user_save($account, $array = array(), $category = 'account') { } db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid); + // Save user roles (delete just to be safe). + db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']); + foreach (array_keys($array['roles']) as $rid) { + if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { + db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid); + } + } + // Build the finished user object. $user = user_load(array('uid' => $array['uid'])); } @@ -1187,6 +1189,16 @@ function user_register() { '#description' => t('Provide a password for the new account.'), '#required' => TRUE, ); + $roles = user_roles(1); + unset($roles[DRUPAL_AUTHENTICATED_RID]); + if ($roles) { + $form['roles'] = array('#type' => 'checkboxes', + '#title' => t('Roles'), + '#default_value' => array_keys((array)$edit['roles']), + '#options' => $roles, + '#description' => t('The user receives the combined permissions of the authenticated user role and all roles selected here.') + ); + } $form['notify'] = array( '#type' => 'checkbox', '#title' => t('Notify user of new account') @@ -1202,8 +1214,9 @@ function user_register() { $form['account']['name'] = $form['name']; $form['account']['mail'] = $form['mail']; $form['account']['pass'] = $form['pass']; + $form['account']['roles'] = $form['roles']; $form['account']['notify'] = $form['notify']; - unset($form['name'], $form['mail'], $form['pass'], $form['notify']); + unset($form['name'], $form['mail'], $form['pass'], $form['roles'], $form['notify']); $form = array_merge($form, $extra); } $form['submit'] = array('#type' => 'submit', '#value' => t('Create new account'), '#weight' => 30); @@ -1223,6 +1236,7 @@ function user_register_submit($form_id, $form_values) { $mail = $form_values['mail']; $name = $form_values['name']; $pass = $admin ? $form_values['pass'] : user_password(); + $roles = array_filter($form_values['roles']); // Remove unset roles $notify = $form_values['notify']; $from = variable_get('site_mail', ini_get('sendmail_from')); @@ -1231,7 +1245,7 @@ function user_register_submit($form_id, $form_values) { return 'user/register'; } - $account = user_save('', array_merge($form_values, array('pass' => $pass, 'init' => $mail, 'status' => ($admin || variable_get('user_register', 1) == 1)))); + $account = user_save('', array_merge($form_values, array('pass' => $pass, 'init' => $mail, 'roles' => $roles, 'status' => ($admin || variable_get('user_register', 1) == 1)))); watchdog('user', t('New user: %name %email.', array('%name' => theme('placeholder', $name), '%email' => theme('placeholder', '<'. $mail .'>'))), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit')); $variables = array('%username' => $name, '%site' => variable_get('site_name', 'drupal'), '%password' => $pass, '%uri' => $base_url, '%uri_brief' => substr($base_url, strlen('http://')), '%mailto' => $mail, '%date' => format_date(time()), '%login_uri' => url('user', NULL, NULL, TRUE), '%edit_uri' => url('user/'. $account->uid .'/edit', NULL, NULL, TRUE), '%login_url' => user_pass_reset_url($account)); diff --git a/modules/user/user.module b/modules/user/user.module index ee69debd9bf..94a473495a4 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -190,12 +190,6 @@ function user_save($account, $array = array(), $category = 'account') { } db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values); - // Reload user roles (delete just to be safe). - db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']); - foreach ((array)$array['roles'] as $rid) { - db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid); - } - // Build the initial user object. $user = user_load(array('uid' => $array['uid'])); @@ -210,6 +204,14 @@ function user_save($account, $array = array(), $category = 'account') { } db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid); + // Save user roles (delete just to be safe). + db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']); + foreach (array_keys($array['roles']) as $rid) { + if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { + db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid); + } + } + // Build the finished user object. $user = user_load(array('uid' => $array['uid'])); } @@ -1187,6 +1189,16 @@ function user_register() { '#description' => t('Provide a password for the new account.'), '#required' => TRUE, ); + $roles = user_roles(1); + unset($roles[DRUPAL_AUTHENTICATED_RID]); + if ($roles) { + $form['roles'] = array('#type' => 'checkboxes', + '#title' => t('Roles'), + '#default_value' => array_keys((array)$edit['roles']), + '#options' => $roles, + '#description' => t('The user receives the combined permissions of the authenticated user role and all roles selected here.') + ); + } $form['notify'] = array( '#type' => 'checkbox', '#title' => t('Notify user of new account') @@ -1202,8 +1214,9 @@ function user_register() { $form['account']['name'] = $form['name']; $form['account']['mail'] = $form['mail']; $form['account']['pass'] = $form['pass']; + $form['account']['roles'] = $form['roles']; $form['account']['notify'] = $form['notify']; - unset($form['name'], $form['mail'], $form['pass'], $form['notify']); + unset($form['name'], $form['mail'], $form['pass'], $form['roles'], $form['notify']); $form = array_merge($form, $extra); } $form['submit'] = array('#type' => 'submit', '#value' => t('Create new account'), '#weight' => 30); @@ -1223,6 +1236,7 @@ function user_register_submit($form_id, $form_values) { $mail = $form_values['mail']; $name = $form_values['name']; $pass = $admin ? $form_values['pass'] : user_password(); + $roles = array_filter($form_values['roles']); // Remove unset roles $notify = $form_values['notify']; $from = variable_get('site_mail', ini_get('sendmail_from')); @@ -1231,7 +1245,7 @@ function user_register_submit($form_id, $form_values) { return 'user/register'; } - $account = user_save('', array_merge($form_values, array('pass' => $pass, 'init' => $mail, 'status' => ($admin || variable_get('user_register', 1) == 1)))); + $account = user_save('', array_merge($form_values, array('pass' => $pass, 'init' => $mail, 'roles' => $roles, 'status' => ($admin || variable_get('user_register', 1) == 1)))); watchdog('user', t('New user: %name %email.', array('%name' => theme('placeholder', $name), '%email' => theme('placeholder', '<'. $mail .'>'))), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit')); $variables = array('%username' => $name, '%site' => variable_get('site_name', 'drupal'), '%password' => $pass, '%uri' => $base_url, '%uri_brief' => substr($base_url, strlen('http://')), '%mailto' => $mail, '%date' => format_date(time()), '%login_uri' => url('user', NULL, NULL, TRUE), '%edit_uri' => url('user/'. $account->uid .'/edit', NULL, NULL, TRUE), '%login_url' => user_pass_reset_url($account));