2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * OAuth 2 Linked login configuration page.
20 * @package auth_oauth2
21 * @copyright 2017 Damyon Wiese <damyon@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../../config.php');
26 require_once($CFG->libdir
.'/adminlib.php');
27 require_once($CFG->libdir
.'/tablelib.php');
29 $PAGE->set_url('/auth/oauth2/linkedlogins.php');
30 $PAGE->set_context(context_user
::instance($USER->id
));
31 $PAGE->set_pagelayout('admin');
32 $strheading = get_string('linkedlogins', 'auth_oauth2');
33 $PAGE->set_title($strheading);
34 $PAGE->set_heading($strheading);
38 if (!\auth_oauth
2\api
::is_enabled()) {
39 throw new \
moodle_exception('notenabled', 'auth_oauth2');
42 $action = optional_param('action', '', PARAM_ALPHAEXT
);
43 if ($action == 'new') {
45 $issuerid = required_param('issuerid', PARAM_INT
);
46 $issuer = \core\oauth2\api
::get_issuer($issuerid);
48 if (!$issuer->is_available_for_login()) {
49 throw new \
moodle_exception('issuernologin', 'auth_oauth2');
52 // We do a login dance with this issuer.
53 $addparams = ['action' => 'new', 'issuerid' => $issuerid, 'sesskey' => sesskey()];
54 $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
55 $client = \core\oauth2\api
::get_user_oauth_client($issuer, $addurl);
57 if (optional_param('logout', false, PARAM_BOOL
)) {
61 if (!$client->is_logged_in()) {
62 redirect($client->get_login_url());
65 $userinfo = $client->get_userinfo();
67 if (!empty($userinfo)) {
69 \auth_oauth
2\api
::link_login($userinfo, $issuer);
70 redirect($PAGE->url
, get_string('changessaved'), null, \core\output\notification
::NOTIFY_SUCCESS
);
71 } catch (Exception
$e) {
72 redirect($PAGE->url
, $e->getMessage(), null, \core\output\notification
::NOTIFY_ERROR
);
75 redirect($PAGE->url
, get_string('notloggedin', 'auth_oauth2'), null, \core\output\notification
::NOTIFY_ERROR
);
77 } else if ($action == 'delete') {
79 $linkedloginid = required_param('linkedloginid', PARAM_INT
);
81 auth_oauth2\api
::delete_linked_login($linkedloginid);
82 redirect($PAGE->url
, get_string('changessaved'), null, \core\output\notification
::NOTIFY_SUCCESS
);
85 $renderer = $PAGE->get_renderer('auth_oauth2');
87 $linkedloginid = optional_param('id', '', PARAM_RAW
);
90 auth_oauth2\api
::clean_orphaned_linked_logins();
92 $issuers = \core\oauth2\api
::get_all_issuers(true);
94 $anyshowinloginpage = false;
95 $issuerbuttons = array();
96 foreach ($issuers as $issuer) {
97 if (!$issuer->is_available_for_login()) {
100 $anyshowinloginpage = true;
102 $addparams = ['action' => 'new', 'issuerid' => $issuer->get('id'), 'sesskey' => sesskey(), 'logout' => true];
103 $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
104 $issuerbuttons[$issuer->get('id')] = $renderer->single_button($addurl, get_string('createnewlinkedlogin', 'auth_oauth2',
105 s($issuer->get_display_name())));
108 if (!$anyshowinloginpage) {
109 // Just a notification that we can't make it.
110 $preferencesurl = new moodle_url('/user/preferences.php');
111 redirect($preferencesurl, get_string('noissuersavailable', 'auth_oauth2'), null, \core\output\notification
::NOTIFY_WARNING
);
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading(get_string('linkedlogins', 'auth_oauth2'));
116 echo $OUTPUT->doc_link('Linked_Logins', get_string('linkedloginshelp', 'auth_oauth2'));
117 $linkedlogins = auth_oauth2\api
::get_linked_logins();
119 echo $renderer->linked_logins_table($linkedlogins);
121 foreach ($issuerbuttons as $issuerbutton) {
125 echo $OUTPUT->footer();