Skip to content

Commit

Permalink
MDL-26198 make user_has_role_assignment() check parent contexts too
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 30, 2011
1 parent 8cdc85a commit ff03c5b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/accesslib.php
Expand Up @@ -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 $userid
* @param int $roleid * @param int $roleid
* @param int $contextid * @param int $contextid empty means any context
* @return bool * @return bool
*/ */
function user_has_role_assignment($userid, $roleid, $contextid = 0) { function user_has_role_assignment($userid, $roleid, $contextid = 0) {
global $DB; global $DB;


if ($contextid) { 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 { } else {
return $DB->record_exists('role_assignments', array('userid'=>$userid, 'roleid'=>$roleid)); return $DB->record_exists('role_assignments', array('userid'=>$userid, 'roleid'=>$roleid));
} }
Expand Down

0 comments on commit ff03c5b

Please sign in to comment.