Skip to content

Commit

Permalink
Merge pull request #25 from catalyst/add-capability
Browse files Browse the repository at this point in the history
Add manage capability and move rules to own page
  • Loading branch information
brendanheywood authored Dec 3, 2024
2 parents 49b6b75 + 43731ba commit dc43b64
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 11 deletions.
90 changes: 90 additions & 0 deletions classes/form/manage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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/>.

/**
* Form to manage redirect rules.
*
* @package tool_redirects
* @author Benjamin Walker <[email protected]>
* @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');
}
}
}
}
2 changes: 1 addition & 1 deletion classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
39 changes: 39 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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.
*
* This files lists capabilities related to tool_redirects.
*
* @package tool_redirects
* @author Benjamin Walker <[email protected]>
* @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,
],
],
];
57 changes: 57 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/>.

/**
* Page to manage redirect rules
*
* @package tool_redirects
* @author Benjamin Walker <[email protected]>
* @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();
1 change: 1 addition & 0 deletions lang/en/tool_redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
$string['redirectwarning'] = '<p>This pages url matched a regex: <code>{$a->regex}</code> and so would have redirected to:<p>
<a href="{$a->target}">{$a->target}</a>
<p>You are seeing this because you are able to <a href="{$a->editurl}">edit the redirect configuration</a>.</p>';
$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.';
19 changes: 11 additions & 8 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dc43b64

Please sign in to comment.