From 5a17a54c48e59a2798dd68a8d1e939149523dab9 Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Mon, 30 Mar 2015 02:09:35 -0400 Subject: [PATCH] Issue #2394517 by opdavies: Add a function to check if a user has a certain role --- CHANGELOG.txt | 2 ++ modules/user/user.module | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 4060fcba6d2..3414ff83319 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,8 @@ Drupal 7.36, xxxx-xx-xx (development version) ----------------------- +- Added a user_has_role() function to check whether a user has a particular + role (API addition). - Fixed installation failures when an opcode cache is enabled. - Fixed a bug in the Drupal 6 to Drupal 7 upgrade path which caused private files to be inaccessible. diff --git a/modules/user/user.module b/modules/user/user.module index bdfd36fa388..d74ed2f4093 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -848,6 +848,26 @@ function user_is_blocked($name) { ->execute()->fetchObject(); } +/** + * Checks if a user has a role. + * + * @param int $rid + * A role ID. + * + * @param object|null $account + * (optional) A user account. Defaults to the current user. + * + * @return bool + * TRUE if the user has the role, or FALSE if not. + */ +function user_has_role($rid, $account = NULL) { + if (!$account) { + $account = $GLOBALS['user']; + } + + return isset($account->roles[$rid]); +} + /** * Implements hook_permission(). */