Merge branch 'MDL-62144-master' of git://github.com/damyon/moodle
[moodle.git] / auth / mnet / land.php
blob07317d1cd5b1aeec54ffc4d468fad6b535a26a8a
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.
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 * Authentication Plugin: Moodle Network Authentication
19 * Multiple host authentication support for Moodle Network.
21 * @package auth_mnet
22 * @author Martin Dougiamas
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 require_once __DIR__ . '/../../config.php';
27 require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
29 // grab the GET params
30 $token = required_param('token', PARAM_BASE64);
31 $remotewwwroot = required_param('idp', PARAM_URL);
32 $wantsurl = required_param('wantsurl', PARAM_LOCALURL);
33 $wantsremoteurl = optional_param('remoteurl', false, PARAM_BOOL);
35 $url = new moodle_url('/auth/mnet/jump.php', array('token'=>$token, 'idp'=>$remotewwwroot, 'wantsurl'=>$wantsurl));
36 if ($wantsremoteurl !== false) $url->param('remoteurl', $wantsremoteurl);
37 $PAGE->set_url($url);
38 $PAGE->set_context(context_system::instance());
40 $site = get_site();
42 if (!is_enabled_auth('mnet')) {
43 print_error('mnetdisable');
46 // confirm the MNET session
47 $mnetauth = get_auth_plugin('mnet');
48 $remotepeer = new mnet_peer();
49 $remotepeer->set_wwwroot($remotewwwroot);
50 // this creates the local user account if necessary, or updates it if it already exists
51 $localuser = $mnetauth->confirm_mnet_session($token, $remotepeer);
53 // log in
54 $user = get_complete_user_data('id', $localuser->id, $localuser->mnethostid);
55 complete_user_login($user);
56 // now that we've logged in, set up the mnet session properly
57 $mnetauth->update_mnet_session($user, $token, $remotepeer);
59 if (!empty($localuser->mnet_foreign_host_array)) {
60 $USER->mnet_foreign_host_array = $localuser->mnet_foreign_host_array;
63 // redirect
64 if ($wantsremoteurl) {
65 redirect($remotewwwroot . $wantsurl);
67 redirect($CFG->wwwroot . $wantsurl);