Some clean up in auth.php

This commit is contained in:
Dmitrii Metelkin 2016-08-17 16:02:11 +10:00
parent 8b3534c803
commit dfae36ad51

View file

@ -34,7 +34,11 @@ require_once($CFG->libdir.'/authlib.php');
*/ */
class auth_plugin_userkey extends auth_plugin_base { class auth_plugin_userkey extends auth_plugin_base {
/**
* Default mapping field.
*/
const DEFAULT_MAPPING_FIELD = 'email'; const DEFAULT_MAPPING_FIELD = 'email';
/** /**
* User key manager. * User key manager.
* *
@ -48,11 +52,6 @@ class auth_plugin_userkey extends auth_plugin_base {
public function __construct() { public function __construct() {
$this->authtype = 'userkey'; $this->authtype = 'userkey';
$this->config = get_config('auth_userkey'); $this->config = get_config('auth_userkey');
//$this->config->mappingfield string
//$this->config->iprestriction bool
//$this->config->keylifetime string
//$this->config->createuser bool
} }
/** /**
@ -95,14 +94,17 @@ class auth_plugin_userkey extends auth_plugin_base {
} }
/** /**
* Set userkey manager.
*
* @param \auth_userkey\userkey_manager_interface $keymanager * @param \auth_userkey\userkey_manager_interface $keymanager
*/ */
public function set_userkey_manager(userkey_manager_interface $keymanager) { public function set_userkey_manager(userkey_manager_interface $keymanager) {
$this->userkeymanager = $keymanager; $this->userkeymanager = $keymanager;
} }
/** /**
* Return mapping field to find a lms user.
*
* @return string * @return string
*/ */
public function get_mapping_field() { public function get_mapping_field() {
@ -113,29 +115,71 @@ class auth_plugin_userkey extends auth_plugin_base {
return self::DEFAULT_MAPPING_FIELD; return self::DEFAULT_MAPPING_FIELD;
} }
/**
* Check if we need to create a new user.
*
* @return bool
*/
protected function should_create_user() {
if (isset($this->config->createuser)) {
return $this->config->createuser;
}
return false;
}
/**
* Create a new user.
*/
protected function create_user() {
// TODO:
// 1. Validate user
// 2. Create user.
// 3. Throw exception if something went wrong.
}
/**
* Return login URL.
*
* @param array|stdClass $user User data.
*
* @return string Login URL.
*
* @throws \invalid_parameter_exception
*/
public function get_login_url($user) { public function get_login_url($user) {
global $CFG, $DB; global $CFG, $DB;
$user = (array)$user;
$mappingfield = $this->get_mapping_field(); $mappingfield = $this->get_mapping_field();
if (!isset($user[$mappingfield]) || empty($user[$mappingfield])) { if (!isset($user[$mappingfield]) || empty($user[$mappingfield])) {
throw new invalid_parameter_exception('User field "' . $mappingfield . '" is not set or empty.'); throw new invalid_parameter_exception('Required field "' . $mappingfield . '" is not set or empty.');
} }
// $params = array($mappingfield => $user[$mappingfield], 'mnethostid' => $CFG->mnet_localhost_id); $params = array(
// $mappingfield => $user[$mappingfield],
// if (!$user = $DB->get_record('user', $params) ) { 'mnethostid' => $CFG->mnet_localhost_id,
// throw new invalid_parameter_exception('User is not exist'); );
// }
// $user = $DB->get_record('user', $params);
// if (!isset($this->userkeymanager)) {
// $keymanager = new auth_userkey\core_userkey_manager($user->id, $this->config); if (empty($user)) {
// $this->set_userkey_manager($keymanager); if (!$this->should_create_user()) {
// } throw new invalid_parameter_exception('User is not exist');
// } else {
// $userkey = $this->userkeymanager->create_key(); $user = $this->create_user();
// }
// return $CFG->wwwroot . '/auth/userkey/login.php?key=' . $userkey; }
if (!isset($this->userkeymanager)) {
$userkeymanager = new core_userkey_manager($user->id, $this->config);
$this->set_userkey_manager($userkeymanager);
}
$userkey = $this->userkeymanager->create_key();
return $CFG->wwwroot . '/auth/userkey/login.php?key=' . $userkey;
} }
} }