You are here

function _security_review_weak_passwords in Security Review 7

Same name and namespace in other branches
  1. 6 security_review.inc \_security_review_weak_passwords()
1 call to _security_review_weak_passwords()
security_review_check_name_passwords in ./security_review.inc

File

./security_review.inc, line 593
Stand-alone security checks and review system.

Code

function _security_review_weak_passwords($trusted_roles) {
  require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  $weak_users = array();

  // Select users with a trusted role.
  $query = db_select('users', 'u');
  $query
    ->leftJoin('users_roles', 'ur', 'u.uid = ur.uid AND ur.rid IN (:rids)', array(
    ':rids' => $trusted_roles,
  ));
  $query
    ->fields('u', array(
    'uid',
    'name',
    'pass',
  ));
  $query
    ->addExpression('COUNT(rid)', 'count');
  $query
    ->groupBy('u.uid');
  $query
    ->groupBy('u.name');
  $query
    ->groupBy('u.pass');
  $results = $query
    ->execute()
    ->fetchAllAssoc('uid');

  // Find users with the same password as their username.
  foreach ($results as $row) {
    if ($row->count > 0 || $row->uid == 1) {

      // Make a psuedo account object to avoid loading the user.
      $account = (object) array(
        'uid' => $row->uid,
        'name' => $row->name,
        'pass' => $row->pass,
      );
      if (user_check_password($row->name, $account)) {
        $weak_users[$row->uid] = $row->name;
      }
    }
  }
  return $weak_users;
}