Skip to content

Commit

Permalink
webservice MDL-23440 more explicit error message when validation cont…
Browse files Browse the repository at this point in the history
…ext fail in some external functions.
  • Loading branch information
mouneyrac committed Jul 30, 2010
1 parent 451f1e3 commit 41e962f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
10 changes: 9 additions & 1 deletion enrol/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ public static function get_enrolled_users($courseid, $withcapability, $groupid,
$context = $coursecontext;
}

self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $params['courseid'];
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}

if ($courseid == SITEID) {
require_capability('moodle/site:viewparticipants', $context);
Expand Down
70 changes: 63 additions & 7 deletions group/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ public static function create_groups($groups) {

// now security checks
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $group->courseid;
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

// finally create the group
Expand Down Expand Up @@ -135,7 +143,15 @@ public static function get_groups($groupids) {

// now security checks
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $group->courseid;
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

$groups[] = (array)$group;
Expand Down Expand Up @@ -184,7 +200,15 @@ public static function get_course_groups($courseid) {

// now security checks
$context = get_context_instance(CONTEXT_COURSE, $params['courseid']);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $params['courseid'];
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

$gs = groups_get_all_groups($params['courseid'], 0, 0, 'g.id, g.courseid, g.name, g.description, g.enrolmentkey');
Expand Down Expand Up @@ -251,7 +275,15 @@ public static function delete_groups($groupids) {

// now security checks
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $group->courseid;
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

groups_delete_group($group);
Expand Down Expand Up @@ -296,7 +328,15 @@ public static function get_groupmembers($groupids) {
$group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
// now security checks
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $group->courseid;
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

$groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC');
Expand Down Expand Up @@ -364,7 +404,15 @@ public static function add_groupmembers($members) {

// now security checks
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $group->courseid;
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

// now make sure user is enrolled in course - this is mandatory requirement,
Expand Down Expand Up @@ -430,7 +478,15 @@ public static function delete_groupmembers($members) {

// now security checks
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
self::validate_context($context);
try {
self::validate_context($context);
} catch (Exception $e) {
$exceptionparam = new stdClass();
$exceptionparam->message = $e->getMessage();
$exceptionparam->courseid = $group->courseid;
throw new moodle_exception(
get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
}
require_capability('moodle/course:managegroups', $context);

groups_remove_member($group, $user);
Expand Down

0 comments on commit 41e962f

Please sign in to comment.