| 1 |
<?php |
<?php |
| 2 |
// $Id: alt_login.module,v 1.15 2008/02/17 15:02:33 thehunmonkgroup Exp $ |
// $Id: alt_login.module,v 1.16 2009/07/27 17:00:58 thehunmonkgroup Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_help() |
* Implement hook_help(). |
| 6 |
*/ |
*/ |
| 7 |
function alt_login_help($path, $arg) { |
function alt_login_help($path, $arg) { |
| 8 |
switch ($path) { |
switch ($path) { |
| 17 |
} |
} |
| 18 |
|
|
| 19 |
/** |
/** |
| 20 |
* Implementation of hook_perm(). |
* Implement hook_permission(). |
| 21 |
*/ |
*/ |
| 22 |
function alt_login_perm() { |
function alt_login_permission() { |
| 23 |
return array('create alternate login'); |
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 |
* Implementation of hook_form_alter() |
* Implement hook_form_alter(). |
| 33 |
*/ |
*/ |
| 34 |
function alt_login_form_alter(&$form, $form_state, $form_id) { |
function alt_login_form_alter(&$form, &$form_state, $form_id) { |
| 35 |
|
|
| 36 |
switch ($form_id) { |
switch ($form_id) { |
| 37 |
case 'user_login': |
case 'user_login': |
| 59 |
} |
} |
| 60 |
break; |
break; |
| 61 |
case 'user_admin_settings': |
case 'user_admin_settings': |
| 62 |
$form['registration']['alt_login_user_registration'] = array( |
$form['registration_cancellation']['alt_login_user_registration'] = array( |
| 63 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 64 |
'#title' => t('Set alternate login on registration'), |
'#title' => t('Set alternate login on registration'), |
| 65 |
'#default_value' => variable_get('alt_login_user_registration', 1), |
'#default_value' => variable_get('alt_login_user_registration', 1), |
| 66 |
'#description' => t('If selected, new users will be allowed to set an alternate login name as part of the user registration process.'), |
'#description' => t('If selected, new users will be allowed to set an alternate login name as part of the user registration process.'), |
| 67 |
); |
); |
| 68 |
break; |
break; |
| 69 |
|
case 'user_profile_form': |
| 70 |
|
if ($form['#user_category'] == 'account') { |
| 71 |
|
$user = $form['#user']; |
| 72 |
|
// Grab the current alt login if it exists. |
| 73 |
|
$alt_login = db_query('SELECT alt_login FROM {alt_login} WHERE uid = :uid', array( |
| 74 |
|
':uid' => $user->uid, |
| 75 |
|
))->fetchField(); |
| 76 |
|
$alt_login = $alt_login ? $alt_login : ''; |
| 77 |
|
|
| 78 |
|
// Permissioned users can create/edit their own alt login. |
| 79 |
|
if (user_access('create alternate login')) { |
| 80 |
|
$form['account']['alt_login'] = array( |
| 81 |
|
'#type' => 'textfield', |
| 82 |
|
'#title' => t('Alternate Login'), |
| 83 |
|
'#maxlength' => USERNAME_MAX_LENGTH, |
| 84 |
|
'#description' => t('If you wish to provide another valid login name, enter it here: only letters, numbers and spaces are allowed.'), |
| 85 |
|
'#default_value' => $alt_login, |
| 86 |
|
); |
| 87 |
|
} |
| 88 |
|
// For non-permissioned users, display their alt login if it exists. |
| 89 |
|
elseif ($alt_login) { |
| 90 |
|
$form['account']['alt_login'] = array( |
| 91 |
|
'#type' => 'item', |
| 92 |
|
'#title' => t('Alternate Login'), |
| 93 |
|
'#description' => t('You may also login with this alternate username.'), |
| 94 |
|
'#markup' => $alt_login, |
| 95 |
|
); |
| 96 |
|
} |
| 97 |
|
} |
| 98 |
|
break; |
| 99 |
} |
} |
| 100 |
} |
} |
| 101 |
|
|
| 102 |
/** |
/** |
| 103 |
* Implementation of hook_user(). |
* Implement hook_user_load(). |
| 104 |
*/ |
*/ |
| 105 |
function alt_login_user($type, &$edit, &$user, $category = NULL) { |
function alt_login_user_load($users) { |
| 106 |
|
$result = db_query('SELECT uid, alt_login FROM {alt_login} WHERE uid IN (:uids)', array( |
| 107 |
if ($type == 'load') { |
':uids' => array_keys($users), |
| 108 |
return _alt_login_load($user); |
)); |
| 109 |
} |
foreach ($result as $record) { |
| 110 |
if ($type == 'delete') { |
$users[$record->uid]->alt_login = $record->alt_login; |
|
return _alt_login_delete($user->uid); |
|
|
} |
|
|
if ($type == 'validate') { |
|
|
$uid = isset($user->uid) ? $user->uid : NULL; |
|
|
return _alt_login_validate($uid, $edit); |
|
|
} |
|
|
if ($category == 'account' && $type == 'update') { |
|
|
return _alt_login_update($user->uid, $edit); |
|
|
} |
|
|
if ($type == 'insert') { |
|
|
return _alt_login_register_submit($user->uid, $edit); |
|
|
} |
|
|
if ($category == 'account' && $type == 'form') { |
|
|
return _alt_login_form($user, $edit); |
|
| 111 |
} |
} |
| 112 |
} |
} |
| 113 |
|
|
| 114 |
/** |
/** |
| 115 |
* Loads the user's alt_login to the user object. |
* Implement hook_user_cancel(). |
|
* |
|
|
* @param $user The user object. |
|
| 116 |
*/ |
*/ |
| 117 |
function _alt_login_load(&$user) { |
function alt_login_user_cancel($edit, $user, $method) { |
| 118 |
if ($alt_login = db_result(db_query('SELECT alt_login FROM {alt_login} WHERE uid = %d', $user-> uid))) { |
switch ($method) { |
| 119 |
$user->alt_login = $alt_login; |
case 'user_cancel_reassign': |
| 120 |
|
case 'user_cancel_delete': |
| 121 |
|
db_delete('alt_login') |
| 122 |
|
->condition('uid', $user->uid) |
| 123 |
|
->execute(); |
| 124 |
|
break; |
| 125 |
} |
} |
| 126 |
} |
} |
| 127 |
|
|
| 128 |
/** |
/** |
| 129 |
* Validates the alt login name if entered. |
* Implement hook_user_validate(). |
|
* |
|
|
* @param $uid The user ID of the specified user. |
|
|
* @param $edit Posted data. |
|
| 130 |
*/ |
*/ |
| 131 |
function _alt_login_validate($uid, $edit) { |
function alt_login_user_validate(&$edit, $user, $category = NULL) { |
| 132 |
|
$uid = isset($user->uid) ? $user->uid : NULL; |
| 133 |
$alt_login = isset($edit['alt_login']) ? $edit['alt_login'] : NULL; |
$alt_login = isset($edit['alt_login']) ? $edit['alt_login'] : NULL; |
| 134 |
|
|
| 135 |
// Make sure user can't set their username to an existing alt_login name. |
// Make sure user can't set their username to an existing alt_login name. |
| 136 |
if (db_result(db_query("SELECT alt_login FROM {alt_login} WHERE alt_login = '%s' and uid != %d", $edit['name'], $uid))) { |
if (db_query("SELECT alt_login FROM {alt_login} WHERE alt_login = :alt_login AND uid != :uid", array( |
| 137 |
|
':alt_login' => $edit['name'], |
| 138 |
|
':uid' => $uid, |
| 139 |
|
))->fetchField()) { |
| 140 |
form_set_error('name', t('The name %name is already in use.', array('%name' => $edit['name']))); |
form_set_error('name', t('The name %name is already in use.', array('%name' => $edit['name']))); |
| 141 |
} |
} |
| 142 |
|
|
| 147 |
} |
} |
| 148 |
|
|
| 149 |
// Make sure the name isn't already taken as either another alt login or username. |
// Make sure the name isn't already taken as either another alt login or username. |
| 150 |
if (db_result(db_query("SELECT uid FROM {alt_login} WHERE alt_login = '%s' AND uid != %d", $alt_login, $uid)) || db_result(db_query("SELECT uid FROM {users} WHERE name = '%s' AND uid != %d", $alt_login, $uid))) { |
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()) { |
| 151 |
form_set_error('alt_login', t('The name %name is already in use.', array('%name' => $alt_login))); |
form_set_error('alt_login', t('The name %name is already in use.', array('%name' => $alt_login))); |
| 152 |
} |
} |
| 153 |
|
|
| 159 |
} |
} |
| 160 |
|
|
| 161 |
/** |
/** |
| 162 |
* Checks for alt login name on password reset requests. |
* Implement hook_user_update(). |
|
* |
|
|
* @param $form The form array. |
|
| 163 |
*/ |
*/ |
| 164 |
function _alt_login_pass_validate($form, &$form_state) { |
function alt_login_user_update(&$edit, $user, $category = NULL) { |
| 165 |
if ($name = db_result(db_query("SELECT u.name FROM {users} u INNER JOIN {alt_login} a ON u.uid = a.uid WHERE a.alt_login = '%s'", $form['#value']))) { |
if ($category == 'account') { |
| 166 |
form_set_value($form, $name, $form_state); |
// Only proceed if some alt_login value exists. |
| 167 |
} |
if (isset($edit['alt_login'])) { |
| 168 |
} |
$alt_login = $edit['alt_login']; |
| 169 |
|
} |
| 170 |
|
else { |
| 171 |
|
return; |
| 172 |
|
} |
| 173 |
|
|
| 174 |
/** |
$uid = $user->uid; |
|
* Updates the alt login info for the specified user. |
|
|
* |
|
|
* @param $uid The user ID of the user. |
|
|
* @param $edit Posted data. |
|
|
*/ |
|
|
function _alt_login_update($uid, &$edit) { |
|
|
// Only proceed if some alt_login value exists. |
|
|
if (isset($edit['alt_login'])) { |
|
|
$alt_login = $edit['alt_login']; |
|
|
} |
|
|
else { |
|
|
return; |
|
|
} |
|
| 175 |
|
|
| 176 |
// Only keep a database entry if an alternate login has been specified. |
// Only keep a database entry if an alternate login has been specified. |
| 177 |
if ($alt_login) { |
if ($alt_login) { |
| 178 |
if (db_result(db_query('SELECT uid FROM {alt_login} WHERE uid = %d', $uid))) { |
if (db_query('SELECT uid FROM {alt_login} WHERE uid = :uid', array( |
| 179 |
db_query("UPDATE {alt_login} SET alt_login = '%s' WHERE uid = %d", $alt_login, $uid); |
':uid' => $uid, |
| 180 |
|
))->fetchField()) { |
| 181 |
|
db_update('alt_login') |
| 182 |
|
->fields(array( |
| 183 |
|
'alt_login' => $alt_login, |
| 184 |
|
)) |
| 185 |
|
->condition('uid', $uid) |
| 186 |
|
->execute(); |
| 187 |
|
} |
| 188 |
|
else { |
| 189 |
|
db_insert('alt_login') |
| 190 |
|
->fields(array( |
| 191 |
|
'uid' => $uid, |
| 192 |
|
'alt_login' => $alt_login, |
| 193 |
|
)) |
| 194 |
|
->execute(); |
| 195 |
|
} |
| 196 |
} |
} |
| 197 |
else { |
// Delete the alt login if it was set to an empty string. |
| 198 |
db_query("INSERT INTO {alt_login} (uid, alt_login) VALUES (%d, '%s')", $uid, $alt_login); |
elseif ($alt_login === '') { |
| 199 |
|
db_delete('alt_login') |
| 200 |
|
->condition('uid', $uid) |
| 201 |
|
->execute(); |
| 202 |
} |
} |
| 203 |
|
// Don't want this saved in the data column of the users table, so unset. |
| 204 |
|
unset($edit['alt_login']); |
| 205 |
} |
} |
|
// Delete the alt login if it was set to an empty string. |
|
|
elseif ($alt_login === '') { |
|
|
db_query('DELETE FROM {alt_login} WHERE uid = %d', $uid); |
|
|
} |
|
|
// Don't want this saved in the data column of the users table, so unset. |
|
|
unset($edit['alt_login']); |
|
| 206 |
} |
} |
| 207 |
|
|
| 208 |
/** |
/** |
| 209 |
* Deletes alt login info for a user if the user is deleted. |
* Implement hook_user_insert(). |
|
* |
|
|
* @param $uid The user ID of the user being deleted. |
|
| 210 |
*/ |
*/ |
| 211 |
function _alt_login_delete($uid) { |
function alt_login_user_insert(&$edit, $user, $category = NULL) { |
| 212 |
db_query('DELETE FROM {alt_login} WHERE uid = %d', $uid); |
$alt_login = isset($edit['alt_login']) ? $edit['alt_login'] : NULL; |
| 213 |
|
// Only insert a row if the alt login exists. |
| 214 |
|
if ($alt_login) { |
| 215 |
|
db_insert('alt_login') |
| 216 |
|
->fields(array( |
| 217 |
|
'uid' => $user->uid, |
| 218 |
|
'alt_login' => $alt_login, |
| 219 |
|
)) |
| 220 |
|
->execute(); |
| 221 |
|
} |
| 222 |
|
unset($edit['alt_login']); |
| 223 |
} |
} |
| 224 |
|
|
| 225 |
/** |
/** |
| 226 |
* Generates an alt login form field. |
* Checks for alt login name on password reset requests. |
| 227 |
* |
* |
| 228 |
* @param $user The user to generate the field for. |
* @param $form Form element being validated. |
| 229 |
* @param $edit An array of form data. |
* @param $form_state |
| 230 |
* @return An array representing the form field. |
*/ |
| 231 |
*/ |
function _alt_login_pass_validate($form, &$form_state) { |
| 232 |
function _alt_login_form($user, &$edit) { |
if ($name = db_query("SELECT u.name FROM {users} u INNER JOIN {alt_login} a ON u.uid = a.uid WHERE a.alt_login = :alt_login", array( |
| 233 |
|
':alt_login' => $form['#value'], |
| 234 |
$form = array(); |
))->fetchField()) { |
| 235 |
|
form_set_value($form, $name, $form_state); |
|
// Grab the current alt login if it exists. |
|
|
$alt_login = db_result(db_query('SELECT alt_login FROM {alt_login} WHERE uid = %d', $user->uid)); |
|
|
$alt_login = $alt_login ? $alt_login : ''; |
|
|
|
|
|
// Permissioned users can create/edit their own alt login. |
|
|
if (user_access('create alternate login')) { |
|
|
$form['account']['alt_login'] = array('#type' => 'textfield', |
|
|
'#title' => t('Alternate Login'), |
|
|
'#maxlength' => USERNAME_MAX_LENGTH, |
|
|
'#description' => t('If you wish to provide another valid login name, enter it here: only letters, numbers and spaces are allowed.'), |
|
|
'#default_value' => $alt_login, |
|
|
); |
|
|
} |
|
|
// For non-permissioned users, display their alt login if it exists. |
|
|
elseif ($alt_login) { |
|
|
$form['account']['alt_login'] = array('#type' => 'item', |
|
|
'#title' => t('Alternate Login'), |
|
|
'#description' => t('You may also login with this alternate username.'), |
|
|
'#value' => $alt_login, |
|
|
); |
|
| 236 |
} |
} |
|
|
|
|
return $form; |
|
| 237 |
} |
} |
| 238 |
|
|
| 239 |
/** |
/** |
| 240 |
* Checks to see if a valid alt login was used, and converts it to the real username if so. |
* Checks to see if a valid alt login was used, and converts it to the real username if so. |
| 241 |
* |
* |
| 242 |
* @param $form Form element being validated. |
* @param $form Form element being validated. |
| 243 |
|
* @param $form_state |
| 244 |
*/ |
*/ |
| 245 |
function _alt_login_login_validate($form, &$form_state) { |
function _alt_login_login_validate($form, &$form_state) { |
| 246 |
$name = $form['#value']; |
$name = $form['#value']; |
| 247 |
$username = db_fetch_object(db_query("SELECT u.name FROM {users} u INNER JOIN {alt_login} al ON u.uid = al.uid WHERE al.alt_login = '%s'", $name)); |
$username = db_fetch_object(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( |
| 248 |
|
':alt_login' => $name, |
| 249 |
|
))); |
| 250 |
if ($username->name) { |
if ($username->name) { |
| 251 |
form_set_value($form, $username->name, $form_state); |
form_set_value($form, $username->name, $form_state); |
| 252 |
} |
} |
| 253 |
} |
} |
| 254 |
|
|
|
/** |
|
|
* Adds an alt login for a new user if it was entered. |
|
|
* |
|
|
* @param $uid The user ID of the new user. |
|
|
* @param $edit Posted values from registration. |
|
|
*/ |
|
|
function _alt_login_register_submit($uid, &$edit) { |
|
|
$alt_login = isset($edit['alt_login']) ? $edit['alt_login'] : NULL; |
|
|
// Only insert a row if the alt login exists. |
|
|
if ($alt_login) { |
|
|
db_query("INSERT INTO {alt_login} (uid, alt_login) VALUES (%d, '%s')", $uid, $alt_login); |
|
|
} |
|
|
unset($edit['alt_login']); |
|
|
} |
|