MDL-58220 assign: Only show write access to teachers
[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()) {
69
70 try {
71 if (!empty($data->id)) {
72 core\oauth2\api::update_issuer($data);
73 } else {
74 core\oauth2\api::create_issuer($data);
75 }
76 redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
77 } catch (Exception $e) {
78 redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
79 }
80 } else {
81 echo $OUTPUT->header();
82 if ($issuer) {
8445556b 83 echo $OUTPUT->heading(get_string('editissuer', 'tool_oauth2', s($issuer->get('name'))));
60237253
DW
84 } else {
85 echo $OUTPUT->heading(get_string('createnewissuer', 'tool_oauth2'));
86 }
87 $mform->display();
88 echo $OUTPUT->footer();
89 }
dc4b5685
DW
90} else if ($action == 'edittemplate') {
91
92 $type = optional_param('type', '', PARAM_ALPHA);
93 if (empty($type)) {
94 echo $OUTPUT->header();
95 echo $OUTPUT->heading(get_string('createfromtemplate', 'tool_oauth2'));
96 echo '<p>' . get_string('createfromtemplatedesc', 'tool_oauth2') . '</p>';
97 $params = ['action' => 'edittemplate', 'type' => 'google', 'sesskey' => sesskey()];
98 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
99 echo $renderer->single_button($addurl, get_string('createnewgoogleissuer', 'tool_oauth2'));
100 $params = ['action' => 'edittemplate', 'type' => 'microsoft', 'sesskey' => sesskey()];
101 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
102 echo $renderer->single_button($addurl, get_string('createnewmicrosoftissuer', 'tool_oauth2'));
103 $params = ['action' => 'edittemplate', 'type' => 'facebook', 'sesskey' => sesskey()];
104 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
105 echo $renderer->single_button($addurl, get_string('createnewfacebookissuer', 'tool_oauth2'));
106 echo $OUTPUT->footer();
107 } else {
108 require_sesskey();
109 $issuer = core\oauth2\api::create_standard_issuer($type);
110 $params = ['action' => 'edit', 'id' => $issuer->get('id')];
111 $editurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
112 redirect($editurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
113 }
60237253
DW
114
115} else if ($action == 'delete') {
116
117 if (!optional_param('confirm', false, PARAM_BOOL)) {
8445556b 118 $continueparams = ['action' => 'delete', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
60237253
DW
119 $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
120 $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
121 echo $OUTPUT->header();
8445556b 122 echo $OUTPUT->confirm(get_string('deleteconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
60237253
DW
123 echo $OUTPUT->footer();
124 } else {
125 require_sesskey();
8445556b 126 core\oauth2\api::delete_issuer($issuerid);
60237253
DW
127 redirect($PAGE->url, get_string('issuerdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
128 }
129
130} else if ($action == 'auth') {
131
132 if (!optional_param('confirm', false, PARAM_BOOL)) {
8445556b 133 $continueparams = ['action' => 'auth', 'id' => $issuerid, 'sesskey' => sesskey(), 'confirm' => true];
60237253
DW
134 $continueurl = new moodle_url('/admin/tool/oauth2/issuers.php', $continueparams);
135 $cancelurl = new moodle_url('/admin/tool/oauth2/issuers.php');
136 echo $OUTPUT->header();
8445556b 137 echo $OUTPUT->confirm(get_string('authconfirm', 'tool_oauth2', s($issuer->get('name'))), $continueurl, $cancelurl);
60237253
DW
138 echo $OUTPUT->footer();
139 } else {
140 require_sesskey();
8445556b 141 $params = ['sesskey' => sesskey(), 'id' => $issuerid, 'action' => 'auth', 'confirm' => true, 'response' => true];
60237253
DW
142 if (core\oauth2\api::connect_system_account($issuer, new moodle_url('/admin/tool/oauth2/issuers.php', $params))) {
143 redirect($PAGE->url, get_string('authconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
144 } else {
145 redirect($PAGE->url, get_string('authnotconnected', 'tool_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
146 }
147 }
148} else if ($action == 'moveup') {
149 require_sesskey();
8445556b 150 core\oauth2\api::move_up_issuer($issuerid);
60237253
DW
151 redirect($PAGE->url);
152
153} else if ($action == 'movedown') {
154 require_sesskey();
8445556b 155 core\oauth2\api::move_down_issuer($issuerid);
60237253
DW
156 redirect($PAGE->url);
157
158} else {
159 echo $OUTPUT->header();
870a4a82
DW
160 echo $OUTPUT->heading(get_string('pluginname', 'tool_oauth2'));
161 echo $OUTPUT->doc_link('OAuth2_Services', get_string('serviceshelp', 'tool_oauth2'));
60237253
DW
162 $issuers = core\oauth2\api::get_all_issuers();
163 echo $renderer->issuers_table($issuers);
164
165 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['action' => 'edit']);
166 echo $renderer->single_button($addurl, get_string('createnewissuer', 'tool_oauth2'));
dc4b5685
DW
167 $addurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['action' => 'edittemplate']);
168 echo $renderer->single_button($addurl, get_string('createnewstandardissuer', 'tool_oauth2'));
60237253 169 echo $OUTPUT->footer();
931c0234 170
60237253 171}