MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / oauth2 / issuers.php
CommitLineData
60237253
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 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/issuers.php');
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);
8445556b 43$issuerid = optional_param('id', '', PARAM_RAW);
60237253
DW
44$issuer = null;
45$mform = null;
46
8445556b
DW
47if ($issuerid) {
48 $issuer = \core\oauth2\api::get_issuer($issuerid);
60237253
DW
49 if (!$issuer) {
50 print_error('invaliddata');
51 }
52}
53
54if ($action == 'edit') {
55 if ($issuer) {
8445556b 56 $PAGE->navbar->add(get_string('editissuer', 'tool_oauth2', s($issuer->get('name'))));
60237253
DW
57 } else {
58 $PAGE->navbar->add(get_string('createnewissuer', 'tool_oauth2'));
59 }
60
61 $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer]);
62}
63
64if ($mform && $mform->is_cancelled()) {
65 redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
66} else if ($action == 'edit') {
67
68 if ($data = $mform->get_data()) {
60237253
DW
69 try {
70 if (!empty($data->id)) {
71 core\oauth2\api::update_issuer($data);
72 } else {
73 core\oauth2\api::create_issuer($data);
74 }
75 redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
76 } catch (Exception $e) {
77 redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
78 }
79 } else {
80 echo $OUTPUT->header();
81 if ($issuer) {
8445556b 82 echo $OUTPUT->heading(get_string('editissuer', 'tool_oauth2', s($issuer->get('name'))));
60237253
DW
83 } else {
84 echo $OUTPUT->heading(get_string('createnewissuer', 'tool_oauth2'));
85 }
86 $mform->display();
87 echo $OUTPUT->footer();
88 }
fa6cd89b
TR
89} else if ($action == 'savetemplate') {
90
91 $type = required_param('type', PARAM_ALPHA);
92 $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer, 'type' => $type]);
93 if ($mform->is_cancelled()) {
94 redirect(new moodle_url('/admin/tool/oauth2/issuers.php'));
95 }
96 if ($mform->is_submitted() && $data = $mform->get_data()) {
97 $issuer = new core\oauth2\issuer(0, $data);
98 $issuer->create();
99 $issuer = core\oauth2\api::create_endpoints_for_standard_issuer($type, $issuer);
100 redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
101 } else {
102 echo $OUTPUT->header();
103 $mform->display();
104 echo $OUTPUT->footer();
105 }
106
dc4b5685
DW
107} else if ($action == 'edittemplate') {
108
818e789a 109 $type = required_param('type', PARAM_ALPHA);
1d43165a 110 $docs = required_param('docslink', PARAM_ALPHAEXT);
818e789a 111 require_sesskey();
fa6cd89b
TR
112 $issuer = core\oauth2\api::init_standard_issuer($type);
113 $mform = new \tool_oauth2\form\issuer(null, ['persistent' => $issuer, 'type' => $type]);
114
115 echo $OUTPUT->header();
116 $mform->display();
117 echo $OUTPUT->footer();
118
eca128bf
DW
119} else if ($action == 'enable') {
120
121 require_sesskey();
122 core\oauth2\api::enable_issuer($issuerid);
123 redirect($PAGE->url, get_string('issuerenabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
124
125} else if ($action == 'disable') {
126
127 require_sesskey();
128 core\oauth2\api::disable_issuer($issuerid);
129 redirect($PAGE->url, get_string('issuerdisabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
60237253
DW
130
131} else if ($action == 'delete') {
132
133 if (!optional_param('confirm', false, PARAM_BOOL)) {
8445556b 134 $continueparams = ['action' => 'delete', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
60237253
DW
135 $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
136 $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
137 echo $OUTPUT->header();
8445556b 138 echo $OUTPUT->confirm(get_string('deleteconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
60237253
DW
139 echo $OUTPUT->footer();
140 } else {
141 require_sesskey();
8445556b 142 core\oauth2\api::delete_issuer($issuerid);
60237253
DW
143 redirect($PAGE->url, get_string('issuerdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
144 }
145
146} else if ($action == 'auth') {
147
148 if (!optional_param('confirm', false, PARAM_BOOL)) {
8445556b 149 $continueparams = ['action' => 'auth', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
60237253
DW
150 $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
151 $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
152 echo $OUTPUT->header();
8445556b 153 echo $OUTPUT->confirm(get_string('authconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
60237253
DW
154 echo $OUTPUT->footer();
155 } else {
156 require_sesskey();
8445556b 157 $params = ['sesskey' => sesskey(), 'id' => $issuerid, 'action' => 'auth', 'confirm' => true, 'response' => true];
60237253
DW
158 if (core\oauth2\api::connect_system_account($issuer, new moodle_url('/admin/tool/oauth2/issuers.php', $params))) {
159 redirect($PAGE->url, get_string('authconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
160 } else {
161 redirect($PAGE->url, get_string('authnotconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
162 }
163 }
164} else if ($action == 'moveup') {
165 require_sesskey();
8445556b 166 core\oauth2\api::move_up_issuer($issuerid);
60237253
DW
167 redirect($PAGE->url);
168
169} else if ($action == 'movedown') {
170 require_sesskey();
8445556b 171 core\oauth2\api::move_down_issuer($issuerid);
60237253
DW
172 redirect($PAGE->url);
173
174} else {
175 echo $OUTPUT->header();
870a4a82
DW
176 echo $OUTPUT->heading(get_string('pluginname', 'tool_oauth2'));
177 echo $OUTPUT->doc_link('OAuth2_Services', get_string('serviceshelp', 'tool_oauth2'));
60237253
DW
178 $issuers = core\oauth2\api::get_all_issuers();
179 echo $renderer->issuers_table($issuers);
180
1d43165a
DW
181 $docs = 'admin/tool/oauth2/issuers/google';
182 $params = ['action' => 'edittemplate', 'type' => 'google', 'sesskey' => sesskey(), 'docslink' => $docs];
818e789a
DW
183 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
184 echo $renderer->single_button($addurl, get_string('createnewgoogleissuer', 'tool_oauth2'));
1d43165a
DW
185 $docs = 'admin/tool/oauth2/issuers/microsoft';
186 $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey(), 'docslink' => $docs];
818e789a
DW
187 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
188 echo $renderer->single_button($addurl, get_string('createnewmicrosoftissuer', 'tool_oauth2'));
1d43165a
DW
189 $docs = 'admin/tool/oauth2/issuers/facebook';
190 $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey(), 'docslink' => $docs];
191 $params = ['action' => 'edittemplate', 'type' => 'facebook', 'sesskey' => sesskey(), 'docslink' => $docs];
818e789a
DW
192 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
193 echo $renderer->single_button($addurl, get_string('createnewfacebookissuer', 'tool_oauth2'));
60237253 194 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['action' => 'edit']);
3e3e120d
TR
195 $docs = 'admin/tool/oauth2/issuers/nextcloud';
196 $params = ['action' => 'edittemplate', 'type' => 'nextcloud', 'sesskey' => sesskey(), 'docslink' => $docs];
197 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
198 echo $renderer->single_button($addurl, get_string('createnewnextcloudissuer', 'tool_oauth2'));
199 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['action' => 'edit']);
60237253
DW
200 echo $renderer->single_button($addurl, get_string('createnewissuer', 'tool_oauth2'));
201 echo $OUTPUT->footer();
931c0234 202
60237253 203}