From ee9e079ddd8b4a8bc767ec218a1ccbc7a131d75c Mon Sep 17 00:00:00 2001 From: Daniel Neis Date: Mon, 16 Dec 2013 16:39:18 -0200 Subject: [PATCH] MDL-35864 Require 'enrol/PLUGIN:config' capabilities to delete enrol instances --- enrol/category/db/access.php | 8 ++++ enrol/category/lib.php | 7 ++- enrol/category/version.php | 2 +- enrol/cohort/lib.php | 12 ++++++ enrol/database/db/access.php | 8 ++++ enrol/database/lib.php | 6 ++- enrol/database/version.php | 2 +- enrol/flatfile/lib.php | 5 ++- enrol/guest/lib.php | 12 ++++++ enrol/imsenterprise/db/access.php | 37 ++++++++++++++++ enrol/imsenterprise/lib.php | 11 +++++ enrol/imsenterprise/version.php | 2 +- enrol/instances.php | 71 +++++++++++++++++++------------ enrol/ldap/lib.php | 8 +++- enrol/manual/lib.php | 11 +++++ enrol/meta/lib.php | 11 +++++ enrol/mnet/db/access.php | 36 ++++++++++++++++ enrol/mnet/lib.php | 11 +++++ enrol/mnet/version.php | 2 +- enrol/paypal/lib.php | 11 +++++ enrol/self/lib.php | 11 +++++ lib/enrollib.php | 26 ++++++++++- 22 files changed, 272 insertions(+), 38 deletions(-) create mode 100644 enrol/imsenterprise/db/access.php create mode 100644 enrol/mnet/db/access.php diff --git a/enrol/category/db/access.php b/enrol/category/db/access.php index 7feea4dad1f..305ba254f65 100644 --- a/enrol/category/db/access.php +++ b/enrol/category/db/access.php @@ -34,6 +34,14 @@ $capabilities = array( 'archetypes' => array( ) ), + 'enrol/category:config' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'manager' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + ) + ), ); diff --git a/enrol/category/lib.php b/enrol/category/lib.php index b806eae8fe4..f25b60bd958 100644 --- a/enrol/category/lib.php +++ b/enrol/category/lib.php @@ -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; } diff --git a/enrol/category/version.php b/enrol/category/version.php index cb462c0f44c..fbe7f92ffa2 100644 --- a/enrol/category/version.php +++ b/enrol/category/version.php @@ -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; diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index ef6d5ae8675..e6a30266460 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -30,6 +30,18 @@ defined('MOODLE_INTERNAL') || die(); * @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. * diff --git a/enrol/database/db/access.php b/enrol/database/db/access.php index 5a6855ed11d..922beb3b6c7 100644 --- a/enrol/database/db/access.php +++ b/enrol/database/db/access.php @@ -34,4 +34,12 @@ $capabilities = array( 'manager' => CAP_ALLOW, ) ), + 'enrol/database:config' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'manager' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + ) + ), ); diff --git a/enrol/database/lib.php b/enrol/database/lib.php index b2aa3638ec8..e51002ae2ee 100644 --- a/enrol/database/lib.php +++ b/enrol/database/lib.php @@ -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; } diff --git a/enrol/database/version.php b/enrol/database/version.php index b4cfdcf520a..c8c04a32894 100644 --- a/enrol/database/version.php +++ b/enrol/database/version.php @@ -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? diff --git a/enrol/flatfile/lib.php b/enrol/flatfile/lib.php index 335a76609cd..fa468327e1e 100644 --- a/enrol/flatfile/lib.php +++ b/enrol/flatfile/lib.php @@ -101,8 +101,9 @@ class enrol_flatfile_plugin extends enrol_plugin { * @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); } /** diff --git a/enrol/guest/lib.php b/enrol/guest/lib.php index 2f0d4442437..df601777d78 100644 --- a/enrol/guest/lib.php +++ b/enrol/guest/lib.php @@ -391,4 +391,16 @@ class enrol_guest_plugin extends enrol_plugin { // 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); + } + } diff --git a/enrol/imsenterprise/db/access.php b/enrol/imsenterprise/db/access.php new file mode 100644 index 00000000000..09b9a4494be --- /dev/null +++ b/enrol/imsenterprise/db/access.php @@ -0,0 +1,37 @@ +. + +/** + * 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, + ) + ), +); + diff --git a/enrol/imsenterprise/lib.php b/enrol/imsenterprise/lib.php index 6db08519456..803f85102a6 100644 --- a/enrol/imsenterprise/lib.php +++ b/enrol/imsenterprise/lib.php @@ -788,4 +788,15 @@ class enrol_imsenterprise_plugin extends enrol_plugin { 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); + } } diff --git a/enrol/imsenterprise/version.php b/enrol/imsenterprise/version.php index 6d978c6a13e..ad89d477d2d 100644 --- a/enrol/imsenterprise/version.php +++ b/enrol/imsenterprise/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2014051200; +$plugin->version = 2014051201; $plugin->requires = 2014050800; $plugin->component = 'enrol_imsenterprise'; $plugin->cron = 60; diff --git a/enrol/instances.php b/enrol/instances.php index 09ed54922ad..825a9e81ae1 100644 --- a/enrol/instances.php +++ b/enrol/instances.php @@ -96,34 +96,52 @@ if ($canconfig and $action and confirm_sesskey()) { $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]; @@ -212,8 +230,7 @@ foreach ($instances as $instance) { } ++$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'))); } @@ -234,7 +251,7 @@ foreach ($instances as $instance) { } // 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); } diff --git a/enrol/ldap/lib.php b/enrol/ldap/lib.php index b4bf0b99766..21d5b1e5a28 100644 --- a/enrol/ldap/lib.php +++ b/enrol/ldap/lib.php @@ -106,7 +106,12 @@ class enrol_ldap_plugin extends enrol_plugin { * @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; } @@ -1155,4 +1160,3 @@ class enrol_ldap_plugin extends enrol_plugin { } } } - diff --git a/enrol/manual/lib.php b/enrol/manual/lib.php index 805ed05e81b..4066f9d25d0 100644 --- a/enrol/manual/lib.php +++ b/enrol/manual/lib.php @@ -553,4 +553,15 @@ class enrol_manual_plugin extends enrol_plugin { 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); + } } diff --git a/enrol/meta/lib.php b/enrol/meta/lib.php index 258232a07d7..30d2731e643 100644 --- a/enrol/meta/lib.php +++ b/enrol/meta/lib.php @@ -148,5 +148,16 @@ class enrol_meta_plugin extends enrol_plugin { 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); + } } diff --git a/enrol/mnet/db/access.php b/enrol/mnet/db/access.php new file mode 100644 index 00000000000..f9809792d41 --- /dev/null +++ b/enrol/mnet/db/access.php @@ -0,0 +1,36 @@ +. + +/** + * 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, + ) + ), +); diff --git a/enrol/mnet/lib.php b/enrol/mnet/lib.php index 28578a26c0a..2d3c55b3474 100644 --- a/enrol/mnet/lib.php +++ b/enrol/mnet/lib.php @@ -88,4 +88,15 @@ class enrol_mnet_plugin extends enrol_plugin { 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); + } } diff --git a/enrol/mnet/version.php b/enrol/mnet/version.php index 8caa9a8f87b..7c561d36c17 100644 --- a/enrol/mnet/version.php +++ b/enrol/mnet/version.php @@ -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) diff --git a/enrol/paypal/lib.php b/enrol/paypal/lib.php index f3762d78e4c..71ac2e4a204 100644 --- a/enrol/paypal/lib.php +++ b/enrol/paypal/lib.php @@ -307,4 +307,15 @@ class enrol_paypal_plugin extends enrol_plugin { $this->process_expirations($trace); return 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/paypal:config', $context); + } } diff --git a/enrol/self/lib.php b/enrol/self/lib.php index edc114f7bfb..e2ad5837119 100644 --- a/enrol/self/lib.php +++ b/enrol/self/lib.php @@ -644,4 +644,15 @@ class enrol_self_plugin extends enrol_plugin { // we do not use component in manual or self enrol. role_assign($roleid, $userid, $contextid, '', 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/self:config', $context); + } } diff --git a/lib/enrollib.php b/lib/enrollib.php index 6ba64982e9e..fceaf30b0d5 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1537,11 +1537,25 @@ abstract class enrol_plugin { /** * Is it possible to delete enrol instance via standard UI? * + * @deprecated since Moodle 2.8 MDL-35864 - please use can_delete_instance() instead. + * @todo MDL-35864 This will be deleted in Moodle 2.9. + * @see class_name::can_delete_instance() * @param object $instance * @return bool */ public function instance_deleteable($instance) { - return true; + debugging('Function enrol_plugin::instance_deleteable() is deprecated', DEBUG_DEVELOPER); + return $this->can_delete_instance($instance); + } + + /** + * Is it possible to delete enrol instance via standard UI? + * + * @param object $instance + * @return bool + */ + public function can_delete_instance($instance) { + return false; } /** @@ -2307,4 +2321,14 @@ abstract class enrol_plugin { // Implement if you want to restore protected group memberships, // usually this is not necessary because plugins should be able to recreate the memberships automatically. } + + /** + * Which capabilities are needed to configure the enrol instance via standard UI + * + * @return array Array with capabilities needed to configure the enrol instance + */ + public function get_config_instance_capabilities() { + return array(); + } + }