d8f68f142991b64ec357707317875cdf5060e217
[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         \auth_oauth2\api::link_login($userinfo, $issuer);
62         redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
63     } else {
64         redirect($PAGE->url, get_string('notloggedin', 'auth_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
65     }
66 } else if ($action == 'delete') {
67     require_sesskey();
68     $linkedloginid = required_param('linkedloginid', PARAM_INT);
70     auth_oauth2\api::delete_linked_login($linkedloginid);
71     redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
72 }
74 $renderer = $PAGE->get_renderer('auth_oauth2');
76 $linkedloginid = optional_param('id', '', PARAM_RAW);
77 $linkedlogin = null;
79 echo $OUTPUT->header();
80 echo $OUTPUT->heading(get_string('linkedlogins', 'auth_oauth2'));
81 echo $OUTPUT->doc_link('Linked_Logins', get_string('linkedloginshelp', 'auth_oauth2'));
82 auth_oauth2\api::clean_orphaned_linked_logins();
83 $linkedlogins = auth_oauth2\api::get_linked_logins();
85 echo $renderer->linked_logins_table($linkedlogins);
87 $issuers = \core\oauth2\api::get_all_issuers();
89 foreach ($issuers as $issuer) {
90     if (!$issuer->is_authentication_supported()) {
91         continue;
92     }
94     $addparams = ['action' => 'new', 'issuerid' => $issuer->get('id'), 'sesskey' => sesskey(), 'logout' => true];
95     $addurl = new moodle_url('/auth/oauth2/linkedlogins.php', $addparams);
96     echo $renderer->single_button($addurl, get_string('createnewlinkedlogin', 'auth_oauth2', s($issuer->get('name'))));
97 }
98 echo $OUTPUT->footer();