From c06140513d958cd89c90739d5f359052f5cb7084 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 19 Aug 2006 08:12:45 +0000 Subject: [PATCH] Fixed up get_user_roles to remove notices --- lib/accesslib.php | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 064e8e61b8..3356d0384d 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1609,11 +1609,12 @@ function user_can_override($context, $targetroleid) { return false; } // pull out all active roles of this user from this context(or above) - $userroles = get_user_roles($context); - foreach ($userroles as $userrole) { - // if any in the role_allow_override table, then it's ok - if (get_record('role_allow_override', 'roleid', $userrole->roleid, 'allowoverride', $targetroleid)) { - return true; + if ($userroles = get_user_roles($context)) { + foreach ($userroles as $userrole) { + // if any in the role_allow_override table, then it's ok + if (get_record('role_allow_override', 'roleid', $userrole->roleid, 'allowoverride', $targetroleid)) { + return true; + } } } @@ -1629,11 +1630,12 @@ function user_can_assign($context, $targetroleid) { return false; } // pull out all active roles of this user from this context(or above) - $userroles = get_user_roles($context); - foreach ($userroles as $userrole) { - // if any in the role_allow_override table, then it's ok - if (get_record('role_allow_assign', 'roleid', $userrole->roleid, 'allowassign', $targetroleid)) { - return true; + if ($userroles = get_user_roles($context)) { + foreach ($userroles as $userrole) { + // if any in the role_allow_override table, then it's ok + if (get_record('role_allow_assign', 'roleid', $userrole->roleid, 'allowassign', $targetroleid)) { + return true; + } } } @@ -1641,16 +1643,27 @@ function user_can_assign($context, $targetroleid) { } // gets all the user roles assigned in this context, or higher -function get_user_roles($context) { +function get_user_roles($context, $userid=0) { global $USER, $CFG, $db; - - $parents = get_parent_contexts($context); - $parentlists = '('.implode(',' , $parents).')'; + + if (empty($userid)) { + if (empty($USER->id)) { + return array(); + } + $userid = $USER->id; + } + + if ($parents = get_parent_contexts($context)) { + $contexts = ' AND ra.contextid IN ('.implode(',' , $parents).')'; + } else { + $contexts = ' AND ra.contextid = \''.$context->id.'\''; + } + return get_records_sql('SELECT * FROM '.$CFG->prefix.'role_assignments ra - WHERE ra.userid = '.$USER->id.' - AND ra.contextid IN '.$parentlists); + WHERE ra.userid = '.$userid. + $contexts); } ?> -- 2.39.5