ff9a89f5238506d599fb908f836e0e206b100844
[moodle.git] / auth / oauth2 / linkedlogins.php
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/>.
17 /**
18  * OAuth 2 Linked login configuration page.
19  *
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
23  */
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);
36 require_login();
38 $action = optional_param('action', '', PARAM_ALPHAEXT);
39 if ($action == 'new') {
40     require_sesskey();
41     $issuerid = required_param('issuerid', PARAM_INT);
42     $issuer = \core\oauth2\api::get_issuer($issuerid);
45     // We do a login dance with this issuer.
46     $addparams = ['action' => 'new', 'issuerid' => $issuerid, 'sesskey' => sesskey()];
47     $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
48     $client = \core\oauth2\api::get_user_oauth_client($issuer, $addurl);
50     if (optional_param('logout', false, PARAM_BOOL)) {
51         $client->log_out();
52     }
54     if (!$client->is_logged_in()) {
55         redirect($client->get_login_url());
56     }
58     $userinfo = $client->get_userinfo();
60     if (!empty($userinfo)) {
61         try {
62             \auth_oauth2\api::link_login($userinfo, $issuer);
63             redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
64         } catch (Exception $e) {
65             redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
66         }
67     } else {
68         redirect($PAGE->url, get_string('notloggedin', 'auth_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
69     }
70 } else if ($action == 'delete') {
71     require_sesskey();
72     $linkedloginid = required_param('linkedloginid', PARAM_INT);
74     auth_oauth2\api::delete_linked_login($linkedloginid);
75     redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
76 }
78 $renderer = $PAGE->get_renderer('auth_oauth2');
80 $linkedloginid = optional_param('id', '', PARAM_RAW);
81 $linkedlogin = null;
83 echo $OUTPUT->header();
84 echo $OUTPUT->heading(get_string('linkedlogins', 'auth_oauth2'));
85 echo $OUTPUT->doc_link('Linked_Logins', get_string('linkedloginshelp', 'auth_oauth2'));
86 auth_oauth2\api::clean_orphaned_linked_logins();
87 $linkedlogins = auth_oauth2\api::get_linked_logins();
89 echo $renderer->linked_logins_table($linkedlogins);
91 $issuers = \core\oauth2\api::get_all_issuers();
93 foreach ($issuers as $issuer) {
94     if (!$issuer->is_authentication_supported()) {
95         continue;
96     }
98     $addparams = ['action' => 'new', 'issuerid' => $issuer->get('id'), 'sesskey' => sesskey(), 'logout' => true];
99     $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
100     echo $renderer->single_button($addurl, get_string('createnewlinkedlogin', 'auth_oauth2', s($issuer->get('name'))));
102 echo $OUTPUT->footer();