Skip to content

Instantly share code, notes, and snippets.

@Sanchiz
Last active December 29, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanchiz/7627657 to your computer and use it in GitHub Desktop.
Save Sanchiz/7627657 to your computer and use it in GitHub Desktop.
usertools.drush.inc Examples of Drush extension.
<?php
/**
* @file
* User tools.
*/
/**
* Implements hook_drush_command().
*/
function usertools_drush_command() {
$items = array();
$items['last-registration'] = array(
'description' => 'Show last registration.',
'arguments' => array(
'role' => 'Specific role.',
),
'options' => array(
'count' => array(
'description' => 'Count rows.',
'example-value' => '5',
),
),
'examples' => array(
'drush utl admin --count=5' => 'Show the last 5 registrations of admin.',
),
'aliases' => array('utl'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
$items['wipe-users'] = array(
'description' => 'Wipe user tables.',
'examples' => array(
'drush utw' => 'Wipe users.',
),
'aliases' => array('utw'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
);
$items['disable-role'] = array(
'description' => 'Disable role.',
'examples' => array(
'drush utdr' => 'Disable role.',
),
'aliases' => array('utdr'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
);
return $items;
}
/**
* Implements hook_drush_help().
*/
function usertools_drush_help($section) {
switch ($section) {
case 'drush:last-registration':
return dt('This command will show you last registrations.');
}
}
/**
* Implements drush_hook_COMMAND_validate().
*/
function drush_usertools_last_registration_validate($role = 'all') {
$count = drush_get_option('count');
if (!empty($count) && !is_numeric($count)) {
return drush_set_error('Count should be numeric.');
}
}
/**
* Show last registrations.
*/
function drush_usertools_last_registration($role = 'all') {
$count = drush_get_option('count');
if (empty($count)) {
$count = 10;
}
$query = db_select('users', 'u');
// Condition by role.
if ($role != 'all') {
$rid = drush_usertools_get_role_by_name($role);
$query->innerJoin('users_roles', 'ur', 'u.uid = ur.uid');
$query->condition('ur.rid', $rid);
}
$query->fields('u', array('uid', 'name', 'mail', 'access', 'created'));
$query->condition('u.uid', 0, '<>');
$query->range(0, $count);
$query->orderBy('u.created', 'DESC');
$result = $query->execute();
$items[] = array('uid', dt('Name'), dt('email'), dt('Last login'));
foreach ($result as $user) {
$items[] = array(
$user->uid,
$user->name,
$user->mail,
$user->access == 0 ? dt('Never') : format_interval(time() - $user->access) . ' ago',
);
}
drush_print_table($items, TRUE);
}
/**
* Return rid by name.
*/
function drush_usertools_get_role_by_name($name) {
$roles = user_roles();
return array_search($name, $roles);
}
/**
* Wipe users.
*/
function drush_usertools_wipe_users() {
if (drush_confirm(dt('You want to delete all users? Are you sure?'))) {
$query = db_select('users', 'u');
$query->fields('u', array('uid', 'name'));
$query->condition('u.uid', 1, '>');
$result = $query->execute();
foreach ($result as $user) {
drush_log(dt('Deleting user') . ' ' . $user->name . '...', 'warning');
user_delete($user->uid);
drush_log(dt('Succes deleting') . ' ' . $user->name, 'success');
}
}
else {
drush_user_abort();
}
}
/**
* Disable users..
*/
function drush_usertools_disable_role() {
$query = db_select('role', 'r');
$query->fields('r', array('rid', 'name'));
$query->condition('r.rid', 1, '<>');
$query->orderBy('r.rid');
$result = $query->execute();
$options = array();
foreach ($result as $role) {
$options[$role->rid] = array($role->name);
}
$rid = drush_choice($options, dt('Select role:'));
// SQL queries.
drush_log('All users of role ' . $options[$rid][0] . ' disabled.', 'success');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment