| 1 |
<?php |
<?php |
| 2 |
// $Id: phpass.module,v 1.1.4.1 2007/12/24 01:28:41 douggreen Exp $ |
// $Id: phpass.module,v 1.1.4.2 2008/08/29 12:18:08 douggreen Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* phpass Module |
* phpass Module |
| 13 |
*/ |
*/ |
| 14 |
function phpass_form_alter(&$form, $form_state, $form_id) { |
function phpass_form_alter(&$form, $form_state, $form_id) { |
| 15 |
// check for any login process |
// check for any login process |
| 16 |
$validate = isset($form['#validate']['user_login_validate']); |
$validate = array_search('user_login_authenticate_validate', $form['#validate']); |
| 17 |
|
|
| 18 |
// act as-if the module is not installed if SecurePass.php is not installed properly |
// act as-if the module is not installed if SecurePass.php is not installed properly |
| 19 |
if (($form_id == 'user_edit' || $form_id == 'system_modules' || $form_id == 'user_admin_settings' || $validate) && _phpass_is_passwordhash_php_missing()) { |
if (($form_id == 'user_edit' || $form_id == 'system_modules' || $form_id == 'user_admin_settings' || $validate !== FALSE) && _phpass_is_passwordhash_php_missing()) { |
| 20 |
return; |
return; |
| 21 |
} |
} |
| 22 |
|
|
| 23 |
// hook any login process |
// Replace the login authentication validator. |
| 24 |
if ($validate) { |
if ($validate !== FALSE) { |
| 25 |
$form['#validate'][$pos] = 'phpass_login_validate'; |
$form['#validate'][$validate] = 'phpass_login_authenticate_validate'; |
| 26 |
} |
} |
| 27 |
|
|
| 28 |
// hook the change password form |
// hook the change password form |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
/** |
/** |
| 96 |
* Implement hook_user to save the hash password. |
* This is a copy of user_login_authenticate_validate, that calls our validate |
|
*/ |
|
|
function phpass_user($op, &$edit, &$account, $category = NULL) { |
|
|
switch ($op) { |
|
|
case 'submit': |
|
|
_phpass_save($account->uid, $edit['pass']); |
|
|
unset($edit['pass']); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* This is a copy of user_login_validate, that calls our validate |
|
| 97 |
* instead of the default user validation. |
* instead of the default user validation. |
|
* |
|
|
* These are the only changes from user_login_validate: |
|
|
* - $user = user_authenticate($form_values['name'], trim($form_values['pass'])); |
|
|
* + global $user; |
|
|
* + $user = _phpass_user_authenticate($form_values['name'], trim($form_values['pass'])); |
|
| 98 |
*/ |
*/ |
| 99 |
function phpass_login_validate($form, &$form_state) { |
function phpass_login_authenticate_validate($form, &$form_state) { |
| 100 |
if ($form_state['values']['name']) { |
// Name and pass keys are required. |
| 101 |
if (user_is_blocked($form_state['values']['name'])) { |
$form_values = $form_state['values']; |
| 102 |
// blocked in user administration |
if (!empty($form_values['name']) && !empty($form_values['pass']) && |
| 103 |
form_set_error('name', t('The username %name has not been activated or is blocked.', array('%name' => $form_state['values']['name']))); |
$account = _phpass_user_authenticate($form_values['name'], trim($form_values['pass']))) { |
| 104 |
} |
global $user; |
| 105 |
else if (drupal_is_denied('user', $form_state['values']['name'])) { |
$user = $account; |
| 106 |
// denied by access controls |
user_authenticate_finalize($form_values); |
| 107 |
form_set_error('name', t('The name %name is a reserved username.', array('%name' => $form_state['values']['name']))); |
return $user; |
|
} |
|
|
else if ($form_state['values']['pass']) { |
|
|
global $user; |
|
|
$user = _phpass_user_authenticate($form_state['values']['name'], trim($form_state['values']['pass'])); |
|
|
|
|
|
if (!$user->uid) { |
|
|
form_set_error('name', t('Sorry, unrecognized username or password. <a href="@password">Have you forgotten your password?</a>', array('@password' => url('user/password')))); |
|
|
watchdog('user', 'Login attempt failed for %user.', array('%user' => $form_state['values']['name'])); |
|
|
} |
|
|
} |
|
| 108 |
} |
} |
| 109 |
} |
} |
| 110 |
|
|
| 157 |
return user_authenticate($user, $pass); |
return user_authenticate($user, $pass); |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
|
function _phpass_load(&$user) { |
| 161 |
|
$user->roles = array(); |
| 162 |
|
if ($user->uid) { |
| 163 |
|
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; |
| 164 |
|
} |
| 165 |
|
else { |
| 166 |
|
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; |
| 167 |
|
} |
| 168 |
|
$result = db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d', $user->uid); |
| 169 |
|
while ($role = db_fetch_object($result)) { |
| 170 |
|
$user->roles[$role->rid] = $role->name; |
| 171 |
|
} |
| 172 |
|
$array = array(); |
| 173 |
|
user_module_invoke('load', $array, $user); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
function _phpass_save($uid, $pass) { |
function _phpass_save($uid, $pass) { |
| 177 |
// initialize phpass |
// initialize phpass |
| 178 |
require_once(drupal_get_path('module', 'phpass') .'/PasswordHash.php'); |
require_once(drupal_get_path('module', 'phpass') .'/PasswordHash.php'); |