Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow administrators to create Raven-only users #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions raven.module
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,66 @@ function raven_signature_decode($str) {
return $result;
}

/**
* Implements hook_form_FORM_ID_alter() for user_register_form().
*/
function raven_form_user_register_form_alter(&$form, &$form_state, $form_id) {
if (user_access('administer users') === FALSE) {
return;
}

$form['account']['raven_only'] = array(
'#type' => 'radios',
'#title' => 'Raven-only user',
'#default_value' => variable_get('raven_login_override'),
'#options' => array(
0 => 'No',
1 => 'Yes',
),
'#access' => 1,
'#weight' => $form['account']['name']['#weight'] - 1,
);

// We have to wrap the password in a container to be able to hide it (see https://drupal.org/node/1427838)
$firstArray = array_splice($form['account'], 0, array_search('pass', array_keys($form['account'])));
$form['account'] = array_merge($firstArray, array(
'pass_wrapper' => array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name="raven_only"]' => array('value' => 0),
),
),
'pass' => $form['account']['pass'],
)
), $form['account']);
unset($form['account']['pass']);

// Password isn't required if Raven-only is selected
$form['account']['pass_wrapper']['pass']['#required'] = isset($form_state['input']['raven_only']) ? $form_state['input']['raven_only'] == 0 : TRUE;

array_unshift($form['#submit'], 'raven_user_register_submit_empty_password');
$form['#submit'][] = 'raven_user_register_submit_authmap';
}

/**
* Submit handler for the user_register_form which adds a random password if it's empty.
*/
function raven_user_register_submit_empty_password($form, &$form_state) {
if ($form_state['values']['pass'] === '') {
$form_state['values']['pass'] = user_password();
}
}

/**
* Submit handler for the user_register_form which adds an authmap entry for Raven-only users.
*/
function raven_user_register_submit_authmap($form, &$form_state) {
if ($form_state['values']['raven_only'] == 1) {
user_set_authmaps($form_state['user'], array('authname_raven' => $form_state['values']['name']));
}
}

/**
* Implements hook_form_FORM_ID_alter() for user_profile_form().
*
Expand Down