MDL-26198 make user_has_role_assignment() check parent contexts too
authorPetr Skoda <commits@skodak.org>
Sun, 30 Jan 2011 20:49:22 +0000 (21:49 +0100)
committerPetr Skoda <commits@skodak.org>
Sun, 30 Jan 2011 20:49:22 +0000 (21:49 +0100)
lib/accesslib.php

index 87a8287..3a2764a 100755 (executable)
@@ -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));
     }