Skip to content

Commit

Permalink
MDL-58090 oauth2: Move code from subclasses to config
Browse files Browse the repository at this point in the history
Allow the behaviour of each oauth service to be customised by config instead of
subclasses.

Part of MDL-58220
  • Loading branch information
Damyon Wiese committed Apr 3, 2017
1 parent 6023725 commit 8445556
Show file tree
Hide file tree
Showing 21 changed files with 961 additions and 117 deletions.
81 changes: 81 additions & 0 deletions admin/tool/oauth2/classes/form/endpoint.php
@@ -0,0 +1,81 @@
<?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/>.

/**
* This file contains the form add/update oauth2 endpoint.
*
* @package tool_oauth2
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_oauth2\form;
defined('MOODLE_INTERNAL') || die();

use stdClass;
use core\form\persistent;

/**
* Issuer form.
*
* @package tool_oauth2
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class endpoint extends persistent {

protected static $persistentclass = 'core\\oauth2\\endpoint';

protected static $fieldstoremove = array('submitbutton', 'action');

/**
* Define the form - called by parent constructor
*/
public function definition() {
global $PAGE;

$mform = $this->_form;
$endpoint = $this->get_persistent();

$mform->addElement('header', 'generalhdr', get_string('general'));

// Name.
$mform->addElement('text', 'name', get_string('endpointname', 'tool_oauth2'), 'maxlength="255"');
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'endpointname', 'tool_oauth2');

// Url.
$mform->addElement('text', 'url', get_string('endpointurl', 'tool_oauth2'), 'maxlength="1024"');
$mform->addRule('url', null, 'required', null, 'client');
$mform->addRule('url', get_string('maximumchars', '', 1024), 'maxlength', 1024, 'client');
$mform->addHelpButton('url', 'endpointurl', 'tool_oauth2');

$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_RAW);

$mform->addElement('hidden', 'issuerid', $endpoint->get('issuerid'));
$mform->setType('issuerid', PARAM_INT);
$mform->setConstant('issuerid', $this->_customdata['issuerid']);

$mform->addElement('hidden', 'id', $endpoint->get('id'));
$mform->setType('id', PARAM_INT);

$this->add_action_buttons(true, get_string('savechanges', 'tool_oauth2'));
}

}

6 changes: 3 additions & 3 deletions admin/tool/oauth2/classes/form/issuer.php
Expand Up @@ -48,7 +48,7 @@ public function definition() {
global $PAGE;

$mform = $this->_form;
$provider = $this->get_persistent();
$endpoint = $this->get_persistent();

$mform->addElement('header', 'generalhdr', get_string('general'));

Expand Down Expand Up @@ -77,7 +77,7 @@ public function definition() {
$mform->addHelpButton('baseurl', 'issuerbaseurl', 'tool_oauth2');

// Offline access type
$options = $provider->get_behaviour_list();
$options = $endpoint->get_behaviour_list();
$mform->addElement('select', 'behaviour', get_string('issuerbehaviour', 'tool_oauth2'), $options);
$mform->addHelpButton('behaviour', 'issuerbehaviour', 'tool_oauth2');

Expand All @@ -97,7 +97,7 @@ public function definition() {
$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_RAW);

$mform->addElement('hidden', 'id', $provider->get('id'));
$mform->addElement('hidden', 'id', $endpoint->get('id'));
$mform->setType('id', PARAM_INT);

$this->add_action_buttons(true, get_string('savechanges', 'tool_oauth2'));
Expand Down
80 changes: 80 additions & 0 deletions admin/tool/oauth2/classes/form/user_field_mapping.php
@@ -0,0 +1,80 @@
<?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/>.

/**
* This file contains the form add/update oauth2 user_field_mapping.
*
* @package tool_oauth2
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_oauth2\form;
defined('MOODLE_INTERNAL') || die();

use stdClass;
use core\form\persistent;

/**
* Issuer form.
*
* @package tool_oauth2
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_field_mapping extends persistent {

protected static $persistentclass = 'core\\oauth2\\user_field_mapping';

protected static $fieldstoremove = array('submitbutton', 'action');

/**
* Define the form - called by parent constructor
*/
public function definition() {
global $PAGE;

$mform = $this->_form;
$userfieldmapping = $this->get_persistent();

$mform->addElement('header', 'generalhdr', get_string('general'));

// External.
$mform->addElement('text', 'externalfield', get_string('userfieldexternalfield', 'tool_oauth2'), 'maxlength="255"');
$mform->addRule('externalfield', null, 'required', null, 'client');
$mform->addRule('externalfield', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('externalfield', 'userfieldexternalfield', 'tool_oauth2');

// Internal.
$choices = $userfieldmapping->get_internalfield_list();
$mform->addElement('select', 'internalfield', get_string('userfieldinternalfield', 'tool_oauth2'), $choices);
$mform->addHelpButton('internalfield', 'userfieldinternalfield', 'tool_oauth2');

$mform->addElement('hidden', 'action', 'edit');
$mform->setType('action', PARAM_RAW);

$mform->addElement('hidden', 'issuerid', $userfieldmapping->get('issuerid'));
$mform->setConstant('issuerid', $this->_customdata['issuerid']);
$mform->setType('issuerid', PARAM_INT);

$mform->addElement('hidden', 'id', $userfieldmapping->get('id'));
$mform->setType('id', PARAM_INT);

$this->add_action_buttons(true, get_string('savechanges', 'tool_oauth2'));
}

}

142 changes: 138 additions & 4 deletions admin/tool/oauth2/classes/output/renderer.php
Expand Up @@ -79,7 +79,7 @@ public function issuers_table($issuers) {
$name = $issuer->get('name');
$image = $issuer->get('image');
if ($image) {
$name = '<img width=24 height=24 alt="" src="' . $image . '"> ' . $name;
$name = '<img width=24 height=24 alt="" src="' . s($image) . '"> ' . s($name);
}
$namecell = new html_table_cell($name);
$namecell->header = true;
Expand All @@ -104,7 +104,11 @@ public function issuers_table($issuers) {
if (!empty($issuer->get('scopessupported'))) {
$discovered = $OUTPUT->pix_icon('yes', get_string('discovered', 'tool_oauth2'), 'tool_oauth2');
} else {
$discovered = $OUTPUT->pix_icon('no', get_string('notdiscovered', 'tool_oauth2'), 'tool_oauth2');
if ($issuer->get('behaviour') == $issuer::BEHAVIOUR_OPENID_CONNECT) {
$discovered = $OUTPUT->pix_icon('no', get_string('notdiscovered', 'tool_oauth2'), 'tool_oauth2');
} else {
$discovered = '-';
}
}
$discoverystatuscell = new html_table_cell($discovered);

Expand All @@ -125,22 +129,37 @@ public function issuers_table($issuers) {

$systemauthstatuscell = new html_table_cell($systemauth);

// Action links.
$links = '';
// Action links.
$editurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'edit']);
$editlink = html_writer::link($editurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));

$links .= ' ' . $editlink;

// Endpoints.
$editendpointsurl = new moodle_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => $issuer->get('id')]);
$str = get_string('editendpoints', 'tool_oauth2');
$editendpointlink = html_writer::link($editendpointsurl, $OUTPUT->pix_icon('t/viewdetails', $str));
$links .= ' ' . $editendpointlink;

