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