MDL-69542 enrol_lti: admin page providing platform registration

This commit is contained in:
Jake Dallimore 2022-01-24 16:57:46 +08:00
parent 1f3d37390d
commit 8c62efe74e
4 changed files with 474 additions and 0 deletions

View File

@ -0,0 +1,103 @@
<?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/>.
namespace enrol_lti\local\ltiadvantage\admin;
use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
/**
* The admin_setting_registeredplatforms class, for rendering a table of platforms which have been registered.
*
* This setting is useful for LTI 1.3 only.
*
* @package enrol_lti
* @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_registeredplatforms extends \admin_setting {
/**
* Calls parent::__construct with specific arguments
*/
public function __construct() {
$this->nosave = true;
parent::__construct('enrol_lti_tool_registered_platforms', get_string('registeredplatforms', 'enrol_lti'), '',
'');
}
/**
* Always returns true, does nothing.
*
* @return bool true.
*/
public function get_setting() {
return true;
}
/**
* Always returns true, does nothing.
*
* @return bool true.
*/
public function get_defaultsetting() {
return true;
}
/**
* Always returns '', does not write anything.
*
* @param string|array $data the data
* @return string Always returns ''.
*/
public function write_setting($data) {
return '';
}
/**
* Checks if $query is one of the available external services
*
* @param string $query The string to search for
* @return bool Returns true if found, false if not
*/
public function is_related($query) {
if (parent::is_related($query)) {
return true;
}
$appregistrationrepo = new application_registration_repository();
$registrations = $appregistrationrepo->find_all();
foreach ($registrations as $reg) {
if (stripos($reg->get_name(), $query) !== false) {
return true;
}
}
return false;
}
/**
* Builds the HTML to display the table.
*
* @param string $data Unused
* @param string $query
* @return string
*/
public function output_html($data, $query='') {
global $PAGE;
$appregistrationrepo = new application_registration_repository();
$renderer = $PAGE->get_renderer('enrol_lti');
$return = $renderer->render_admin_setting_registered_platforms($appregistrationrepo->find_all());
return highlight($query, $return);
}
}

View File

