From 43731ba277c9ca7adbfe885bcaacd71329559722 Mon Sep 17 00:00:00 2001 From: Benjamin Walker Date: Tue, 3 Dec 2024 10:53:21 +1000 Subject: [PATCH] Add manage capability and move rules to own page --- classes/form/manage.php | 90 ++++++++++++++++++++++++++++++++++++++ classes/helper.php | 2 +- db/access.php | 39 +++++++++++++++++ index.php | 57 ++++++++++++++++++++++++ lang/en/tool_redirects.php | 1 + settings.php | 19 ++++---- version.php | 4 +- 7 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 classes/form/manage.php create mode 100644 db/access.php create mode 100644 index.php diff --git a/classes/form/manage.php b/classes/form/manage.php new file mode 100644 index 0000000..ec306f5 --- /dev/null +++ b/classes/form/manage.php @@ -0,0 +1,90 @@ +. + +/** + * Form to manage redirect rules. + * + * @package tool_redirects + * @author Benjamin Walker + * @copyright 2024, Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_redirects\form; + +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/formslib.php"); + +/** + * Manage redirects form. + */ +class manage extends \moodleform { + /** @var array config settings attached to the form */ + protected const FORM_CONFIG = [ + 'rules', + ]; + + /** @var array config */ + protected $config; + + /** + * Form definition + * + * @return void + */ + public function definition(): void { + global $CFG, $DB, $USER; + + $mform = $this->_form; + + // Rules. + $mform->addElement('textarea', 'rules', get_string('rules', 'tool_redirects'), ['cols' => 60, 'rows' => 8]); + $mform->setType('rules', PARAM_RAW); + $mform->addElement('static', 'rules_help', '', get_string('rules_desc', 'tool_redirects')); + + $this->add_action_buttons(false); + } + + /** + * Loads the current values of the form config. + * @return void + */ + public function load_form_config(): void { + $config = []; + foreach (self::FORM_CONFIG as $name) { + $config[$name] = get_config('tool_redirects', $name); + } + + // Save empty config values but don't set them. Needed for comparisons. + $this->config = $config; + $this->set_data(array_filter($config)); + } + + /** + * Saves the new form data to config. + * @param \stdClass $data submitted data + * @return void + */ + public function save_form_config(\stdClass $data): void { + foreach ($data as $key => $value) { + if (in_array($key, self::FORM_CONFIG) && $value !== $this->config[$key]) { + set_config($key, $value, 'tool_redirects'); + add_to_config_log($key, (string) $this->config[$key], $value, 'tool_redirects'); + } + } + } +} diff --git a/classes/helper.php b/classes/helper.php index 7968287..90c978a 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -111,7 +111,7 @@ public static function redirect_from_rules() { \core\notification::info(get_string('redirectwarning', 'tool_redirects', [ 'target' => $target->out(), 'regex' => $rule->get_regex(), - 'editurl' => (new \moodle_url('/admin/settings.php?section=tool_redirects'))->out(), + 'editurl' => (new \moodle_url('/admin/tool/redirects/index.php'))->out(), ])); break; } diff --git a/db/access.php b/db/access.php new file mode 100644 index 0000000..ed545c7 --- /dev/null +++ b/db/access.php @@ -0,0 +1,39 @@ +. + +/** + * Capabilities. + * + * This files lists capabilities related to tool_redirects. + * + * @package tool_redirects + * @author Benjamin Walker + * @copyright 2024, Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$capabilities = [ + 'tool/redirects:manage' => [ + 'riskbitmask' => RISK_XSS | RISK_CONFIG, + 'captype' => 'write', + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => [ + 'manager' => CAP_ALLOW, + ], + ], +]; diff --git a/index.php b/index.php new file mode 100644 index 0000000..f7419d7 --- /dev/null +++ b/index.php @@ -0,0 +1,57 @@ +. + +/** + * Page to manage redirect rules + * + * @package tool_redirects + * @author Benjamin Walker + * @copyright 2024, Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../../config.php'); + +// This is locked behind a separate capability so the task can be delegated to non-admins. +require_login(); +$context = context_system::instance(); +require_capability('tool/redirects:manage', $context); + +// Set up the page. +$url = new moodle_url('/admin/tool/redirects/index.php'); +$PAGE->set_url($url); +$PAGE->set_context($context); +$PAGE->set_pagelayout('admin'); +$PAGE->set_title(get_string('redirects:manage', 'tool_redirects')); +$PAGE->set_heading($SITE->fullname); + +// Create the form and load current config. +$mform = new \tool_redirects\form\manage(); +$mform->load_form_config(); + +// Handle form submission. +if ($mform->is_cancelled()) { + redirect(new moodle_url('/')); +} else if ($data = $mform->get_data()) { + $mform->save_form_config($data); + redirect($url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS); +} + +// Display the form. +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('redirects:manage', 'tool_redirects')); +$mform->display(); +echo $OUTPUT->footer(); diff --git a/lang/en/tool_redirects.php b/lang/en/tool_redirects.php index 7de609a..08b86d2 100644 --- a/lang/en/tool_redirects.php +++ b/lang/en/tool_redirects.php @@ -43,6 +43,7 @@ $string['redirectwarning'] = '

This pages url matched a regex: {$a->regex} and so would have redirected to:

{$a->target}

You are seeing this because you are able to edit the redirect configuration.

'; +$string['redirects:manage'] = 'Manage redirect rules'; $string['regex_error_too_short'] = 'RegEx too short'; $string['regex_error_malformed'] = 'Invalid (malformed) RegEx'; $string['privacy:metadata'] = 'The Redirects plugin does not store any personal data.'; diff --git a/settings.php b/settings.php index cbe313f..ea74479 100644 --- a/settings.php +++ b/settings.php @@ -27,15 +27,18 @@ if (is_siteadmin()) { - $settings = new admin_settingpage('tool_redirects', get_string('pluginname', 'tool_redirects')); - $ADMIN->add('tools', $settings); + $category = new admin_category('tool_redirects', get_string('pluginname', 'tool_redirects')); + $ADMIN->add('tools', $category); - $name = 'tool_redirects/rules'; - $title = get_string('rules', 'tool_redirects'); - $description = get_string('rules_desc', 'tool_redirects'); - $default = ''; - $setting = new admin_setting_configtextarea($name, $title, $description, $default); - $settings->add($setting); + $settings = new admin_settingpage('tool_redirects_settings', get_string('generalsettings', 'admin')); + $ADMIN->add('tool_redirects', $settings); + + $ADMIN->add('tool_redirects', new admin_externalpage( + 'tool_redirects_manage', + get_string('redirects:manage', 'tool_redirects'), + new moodle_url('/admin/tool/redirects/index.php'), + 'tool/redirects:manage' + )); $name = 'tool_redirects/redirectadmin'; $title = get_string('redirectadmin', 'tool_redirects'); diff --git a/version.php b/version.php index 72b46b0..79b80fc 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024112800; -$plugin->release = 2024112800; // Match release exactly to version. +$plugin->version = 2024120200; +$plugin->release = 2024120200; // Match release exactly to version. $plugin->requires = 2017051500; // Moodle 3.3. $plugin->component = 'tool_redirects'; $plugin->maturity = MATURITY_STABLE;