Skip to content

Commit

Permalink
MDL-59890 calendar: Use calculated context instead of guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Oct 5, 2017
1 parent bbc8116 commit 86679cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion calendar/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static function get_calendar_events($events = array(), $options = array()
(!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) ||
(!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) ||
($USER->id == $eventobj->userid) ||
(calendar_edit_event_allowed($eventid))) {
(calendar_edit_event_allowed($eventobj))) {
$events[$eventid] = $event;
} else {
$warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
Expand Down
24 changes: 10 additions & 14 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,9 +1074,10 @@ public function set_sources(stdClass $course, array $courses, stdClass $category
$this->categories = true;
$categories = [];
foreach ($courses as $course) {
$category = \coursecat::get($course->category);
$categories = array_merge($categories, $category->get_parents());
$categories[] = $category->id;
if ($category = \coursecat::get($course->category, IGNORE_MISSING)) {
$categories = array_merge($categories, $category->get_parents());
$categories[] = $category->id;
}
}

// And all categories that the user can manage.
Expand Down Expand Up @@ -2146,27 +2147,22 @@ function calendar_edit_event_allowed($event, $manualedit = false) {
// Allow users to add/edit group events if -
// 1) They have manageentries for the course OR
// 2) They have managegroupentries AND are in the group.
$eventcontext = \context_course::instance($event->courseid);
$group = $DB->get_record('groups', array('id' => $event->groupid));
return $group && (
has_capability('moodle/calendar:manageentries', $eventcontext) ||
(has_capability('moodle/calendar:managegroupentries', $eventcontext)
has_capability('moodle/calendar:manageentries', $event->context) ||
(has_capability('moodle/calendar:managegroupentries', $event->context)
&& groups_is_member($event->groupid)));
} else if (!empty($event->courseid)) {
// If groupid is not set, but course is set, it's definitely a course event.
$eventcontext = \context_course::instance($event->courseid);
return has_capability('moodle/calendar:manageentries', $eventcontext);
return has_capability('moodle/calendar:manageentries', $event->context);
} else if (!empty($event->categoryid)) {
// If groupid is not set, but category is set, it's definitely a category event.
$eventcontext = \context_coursecat::instance($event->categoryid);
return has_capability('moodle/calendar:manageentries', $eventcontext);
return has_capability('moodle/calendar:manageentries', $event->context);
} else if (!empty($event->userid) && $event->userid == $USER->id) {
// If course is not set, but userid id set, it's a user event.
$eventcontext = \context_user::instance($event->userid);
return (has_capability('moodle/calendar:manageownentries', $eventcontext));
return (has_capability('moodle/calendar:manageownentries', $event->context));
} else if (!empty($event->userid)) {
$eventcontext = \context_user::instance($event->userid);
return (has_capability('moodle/calendar:manageentries', $eventcontext));
return (has_capability('moodle/calendar:manageentries', $event->context));
}

return false;
Expand Down

0 comments on commit 86679cb

Please sign in to comment.