@ -0,0 +1,115 @@
<?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/>.
namespace enrol_lti\local\ltiadvantage\form;
use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
/**
* The platform_registration_form class, for registering a platform as a consumer of a published tool.
*
* @package enrol_lti
* @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class platform_registration_form extends \moodleform {
/**
* Define the form.
*/
protected function definition() {
$mform = $this->_form;
$strrequired = get_string('required');
// Id.
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
// Name.
$mform->addElement('text', 'name', get_string('registerplatform:name', 'enrol_lti'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', $strrequired, 'required', null, 'client');
// Platform Id.
$mform->addElement('text', 'platformid', get_string('registerplatform:platformid', 'enrol_lti'));
$mform->setType('platformid', PARAM_URL);
$mform->addRule('platformid', $strrequired, 'required', null, 'client');
$mform->addHelpButton('platformid', 'registerplatform:platformid', 'enrol_lti');
// Client Id.
$mform->addElement('text', 'clientid', get_string('registerplatform:clientid', 'enrol_lti'));
$mform->setType('clientid', PARAM_TEXT);
$mform->addRule('clientid', $strrequired, 'required', null, 'client');
$mform->addHelpButton('clientid', 'registerplatform:clientid', 'enrol_lti');
// Authentication request URL.
$mform->addElement('text', 'authenticationrequesturl', get_string('registerplatform:authrequesturl', 'enrol_lti'));
$mform->setType('authenticationrequesturl', PARAM_URL);
$mform->addRule('authenticationrequesturl', $strrequired, 'required', null, 'client');
$mform->addHelpButton('authenticationrequesturl', 'registerplatform:authrequesturl', 'enrol_lti');
// JSON Web Key Set URL.
$mform->addElement('text', 'jwksurl', get_string('registerplatform:jwksurl', 'enrol_lti'));
$mform->setType('jwksurl', PARAM_URL);
$mform->addRule('jwksurl', $strrequired, 'required', null, 'client');
$mform->addHelpButton('jwksurl', 'registerplatform:jwksurl', 'enrol_lti');
// Access token URL.
$mform->addElement('text', 'accesstokenurl', get_string('registerplatform:accesstokenurl', 'enrol_lti'));
$mform->setType('accesstokenurl', PARAM_URL);
$mform->addRule('accesstokenurl', $strrequired, 'required', null, 'client');
$mform->addHelpButton('accesstokenurl', 'registerplatform:accesstokenurl', 'enrol_lti');
$buttonarray = [];
$buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
}
/**
* Provides validation of URL syntax and issuer uniqueness.
*
* @param array $data the form data.
* @param array $files any submitted files.
* @return array an array of errors.
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Check URLs look ok.
foreach ($data as $key => $val) {
if (isset($this->_form->_types[$key]) && $this->_form->_types[$key] == 'url') {
if (!filter_var($val, FILTER_VALIDATE_URL)) {
$errors[$key] = get_string('registerplatform:invalidurlerror', 'enrol_lti');
}
}
}
// Check uniqueness of the {issuer, client_id} tuple.
$appregistrationrepo = new application_registration_repository();
$appreg = $appregistrationrepo->find_by_platform($data['platformid'], $data['clientid']);
if ($appreg) {
if (empty($data['id']) || $appreg->get_id() != $data['id']) {
$errors['clientid'] = get_string('registerplatform:duplicateregistrationerror', 'enrol_lti');
}
}
return $errors;
}
}

View File

@ -0,0 +1,156 @@
<?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/>.
/**
* LTI 1.3 page to create or edit a platform registration.
*
* This page is only used by LTI 1.3. Older versions do not require platforms to be registered with the tool during
* registration.
*
* @package enrol_lti
* @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\notification;
use enrol_lti\local\ltiadvantage\form\platform_registration_form;
use enrol_lti\local\ltiadvantage\entity\application_registration;
use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
use enrol_lti\local\ltiadvantage\repository\context_repository;
use enrol_lti\local\ltiadvantage\repository\deployment_repository;
use enrol_lti\local\ltiadvantage\repository\resource_link_repository;
use enrol_lti\local\ltiadvantage\repository\user_repository;
use enrol_lti\local\ltiadvantage\service\application_registration_service;
require_once(__DIR__ . '/../../config.php');
global $CFG, $OUTPUT, $DB;
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/enrol/lti/lib.php');
$action = required_param('action', PARAM_ALPHA);
if (!in_array($action, ['add', 'edit', 'delete'])) {
throw new coding_exception("Invalid action param '$action'");
}
// The page to go back to when the respective action has been performed.
$toolregistrationurl = new moodle_url($CFG->wwwroot . "/" . $CFG->admin . "/settings.php",
['section' => 'enrolsettingslti_registrations']);
// Local anon helper to extend the nav for this page and call admin_externalpage_setup.
$pagesetup = function(string $pagetitle) {
global $PAGE;
navigation_node::override_active_url(
new moodle_url('/admin/settings.php', ['section' => 'enrolsettingslti_registrations'])
);
admin_externalpage_setup('enrolsettingslti_registrations_edit', '', null, '', ['pagelayout' => 'admin']);
$PAGE->navbar->add($pagetitle);
};
if ($action === 'add') {
$pagesetup(get_string('registerplatformadd', 'enrol_lti'));
$pageurl = new moodle_url('/enrol/lti/register_platform.php', ['action' => 'add']);
$mform = new platform_registration_form($pageurl->out(false));
if ($data = $mform->get_data()) {
$regservice = new application_registration_service(new application_registration_repository(),
new deployment_repository(), new resource_link_repository(), new context_repository(),
new user_repository());
$regservice->create_application_registration($data);
redirect($toolregistrationurl, get_string('registerplatformaddnotice', 'enrol_lti'), null,
notification::NOTIFY_SUCCESS);
} else if (!$mform->is_cancelled()) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('registerplatformadd', 'enrol_lti'));
$mform->display();
echo $OUTPUT->footer();
die();
}
redirect($toolregistrationurl);
} else if ($action === 'edit') {
$regid = required_param('regid', PARAM_INT);
$pagesetup(get_string('registerplatformedit', 'enrol_lti'));
$pageurl = new moodle_url('/enrol/lti/register_platform.php', ['action' => 'edit', 'regid' => $regid]);
$mform = new platform_registration_form($pageurl->out(false));
if (($data = $mform->get_data()) && confirm_sesskey()) {
$regservice = new application_registration_service(new application_registration_repository(),
new deployment_repository(), new resource_link_repository(), new context_repository(),
new user_repository());
$regservice->update_application_registration($data);
redirect($toolregistrationurl, get_string('registerplatformeditnotice', 'enrol_lti'), null,
notification::NOTIFY_SUCCESS);
} else if (!$mform->is_cancelled()) {
// Anon helper to transform data.
$maptoformdata = function(application_registration $registration): \stdClass {
return (object) [
'id' => $registration->get_id(),
'name' => $registration->get_name(),
'platformid' => $registration->get_platformid(),
'clientid' => $registration->get_clientid(),
'authenticationrequesturl' => $registration->get_authenticationrequesturl(),
'jwksurl' => $registration->get_jwksurl(),
'accesstokenurl' => $registration->get_accesstokenurl()
];
};
$appregistrationrepo = new application_registration_repository();
$registration = $appregistrationrepo->find($regid);
if (!$registration) {
throw new coding_exception("cannot edit non-existent registration '{$regid}'.");
}
$mform->set_data($maptoformdata($registration));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('registerplatformedit', 'enrol_lti'));
$mform->display();
echo $OUTPUT->footer();
die();
}
redirect($toolregistrationurl);
} else if ($action === 'delete') {
$regid = required_param('regid', PARAM_INT);
$pagesetup(get_string('registerplatformdelete', 'enrol_lti'));
if (!optional_param('confirm', false, PARAM_BOOL)) {
$continueparams = ['action' => 'delete', 'regid' => $regid, 'sesskey' => sesskey(), 'confirm' => true];
$continueurl = new moodle_url('/enrol/lti/register_platform.php', $continueparams);
$appregrepo = new application_registration_repository();
$appreg = $appregrepo->find($regid);
if (!$appreg) {
throw new coding_exception("Cannot delete non existent application registration '{$regid}'.");
}
echo $OUTPUT->header();
echo $OUTPUT->confirm(
get_string('registerplatformdeleteconfirm', 'enrol_lti', format_string($appreg->get_name())),
$continueurl,
$toolregistrationurl
);
echo $OUTPUT->footer();
} else {
require_sesskey();
$regservice = new application_registration_service(new application_registration_repository(),
new deployment_repository(), new resource_link_repository(), new context_repository(),
new user_repository());
$regservice->delete_application_registration($regid);
redirect($toolregistrationurl,
get_string('registerplatformdeletenotice', 'enrol_lti'), null, notification::NOTIFY_SUCCESS);
}
}

View File

@ -0,0 +1,100 @@
{{!
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/>.
}}
{{!
@template enrol_lti/registered_platforms
Template which displays a table view of all registered apps with options to add, delete and manage deployments.
Classes required for JS:
* none
Data attributes required for JS:
* none
Context variables required for this template:
* hasregs
* addurl
Optional context variables for this template:
* registrations
Example context (json):
{
"hasregs": true,
"registrations": [
{
"name": "Platform Y registration",
"hasdeployments": true,
"countdeployments": 2,
"editurl": "https://example.org/enrol/lti/register_platform.php?action=edit&regid=4",
"deploymentsurl": "https://example.org/enrol/lti/manage_deployments.php?registrationid=1",
"deleteurl": "https://example.org/enrol/lti/register_platform.php?action=delete&regid=4"
}
],
"addurl": "https://example.org/enrol/lti/manage_deployments.php?action=add"
}
}}
<div id="lti_registered_platforms" class="mb-3">
{{#hasregs}}
<table class="admintable generaltable">
<thead>
<tr>
<th>{{#str}}name, core{{/str}}</th>
<th>{{#str}}details, enrol_lti{{/str}}</th>
<th>{{#str}}deployments, enrol_lti{{/str}}</th>
<th>{{#str}}actions, core{{/str}}</th>
</tr>
</thead>
<tbody>
{{#registrations}}
<tr>
<th>
{{name}}
</th>
<td>
<span class="font-weight-bold">{{#str}}registerplatform:platformid, enrol_lti{{/str}}: </span> {{issuer}}<br>
<span class="font-weight-bold">{{#str}}registerplatform:clientid, enrol_lti{{/str}}: </span> {{clientid}}
</td>
<td>
{{#hasdeployments}}
<a href="{{deploymentsurl}}" title="{{#str}} managedeployments, enrol_lti {{/str}}">{{countdeployments}}</a>
{{/hasdeployments}}
{{^hasdeployments}}
{{countdeployments}}
{{/hasdeployments}}
</td>
<td>
<a class="edit" href="{{editurl}}" title="{{#str}} edit {{/str}}">{{#pix}} t/edit, core, {{#str}} edit {{/str}}{{/pix}}</a>
<a class="manage" href="{{deploymentsurl}}" title="{{#str}} managedeployments, enrol_lti {{/str}}">{{#pix}} t/viewdetails, core, {{#str}} managedeployments, enrol_lti {{/str}}{{/pix}}</a>
<a class="delete" href="{{deleteurl}}" title="{{#str}} delete {{/str}}">{{#pix}} t/delete, core, {{#str}} delete {{/str}}{{/pix}}</a>
</td>
</tr>
{{/registrations}}
</tbody>
</table>
{{/hasregs}}
{{^hasregs}}
<div>
{{#str}}noregisteredplatforms, enrol_lti{{/str}}
</div>
<br>
{{/hasregs}}
<div>
<a class="btn btn-secondary" href="{{addurl}}">{{#str}}registerplatformadd, enrol_lti{{/str}}</a>
</div>
</div>