Index: auto_username.module =================================================================== --- auto_username.module (revision 1994) +++ auto_username.module (working copy) @@ -49,7 +49,14 @@ '#title' => t('Other options'), '#collapsible' => TRUE, '#collapsed' => FALSE, - ); + ); + $form['aun_settings']['aun_exclude_roles'] = array( + '#type' => 'checkboxes', + '#options' => user_roles(true), + '#title' => t('Exclusion Roles'), + '#default_value' => variable_get('aun_exclude_roles', ''), + '#description' => t("Defines roles which will NOT be affected by auto_username."), + ); $form['aun_settings']['aun_php'] = array( '#type' => 'checkbox', '#title' => t('Evaluate PHP in pattern.'), @@ -121,7 +128,7 @@ */ function auto_username_user($op, &$edit, &$account, $category = NULL) { static $new_name; - + switch ($op) { case 'validate': $new_name = _auto_username_patternprocessor($account); @@ -144,10 +151,16 @@ break; case 'insert': + // if in one of our Exclusionary roles - do nothing + if (_aun_check_role($account)) break; + db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array($new_name, $account->uid)); break; case 'after_update': + // if in one of our Exclusionary roles - do nothing + if (_aun_check_role($account)) break; + // Only process on update if we're configured to do so. // We have to use after_update here instead of the 'update' op because // update happens before user module does its own saving. Anything we do @@ -306,7 +319,7 @@ * Implementation of hook_form_alter(). */ function auto_username_form_alter($form_id, &$form) { - if ('user_register' == $form_id) { + if ($form_id == 'user_register') { // Fake a real user name submission, because we have to validate against // the bulit-in username validation even though we're going to change the // name anyway. @@ -320,7 +333,11 @@ $form['#submit']['auto_username_user_register_submit'] = array(); } - if ('user_edit' == $form_id && variable_get('aun_update_on_edit', 1)) { + if ($form_id == 'user_edit' && variable_get('aun_update_on_edit', 1)) { + // if in one of our Exclusionary roles - do nothing + $account = user_load(array('uid'=>arg(1))); + if (_aun_check_role($account)) return; + // The username may not be editable. if (isset($form['account']['name'])) { $form['account']['name'] = array( @@ -453,3 +470,15 @@ } } +// helper function to check if user belongs to exclusionary role +function _aun_check_role(&$account) { + // if in one of our Exclusionary roles - return now + $roles = variable_get('aun_exclude_roles', 0); + foreach ($roles as $key => $role) if (!$role) unset($roles[$key]); + + foreach ($account->roles as $roleid => $name) { + if (in_array($roleid, $roles)) return true; + } + + return false; +} \ No newline at end of file