Skip to content

Commit

Permalink
MDL-35864 Require 'enrol/PLUGIN:config' capabilities to delete enrol …
Browse files Browse the repository at this point in the history
…instances
  • Loading branch information
danielneis committed Jun 20, 2014
1 parent 337075d commit ee9e079
Show file tree
Hide file tree
Showing 22 changed files with 272 additions and 38 deletions.
8 changes: 8 additions & 0 deletions enrol/category/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
'archetypes' => array(
)
),
'enrol/category:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),
);


7 changes: 6 additions & 1 deletion enrol/category/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ class enrol_category_plugin extends enrol_plugin {
* @param stdClass $instance
* @return bool
*/
public function instance_deleteable($instance) {
public function can_delete_instance($instance) {
global $DB;

$context = context_course::instance($instance->courseid);
if (!has_capability('enrol/database:config', $context)) {
return false;
}

if (!enrol_is_enabled('category')) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion enrol/category/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014051200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2014051201; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014050800; // Requires this Moodle version
$plugin->component = 'enrol_category'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 60;
12 changes: 12 additions & 0 deletions enrol/cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_cohort_plugin extends enrol_plugin {

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/cohort:config', $context);
}

/**
* Returns localised name of enrol instance.
*
Expand Down
8 changes: 8 additions & 0 deletions enrol/database/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@
'manager' => CAP_ALLOW,
)
),
'enrol/database:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),
);
6 changes: 5 additions & 1 deletion enrol/database/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ class enrol_database_plugin extends enrol_plugin {
* @param stdClass $instance
* @return bool
*/
public function instance_deleteable($instance) {
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
if (!has_capability('enrol/database:config', $context)) {
return false;
}
if (!enrol_is_enabled('database')) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion enrol/database/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014051200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2014051201; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014050800; // Requires this Moodle version
$plugin->component = 'enrol_database'; // Full name of the plugin (used for diagnostics)
//TODO: should we add cron sync?
5 changes: 3 additions & 2 deletions enrol/flatfile/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public function allow_manage(stdClass $instance) {
* @param object $instance
* @return bool
*/
public function instance_deleteable($instance) {
return true;
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/flatfile:manage', $context);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions enrol/guest/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,16 @@ public function restore_instance(restore_enrolments_structure_step $step, stdCla
// No need to set mapping, we do not restore users or roles here.
$step->set_mapping('enrol', $oldid, 0);
}

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/guest:config', $context);
}

}
37 changes: 37 additions & 0 deletions enrol/imsenterprise/db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Capabilities for imsenterprise enrolment plugin.
*
* @package enrol_imsenterprise
* @copyright 2014 Daniel Neis Araujo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$capabilities = array(
'enrol/imsenterprise:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),
);

11 changes: 11 additions & 0 deletions enrol/imsenterprise/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,15 @@ private function get_default_category_id() {

return $defaultcategoryid;
}

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/imsenterprise:config', $context);
}
}
2 changes: 1 addition & 1 deletion enrol/imsenterprise/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014051200;
$plugin->version = 2014051201;
$plugin->requires = 2014050800;
$plugin->component = 'enrol_imsenterprise';
$plugin->cron = 60;
71 changes: 44 additions & 27 deletions enrol/instances.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,34 +96,52 @@
$instance = $instances[$instanceid];
$plugin = $plugins[$instance->enrol];

if ($confirm) {
if (enrol_accessing_via_instance($instance)) {
if (!$confirm2) {
$yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1, 'confirm2'=>1, 'sesskey'=>sesskey()));
$displayname = $plugin->get_instance_name($instance);
$message = markdown_to_html(get_string('deleteinstanceconfirmself', 'enrol', array('name'=>$displayname)));
echo $OUTPUT->header();
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();
if ($plugin->can_delete_instance($instance)) {
if ($confirm) {
if (enrol_accessing_via_instance($instance)) {
if (!$confirm2) {
$yesurl = new moodle_url('/enrol/instances.php',
array('id' => $course->id,
'action' => 'delete',
'instance' => $instance->id,
'confirm' => 1,
'confirm2' => 1,
'sesskey' => sesskey()));
$displayname = $plugin->get_instance_name($instance);
$message = markdown_to_html(get_string('deleteinstanceconfirmself',
'enrol',
array('name' => $displayname)));
echo $OUTPUT->header();
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();
}
}
$plugin->delete_instance($instance);
redirect($PAGE->url);
}
$plugin->delete_instance($instance);
redirect($PAGE->url);
}

echo $OUTPUT->header();
$yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1,'sesskey'=>sesskey()));
$displayname = $plugin->get_instance_name($instance);
$users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
if ($users) {
$message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol', array('name'=>$displayname, 'users'=>$users)));
} else {
$message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol', array('name'=>$displayname)));
echo $OUTPUT->header();
$yesurl = new moodle_url('/enrol/instances.php',
array('id' => $course->id,
'action' => 'delete',
'instance' => $instance->id,
'confirm' => 1,
'sesskey' => sesskey()));
$displayname = $plugin->get_instance_name($instance);
$users = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
if ($users) {
$message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol',
array('name' => $displayname,
'users' => $users)));
} else {
$message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol',
array('name' => $displayname)));
}
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();
}
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();

} else if ($action === 'disable') {
$instance = $instances[$instanceid];
Expand Down Expand Up @@ -212,8 +230,7 @@
}
++$updowncount;

// edit links
if ($plugin->instance_deleteable($instance)) {
if ($plugin->can_delete_instance($instance)) {
$aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
$edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/delete', $strdelete, 'core', array('class' => 'iconsmall')));
}
Expand All @@ -234,7 +251,7 @@
}

// link to instance management
if (enrol_is_enabled($instance->enrol)) {
if (enrol_is_enabled($instance->enrol) && $canconfig) {
if ($icons = $plugin->get_action_icons($instance)) {
$edit = array_merge($edit, $icons);
}
Expand Down
8 changes: 6 additions & 2 deletions enrol/ldap/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ public function __construct() {
* @param object $instance
* @return bool
*/
public function instance_deleteable($instance) {
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
if (!has_capability('enrol/database:config', $context)) {
return false;
}

if (!enrol_is_enabled('ldap')) {
return true;
}
Expand Down Expand Up @@ -1155,4 +1160,3 @@ public function restore_role_assignment($instance, $roleid, $userid, $contextid)
}
}
}

11 changes: 11 additions & 0 deletions enrol/manual/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,15 @@ public function restore_group_member($instance, $groupid, $userid) {

groups_add_member($groupid, $userid);
}

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/manual:config', $context);
}
}
11 changes: 11 additions & 0 deletions enrol/meta/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,16 @@ public function cron() {
require_once("$CFG->dirroot/enrol/meta/locallib.php");
enrol_meta_sync();
}

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/meta:config', $context);
}
}

36 changes: 36 additions & 0 deletions enrol/mnet/db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Capabilities for mnet enrolment plugin.
*
* @package enrol_mnet
* @copyright 2014 Daniel Neis Araujo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$capabilities = array(
'enrol/mnet:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),
);
11 changes: 11 additions & 0 deletions enrol/mnet/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,15 @@ public function get_newinstance_link($courseid) {

return new moodle_url('/enrol/mnet/addinstance.php', array('id'=>$courseid));
}

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/mnet:config', $context);
}
}
2 changes: 1 addition & 1 deletion enrol/mnet/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014051200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2014051201; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014050800; // Requires this Moodle version
$plugin->component = 'enrol_mnet'; // Full name of the plugin (used for diagnostics)
Loading

0 comments on commit ee9e079

Please sign in to comment.