Skip to content

Instantly share code, notes, and snippets.

@wwhurley
Created August 4, 2014 13:07
Show Gist options
  • Save wwhurley/45234c99cb001d5defa8 to your computer and use it in GitHub Desktop.
Save wwhurley/45234c99cb001d5defa8 to your computer and use it in GitHub Desktop.
Unblock user
/*
* Implements hook_user_operations().
*
* Creates an action to unblock a user and additionally remove information
* related to past login attempts.
*/
function mymodule_security_user_operations() {
return array(
'unblock_and_clear' => array(
'label' => t('Unblock selected users and clear failed login attempts'),
'callback' => 'mymodule_security_user_operations_unblock_and_clear',
),
);
}
/*
* Bulk user operation to unblock a user (and remove all failed login attempts).
*
* The standard Drupal unblock operation doesn't affect the flood table and
* login security state.
*/
function mymodule_security_user_operations_unblock_and_clear($accounts) {
$accounts = user_load_multiple($accounts);
foreach ($accounts as $user) {
_login_security_remove_events($user->name);
_mymodule_security_remove_login_flood($user->uid);
// set user status to 1 (active, not unblocked)
$user->status = 1;
user_save($user, array('status' => 1));
$message = t('User <strong>%name</strong> has been unblocked', array('%name' => $user->name));
drupal_set_message($message);
}
}
/*
* Remove login flood information from the DB.
*/
function _mymodule_security_remove_login_flood($uid) {
db_delete('flood')
->condition('event', 'failed_login_attempt_user')
->condition('identifier', $uid . '-%', 'LIKE')
->execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment