Implement login functionality

This commit is contained in:
Dmitrii Metelkin 2016-08-18 16:10:18 +10:00
parent 3e8c4138f0
commit 92555b7d28
2 changed files with 80 additions and 0 deletions

View file

@ -79,6 +79,59 @@ class auth_plugin_userkey extends auth_plugin_base {
return false; return false;
} }
/**
* Login user using userkey.
*
* This is basically customised core require_user_key_login().
*/
public function user_login_userkey() {
global $DB;
$keyvalue = required_param('key', PARAM_ALPHANUM);
$wantsurl = optional_param('wantsurl', '', PARAM_LOCALURL);
$options = array(
'script' => core_userkey_manager::CORE_USER_KEY_MANAGER_SCRIPT,
'value' => $keyvalue
);
if (!$key = $DB->get_record('user_private_key', $options)) {
print_error('invalidkey');
}
if (!empty($key->validuntil) and $key->validuntil < time()) {
print_error('expiredkey');
}
if ($key->iprestriction) {
$remoteaddr = getremoteaddr(null);
if (empty($remoteaddr) or !address_in_subnet($remoteaddr, $key->iprestriction)) {
print_error('ipmismatch');
}
}
if (!$user = $DB->get_record('user', array('id' => $key->userid))) {
print_error('invaliduserid');
}
if (!isset($this->userkeymanager)) {
$userkeymanager = new core_userkey_manager($user->id, $this->config);
$this->set_userkey_manager($userkeymanager);
}
$this->userkeymanager->delete_key();
$user = get_complete_user_data('id', $user->id);
complete_user_login($user);
if (!empty($wantsurl)) {
redirect($wantsurl);
} else {
redirect('/');
}
}
/** /**
* Don't store local passwords. * Don't store local passwords.
* *

27
login.php Normal file
View file

@ -0,0 +1,27 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Login page for auth_userkey.
*
* @package auth_userkey
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
get_auth_plugin('userkey')->user_login_userkey();