To check login credentials in Drupal 6&7:
$uid = user_authenticate($username, $password);
In Drupal 8:
// Inject user.auth service..
$uid = $this->userAuth->authenticate($username, $password);
// or
$uid = \Drupal::service('user.auth')->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->roles))) {
return TRUE;
}
return FALSE;
}