Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-26198 make user_has_role_assignment() check parent contexts too

  • Loading branch information...
commit ff03c5b6b54250b1654ed2995662279d2906a023 1 parent 8cdc85a
@skodak skodak authored
Showing with 19 additions and 3 deletions.
  1. +19 −3 lib/accesslib.php
View
22 lib/accesslib.php
@@ -5526,18 +5526,34 @@ function get_users_from_role_on_context($role, $context) {
}
/**
- * Simple function returning a boolean true if roles exist, otherwise false
+ * Simple function returning a boolean true if user has roles
+ * in context or parent contexts, otherwise false.
*
* @param int $userid
* @param int $roleid
- * @param int $contextid
+ * @param int $contextid empty means any context
* @return bool
*/
function user_has_role_assignment($userid, $roleid, $contextid = 0) {
global $DB;
if ($contextid) {
- return $DB->record_exists('role_assignments', array('userid'=>$userid, 'roleid'=>$roleid, 'contextid'=>$contextid));
+ if (!$context = get_context_instance_by_id($contextid)) {
+ return false;
+ }
+ $parents = get_parent_contexts($context, true);
+ list($contexts, $params) = $DB->get_in_or_equal($parents, SQL_PARAMS_NAMED, 'r0000');
+ $params['userid'] = $userid;
+ $params['roleid'] = $roleid;
+
+ $sql = "SELECT COUNT(ra.id)
+ FROM {role_assignments} ra
+ WHERE ra.userid = :userid AND ra.roleid = :roleid AND ra.contextid $contexts";
+
+ $count = $DB->get_field_sql($sql, $params);
+ var_dump($count);
+ return ($count > 0);
+
} else {
return $DB->record_exists('role_assignments', array('userid'=>$userid, 'roleid'=>$roleid));
}
Please sign in to comment.
Something went wrong with that request. Please try again.