| 1 |
<?php
|
| 2 |
// $Id: alt_login.module,v 1.17 2009/10/06 03:34:55 thehunmonkgroup Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implement hook_help().
|
| 6 |
*/
|
| 7 |
function alt_login_help($path, $arg) {
|
| 8 |
switch ($path) {
|
| 9 |
case 'admin/help#alt_login':
|
| 10 |
$output = t('
|
| 11 |
<p>This module provides an interface that allows registered users to use a login name which is different than their username.</p>
|
| 12 |
<p>To use, simply enable the module, then visit the user edit page. Enter the alternate login name in the \'Alternate Login\' textfield, and save.</p>
|
| 13 |
<p><em>Note that users can still login with their normal username--this just adds the option of another login name. Also note that an alternate login name may not be equivalent to any other current alternate login name, nor any current username.</em></p>');
|
| 14 |
return $output;
|
| 15 |
break;
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implement hook_permission().
|
| 21 |
*/
|
| 22 |
function alt_login_permission() {
|
| 23 |
return array(
|
| 24 |
'create alternate login' => array(
|
| 25 |
'title' => t('Create alternate login'),
|
| 26 |
'description' => t('Allow users to create an alternate login name.'),
|
| 27 |
),
|
| 28 |
);
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implement hook_form_alter().
|
| 33 |
*/
|
| 34 |
function alt_login_form_alter(&$form, &$form_state, $form_id) {
|
| 35 |
|
| 36 |
switch ($form_id) {
|
| 37 |
case 'user_login':
|
| 38 |
case 'user_login_block':
|
| 39 |
case 'user_pass':
|
| 40 |
// This validation happens before the main login validation.
|
| 41 |
$form['name']['#element_validate'][] = '_alt_login_username_validate';
|
| 42 |
break;
|
| 43 |
case 'user_register':
|
| 44 |
if (variable_get('alt_login_user_registration', 1)) {
|
| 45 |
$form['alt_login'] = array('#type' => 'textfield',
|
| 46 |
'#title' => t('Alternate Login'),
|
| 47 |
'#maxlength' => USERNAME_MAX_LENGTH,
|
| 48 |
'#description' => t('If you wish to provide another valid login name, enter it here: only letters, numbers and spaces are allowed.'),
|
| 49 |
'#weight' => 29, // Last field before the submit button.
|
| 50 |
);
|
| 51 |
// Put the form field in the account fieldset if it exists.
|
| 52 |
if (isset($form['account'])) {
|
| 53 |
$form['account']['alt_login'] = $form['alt_login'];
|
| 54 |
unset($form['alt_login']);
|
| 55 |
}
|
| 56 |
}
|
| 57 |
break;
|
| 58 |
case 'user_admin_settings':
|
| 59 |
$form['registration_cancellation']['alt_login_user_registration'] = array(
|
| 60 |
'#type' => 'checkbox',
|
| 61 |
'#title' => t('Set alternate login on registration'),
|
| 62 |
'#default_value' => variable_get('alt_login_user_registration', 1),
|
| 63 |
'#description' => t('If selected, new users will be allowed to set an alternate login name as part of the user registration process.'),
|
| 64 |
);
|
| 65 |
break;
|
| 66 |
case 'user_profile_form':
|
| 67 |
if ($form['#user_category'] == 'account') {
|
| 68 |
$user = $form['#user'];
|
| 69 |
// Grab the current alt login if it exists.
|
| 70 |
$alt_login = db_query('SELECT alt_login FROM {alt_login} WHERE uid = :uid', array(
|
| 71 |
':uid' => $user->uid,
|
| 72 |
))->fetchField();
|
| 73 |
$alt_login = $alt_login ? $alt_login : '';
|
| 74 |
|
| 75 |
// Permissioned users can create/edit their own alt login.
|
| 76 |
if (user_access('create alternate login')) {
|
| 77 |
$form['account']['alt_login'] = array(
|
| 78 |
'#type' => 'textfield',
|
| 79 |
'#title' => t('Alternate Login'),
|
| 80 |
'#maxlength' => USERNAME_MAX_LENGTH,
|
| 81 |
'#description' => t('If you wish to provide another valid login name, enter it here: only letters, numbers and spaces are allowed.'),
|
| 82 |
'#default_value' => $alt_login,
|
| 83 |
);
|
| 84 |
}
|
| 85 |
// For non-permissioned users, display their alt login if it exists.
|
| 86 |
elseif ($alt_login) {
|
| 87 |
$form['account']['alt_login'] = array(
|
| 88 |
'#type' => 'item',
|
| 89 |
'#title' => t('Alternate Login'),
|
| 90 |
'#description' => t('You may also login with this alternate username.'),
|
| 91 |
'#markup' => $alt_login,
|
| 92 |
);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Implement hook_user_load().
|
| 101 |
*/
|
| 102 |
function alt_login_user_load($users) {
|
| 103 |
$result = db_query('SELECT uid, alt_login FROM {alt_login} WHERE uid IN (:uids)', array(
|
| 104 |
':uids' => array_keys($users),
|
| 105 |
));
|
| 106 |
foreach ($result as $record) {
|
| 107 |
$users[$record->uid]->alt_login = $record->alt_login;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Implement hook_user_cancel().
|
| 113 |
*/
|
| 114 |
function alt_login_user_cancel($edit, $user, $method) {
|
| 115 |
switch ($method) {
|
| 116 |
case 'user_cancel_reassign':
|
| 117 |
case 'user_cancel_delete':
|
| 118 |
db_delete('alt_login')
|
| 119 |
->condition('uid', $user->uid)
|
| 120 |
->execute();
|
| 121 |
break;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implement hook_user_validate().
|
| 127 |
*/
|
| 128 |
function alt_login_user_validate(&$edit, $user, $category = NULL) {
|
| 129 |
$uid = isset($user->uid) ? $user->uid : NULL;
|
| 130 |
$alt_login = isset($edit['alt_login']) ? $edit['alt_login'] : NULL;
|
| 131 |
|
| 132 |
// Make sure user can't set their username to an existing alt_login name.
|
| 133 |
if (db_query("SELECT alt_login FROM {alt_login} WHERE alt_login = :alt_login AND uid != :uid", array(
|
| 134 |
':alt_login' => $edit['name'],
|
| 135 |
':uid' => $uid,
|
| 136 |
))->fetchField()) {
|
| 137 |
form_set_error('name', t('The name %name is already in use.', array('%name' => $edit['name'])));
|
| 138 |
}
|
| 139 |
|
| 140 |
if ($alt_login) {
|
| 141 |
// Can't have same username and alt login name.
|
| 142 |
if ($edit['name'] == $alt_login) {
|
| 143 |
form_set_error('alt_login', t('Username and Alternate Login cannot be the same.'));
|
| 144 |
}
|
| 145 |
|
| 146 |
// Make sure the name isn't already taken as either another alt login or username.
|
| 147 |
if (db_query("SELECT uid FROM {alt_login} WHERE alt_login = :alt_login AND uid != :uid", array(':alt_login' => $alt_login, ':uid' => $uid))->fetchField() || db_query("SELECT uid FROM {users} WHERE name = :name AND uid != :uid", array(':name' => $alt_login, ':uid' => $uid))->fetchField()) {
|
| 148 |
form_set_error('alt_login', t('The name %name is already in use.', array('%name' => $alt_login)));
|
| 149 |
}
|
| 150 |
|
| 151 |
// Same validation as the regular username.
|
| 152 |
if ($error = user_validate_name($alt_login)) {
|
| 153 |
form_set_error('alt_login', $error);
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Implement hook_user_update().
|
| 160 |
*/
|
| 161 |
function alt_login_user_update(&$edit, $user, $category = NULL) {
|
| 162 |
if ($category == 'account') {
|
| 163 |
// Only proceed if some alt_login value exists.
|
| 164 |
if (isset($edit['alt_login'])) {
|
| 165 |
$alt_login = $edit['alt_login'];
|
| 166 |
}
|
| 167 |
else {
|
| 168 |
return;
|
| 169 |
}
|
| 170 |
|
| 171 |
$uid = $user->uid;
|
| 172 |
|
| 173 |
// Only keep a database entry if an alternate login has been specified.
|
| 174 |
if ($alt_login) {
|
| 175 |
if (db_query('SELECT uid FROM {alt_login} WHERE uid = :uid', array(
|
| 176 |
':uid' => $uid,
|
| 177 |
))->fetchField()) {
|
| 178 |
db_update('alt_login')
|
| 179 |
->fields(array(
|
| 180 |
'alt_login' => $alt_login,
|
| 181 |
))
|
| 182 |
->condition('uid', $uid)
|
| 183 |
->execute();
|
| 184 |
}
|
| 185 |
else {
|
| 186 |
db_insert('alt_login')
|
| 187 |
->fields(array(
|
| 188 |
'uid' => $uid,
|
| 189 |
'alt_login' => $alt_login,
|
| 190 |
))
|
| 191 |
->execute();
|
| 192 |
}
|
| 193 |
}
|
| 194 |
// Delete the alt login if it was set to an empty string.
|
| 195 |
elseif ($alt_login === '') {
|
| 196 |
db_delete('alt_login')
|
| 197 |
->condition('uid', $uid)
|
| 198 |
->execute();
|
| 199 |
}
|
| 200 |
// Don't want this saved in the data column of the users table, so unset.
|
| 201 |
unset($edit['alt_login']);
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Implement hook_user_insert().
|
| 207 |
*/
|
| 208 |
function alt_login_user_insert(&$edit, $user, $category = NULL) {
|
| 209 |
$alt_login = isset($edit['alt_login']) ? $edit['alt_login'] : NULL;
|
| 210 |
// Only insert a row if the alt login exists.
|
| 211 |
if ($alt_login) {
|
| 212 |
db_insert('alt_login')
|
| 213 |
->fields(array(
|
| 214 |
'uid' => $user->uid,
|
| 215 |
'alt_login' => $alt_login,
|
| 216 |
))
|
| 217 |
->execute();
|
| 218 |
}
|
| 219 |
unset($edit['alt_login']);
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
* Checks to see if a valid alt login was used, and converts it to the real username if so.
|
| 224 |
*
|
| 225 |
* @param $form Form element being validated.
|
| 226 |
* @param $form_state
|
| 227 |
*/
|
| 228 |
function _alt_login_username_validate($form, &$form_state) {
|
| 229 |
$name = $form['#value'];
|
| 230 |
$username = db_query("SELECT u.name FROM {users} u INNER JOIN {alt_login} al ON u.uid = al.uid WHERE al.alt_login = :alt_login", array(
|
| 231 |
':alt_login' => $name,
|
| 232 |
))->fetchField();
|
| 233 |
if ($username) {
|
| 234 |
form_set_value($form, $username, $form_state);
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|