How do I check the logged-in user username/password/role using a function to validate them?
To check login credentials in Drupal 6&amp;7:

$uid = user_authenticate($username, $password);


In Drupal 8:

// Inject user.auth service..
$uid = $this-&gt;userAuth-&gt;authenticate($username, $password);
// or
$uid = \Drupal::service('user.auth')-&gt;authenticate($username, $password);


To check roles:

/**
 * Check User roles
 * @param $role
 * @param null $user
 * @return bool
 */
function user_has_role($roles, $user = NULL) {
  if ($user == NULL) {
    global $user;
  }
  if (!is_array($roles)) {
    $roles = array($roles);
  }
  if (count(array_intersect($roles, $user-&gt;roles))) {
    return TRUE;
  }
  return FALSE;
}