// User field mapping.
$edituserfieldmappingsurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', ['issuerid' => $issuer->get('id')]);
$str = get_string('edituserfieldmappings', 'tool_oauth2');
$edituserfieldmappinglink = html_writer::link($edituserfieldmappingsurl, $OUTPUT->pix_icon('t/user', $str));
$links .= ' ' . $edituserfieldmappinglink;

// Delete.
$deleteurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'delete']);
$deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
$links .= ' ' . $deletelink;
if (!$last) {
// Move down.
$params = ['id' => $issuer->get('id'), 'action' => 'movedown', 'sesskey' => sesskey()];
$movedownurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
$movedownlink = html_writer::link($movedownurl, $OUTPUT->pix_icon('t/down', get_string('movedown')));
$links .= ' ' . $movedownlink;
}
if (!$first) {
// Move up.
$params = ['id' => $issuer->get('id'), 'action' => 'moveup', 'sesskey' => sesskey()];
$moveupurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
$moveuplink = html_writer::link($moveupurl, $OUTPUT->pix_icon('t/up', get_string('moveup')));
Expand All @@ -164,4 +183,119 @@ public function issuers_table($issuers) {
$table->data = $data;
return html_writer::table($table);
}

/**
* This function will render one beautiful table with all the endpoints.
*
* @param \core\oauth2\endpoint[] $endpoints - list of all endpoints.
* @return string HTML to output.
*/
public function endpoints_table($endpoints, $issuerid) {
global $CFG, $OUTPUT;

$table = new html_table();
$table->head = [
get_string('name'),
get_string('url'),
get_string('edit'),
];
$table->attributes['class'] = 'admintable generaltable';
$data = [];

$index = 0;

foreach ($endpoints as $endpoint) {
// Name.
$name = $endpoint->get('name');
$namecell = new html_table_cell(s($name));
$namecell->header = true;

// Url
$url = $endpoint->get('url');
$urlcell = new html_table_cell(s($url));

$links = '';
// Action links.
$editparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'edit'];
$editurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $editparams);
$editlink = html_writer::link($editurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));
$links .= ' ' . $editlink;

// Delete.
$deleteparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'delete'];
$deleteurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $deleteparams);
$deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
$links .= ' ' . $deletelink;

$editcell = new html_table_cell($links);

$row = new html_table_row([
$namecell,
$urlcell,
$editcell,
]);

$data[] = $row;
$index++;
}
$table->data = $data;
return html_writer::table($table);
}

/**
* This function will render one beautiful table with all the user_field_mappings.
*
* @param \core\oauth2\user_field_mapping[] $userfieldmappings - list of all user_field_mappings.
* @return string HTML to output.
*/
public function user_field_mappings_table($userfieldmappings, $issuerid) {
global $CFG, $OUTPUT;

$table = new html_table();
$table->head = [
get_string('userfieldexternalfield', 'tool_oauth2'),
get_string('userfieldinternalfield', 'tool_oauth2'),
get_string('edit'),
];
$table->attributes['class'] = 'admintable generaltable';
$data = [];

$index = 0;

foreach ($userfieldmappings as $userfieldmapping) {
// External field
$externalfield = $userfieldmapping->get('externalfield');
$externalfieldcell = new html_table_cell(s($externalfield));

// Internal field
$internalfield = $userfieldmapping->get('internalfield');
$internalfieldcell = new html_table_cell(s($internalfield));

$links = '';
// Action links.
$editparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'edit'];
$editurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $editparams);
$editlink = html_writer::link($editurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));
$links .= ' ' . $editlink;

// Delete.
$deleteparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'delete'];
$deleteurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $deleteparams);
$deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
$links .= ' ' . $deletelink;

$editcell = new html_table_cell($links);

$row = new html_table_row([
$externalfieldcell,
$internalfieldcell,
$editcell,
]);

$data[] = $row;
$index++;
}
$table->data = $data;
return html_writer::table($table);
}
}

0 comments on commit 8445556

Please sign in to comment.