MDL-58943 repository: Add repository_nextcloud to core plugins
[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 }
dc4b5685
DW
89} else if ($action == 'edittemplate') {
90
818e789a 91 $type = required_param('type', PARAM_ALPHA);
1d43165a 92 $docs = required_param('docslink', PARAM_ALPHAEXT);
818e789a
DW
93 require_sesskey();
94 $issuer = core\oauth2\api::create_standard_issuer($type);
1d43165a 95 $params = ['action' => 'edit', 'id' => $issuer->get('id'), 'docslink' => $docs];
818e789a
DW
96 $editurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
97 redirect($editurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
eca128bf
DW
98} else if ($action == 'enable') {
99
100 require_sesskey();
101 core\oauth2\api::enable_issuer($issuerid);
102 redirect($PAGE->url, get_string('issuerenabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
103
104} else if ($action == 'disable') {
105
106 require_sesskey();
107 core\oauth2\api::disable_issuer($issuerid);
108 redirect($PAGE->url, get_string('issuerdisabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
60237253
DW
109
110} else if ($action == 'delete') {
111
112 if (!optional_param('confirm', false, PARAM_BOOL)) {
8445556b 113 $continueparams = ['action' => 'delete', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
60237253
DW
114 $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
115 $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
116 echo $OUTPUT->header();
8445556b 117 echo $OUTPUT->confirm(get_string('deleteconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
60237253
DW
118 echo $OUTPUT->footer();
119 } else {
120 require_sesskey();
8445556b 121 core\oauth2\api::delete_issuer($issuerid);
60237253
DW
122 redirect($PAGE->url, get_string('issuerdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
123 }
124
125} else if ($action == 'auth') {
126
127 if (!optional_param('confirm', false, PARAM_BOOL)) {
8445556b 128 $continueparams = ['action' => 'auth', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
60237253
DW
129 $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
130 $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
131 echo $OUTPUT->header();
8445556b 132 echo $OUTPUT->confirm(get_string('authconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
60237253
DW
133 echo $OUTPUT->footer();
134 } else {
135 require_sesskey();
8445556b 136 $params = ['sesskey' => sesskey(), 'id' => $issuerid, 'action' => 'auth', 'confirm' => true, 'response' => true];
60237253
DW
137 if (core\oauth2\api::connect_system_account($issuer, new moodle_url('/admin/tool/oauth2/issuers.php', $params))) {
138 redirect($PAGE->url, get_string('authconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
139 } else {
140 redirect($PAGE->url, get_string('authnotconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
141 }
142 }
143} else if ($action == 'moveup') {
144 require_sesskey();
8445556b 145 core\oauth2\api::move_up_issuer($issuerid);
60237253
DW
146 redirect($PAGE->url);
147
148} else if ($action == 'movedown') {
149 require_sesskey();
8445556b 150 core\oauth2\api::move_down_issuer($issuerid);
60237253
DW
151 redirect($PAGE->url);
152
153} else {
154 echo $OUTPUT->header();
870a4a82
DW
155 echo $OUTPUT->heading(get_string('pluginname', 'tool_oauth2'));
156 echo $OUTPUT->doc_link('OAuth2_Services', get_string('serviceshelp', 'tool_oauth2'));
60237253
DW
157 $issuers = core\oauth2\api::get_all_issuers();
158 echo $renderer->issuers_table($issuers);
159
1d43165a
DW
160 $docs = 'admin/tool/oauth2/issuers/google';
161 $params = ['action' => 'edittemplate', 'type' => 'google', 'sesskey' => sesskey(), 'docslink' => $docs];
818e789a
DW
162 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
163 echo $renderer->single_button($addurl, get_string('createnewgoogleissuer', 'tool_oauth2'));
1d43165a
DW
164 $docs = 'admin/tool/oauth2/issuers/microsoft';
165 $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey(), 'docslink' => $docs];
818e789a
DW
166 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
167 echo $renderer->single_button($addurl, get_string('createnewmicrosoftissuer', 'tool_oauth2'));
1d43165a
DW
168 $docs = 'admin/tool/oauth2/issuers/facebook';
169 $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey(), 'docslink' => $docs];
170 $params = ['action' => 'edittemplate', 'type' => 'facebook', 'sesskey' => sesskey(), 'docslink' => $docs];
818e789a
DW
171 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
172 echo $renderer->single_button($addurl, get_string('createnewfacebookissuer', 'tool_oauth2'));
60237253
DW
173 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['action' => 'edit']);
174 echo $renderer->single_button($addurl, get_string('createnewissuer', 'tool_oauth2'));
175 echo $OUTPUT->footer();
931c0234 176
60237253 177}