MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / oauth2 / endpoints.php
CommitLineData
8445556b
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * OAuth 2 Endpoing Configuration page.
19 *
20 * @package tool_oauth2
21 * @copyright 2017 Damyon Wiese <damyon@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../../../config.php');
26require_once($CFG->libdir.'/adminlib.php');
27require_once($CFG->libdir.'/tablelib.php');
28
29$PAGE->set_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => required_param('issuerid', PARAM_INT)]);
30$PAGE->set_context(context_system::instance());
31$PAGE->set_pagelayout('admin');
32$strheading = get_string('pluginname', 'tool_oauth2');
33$PAGE->set_title($strheading);
34$PAGE->set_heading($strheading);
35
36require_login();
37
38require_capability('moodle/site:config', context_system::instance());
39
40$renderer = $PAGE->get_renderer('tool_oauth2');
41
42$action = optional_param('action', '', PARAM_ALPHAEXT);
43$issuerid = required_param('issuerid', PARAM_INT);
44$endpointid = optional_param('endpointid', '', PARAM_INT);
45$endpoint = null;
46$mform = null;
47
48$issuer = \core\oauth2\api::get_issuer($issuerid);
49if (!$issuer) {
50 print_error('invaliddata');
51}
52$PAGE->navbar->override_active_url(new moodle_url('/admin/tool/oauth2/issuers.php'), true);
53
54if (!empty($endpointid)) {
55 $endpoint = \core\oauth2\api::get_endpoint($endpointid);
56}
57
58if ($action == 'edit') {
59 if ($endpoint) {
60 $strparams = [ 'issuer' => s($issuer->get('name')), 'endpoint' => s($endpoint->get('name')) ];
61 $PAGE->navbar->add(get_string('editendpoint', 'tool_oauth2', $strparams));
62 } else {
63 $PAGE->navbar->add(get_string('createnewendpoint', 'tool_oauth2', s($issuer->get('name'))));
64 }
65
66 $mform = new \tool_oauth2\form\endpoint(null, ['persistent' => $endpoint, 'issuerid' => $issuerid]);
67}
68
69if ($mform && $mform->is_cancelled()) {
70 redirect(new moodle_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => $issuerid]));
71} else if ($action == 'edit') {
72
73 if ($data = $mform->get_data()) {
74
75 try {
76 if (!empty($data->id)) {
77 core\oauth2\api::update_endpoint($data);
78 } else {
79 core\oauth2\api::create_endpoint($data);
80 }
81 redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
82 } catch (Exception $e) {
83 redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
84 }
85 } else {
86 echo $OUTPUT->header();
87 if ($endpoint) {
88 $strparams = [ 'issuer' => s($issuer->get('name')), 'endpoint' => s($endpoint->get('name')) ];
89 echo $OUTPUT->heading(get_string('editendpoint', 'tool_oauth2', $strparams));
90 } else {
91 echo $OUTPUT->heading(get_string('createnewendpoint', 'tool_oauth2', s($issuer->get('name'))));
92 }
93 $mform->display();
94 echo $OUTPUT->footer();
95 }
96
97} else if ($action == 'delete') {
98
99 if (!optional_param('confirm', false, PARAM_BOOL)) {
100 $continueparams = [
101 'action' => 'delete',
102 'issuerid' => $issuerid,
103 'endpointid' => $endpointid,
104 'sesskey' => sesskey(),
105 'confirm' => true
106 ];
107 $continueurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $continueparams);
108 $cancelurl = new moodle_url('/admin/tool/oauth2/endpoints.php');
109 echo $OUTPUT->header();
110 $strparams = [ 'issuer' => s($issuer->get('name')), 'endpoint' => s($endpoint->get('name')) ];
111 echo $OUTPUT->confirm(get_string('deleteendpointconfirm', 'tool_oauth2', $strparams), $continueurl, $cancelurl);
112 echo $OUTPUT->footer();
113 } else {
114 require_sesskey();
115 core\oauth2\api::delete_endpoint($endpointid);
116 redirect($PAGE->url, get_string('endpointdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
117 }
118
119} else {
120 echo $OUTPUT->header();
121 echo $OUTPUT->heading(get_string('endpointsforissuer', 'tool_oauth2', s($issuer->get('name'))));
122 $endpoints = core\oauth2\api::get_endpoints($issuer);
123 echo $renderer->endpoints_table($endpoints, $issuerid);
124
125 $addurl = new moodle_url('/admin/tool/oauth2/endpoints.php', ['action' => 'edit', 'issuerid' => $issuerid]);
126 echo $renderer->single_button($addurl, get_string('createnewendpoint', 'tool_oauth2', s($issuer->get('name'))));
127 echo $OUTPUT->footer();
128}