From 7d25db3fda1a623552d8e04fa94b6cb2b89e56e3 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Wed, 17 Aug 2016 15:06:46 +1000 Subject: [PATCH] WIP: add initital test and implementation get_login_url --- auth.php | 63 +++++++++++++++++++++++++++++++++++- tests/auth_plugin_test.php | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/auth.php b/auth.php index 9901adf..c72497c 100644 --- a/auth.php +++ b/auth.php @@ -24,6 +24,9 @@ defined('MOODLE_INTERNAL') || die(); +use auth_userkey\core_userkey_manager; +use auth_userkey\userkey_manager_interface; + require_once($CFG->libdir.'/authlib.php'); /** @@ -31,12 +34,25 @@ require_once($CFG->libdir.'/authlib.php'); */ class auth_plugin_userkey extends auth_plugin_base { + const DEFAULT_MAPPING_FIELD = 'email'; + /** + * User key manager. + * + * @var userkey_manager_interface + */ + private $userkeymanager; + /** * Constructor. */ public function __construct() { $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 } /** @@ -77,4 +93,49 @@ class auth_plugin_userkey extends auth_plugin_base { public function can_change_password() { return false; } + + /** + * @param \auth_userkey\userkey_manager_interface $keymanager + */ + public function set_userkey_manager(userkey_manager_interface $keymanager) { + $this->userkeymanager = $keymanager; + } + + + /** + * @return string + */ + public function get_mapping_field() { + if (isset($this->config->mappingfield) && !empty($this->config->mappingfield)) { + return $this->config->mappingfield; + } + + return self::DEFAULT_MAPPING_FIELD; + } + + public function get_login_url($user) { + global $CFG, $DB; + + $mappingfield = $this->get_mapping_field(); + + if (!isset($user[$mappingfield]) || empty($user[$mappingfield])) { + throw new invalid_parameter_exception('User field "' . $mappingfield . '" is not set or empty.'); + } + +// $params = array($mappingfield => $user[$mappingfield], 'mnethostid' => $CFG->mnet_localhost_id); +// +// if (!$user = $DB->get_record('user', $params) ) { +// throw new invalid_parameter_exception('User is not exist'); +// } +// +// if (!isset($this->userkeymanager)) { +// $keymanager = new auth_userkey\core_userkey_manager($user->id, $this->config); +// $this->set_userkey_manager($keymanager); +// } +// +// $userkey = $this->userkeymanager->create_key(); +// +// return $CFG->wwwroot . '/auth/userkey/login.php?key=' . $userkey; + } + } diff --git a/tests/auth_plugin_test.php b/tests/auth_plugin_test.php index d63a401..34bc0ed 100644 --- a/tests/auth_plugin_test.php +++ b/tests/auth_plugin_test.php @@ -83,4 +83,69 @@ class auth_plugin_userkey_testcase extends advanced_testcase { public function test_auth_plugin_does_not_allow_to_change_passwords() { $this->assertFalse($this->auth->can_change_password()); } + + public function test_get_default_mapping_field() { + $expected = 'email'; + $actual = $this->auth->get_mapping_field(); + + $this->assertEquals($expected, $actual); + } + + public function test_get_mapping_field() { + set_config('mappingfield', 'username', 'auth_userkey'); + $this->auth = new auth_plugin_userkey(); + + $expected = 'username'; + $actual = $this->auth->get_mapping_field(); + + $this->assertEquals($expected, $actual); + } + + /** + * @expectedException \invalid_parameter_exception + */ + public function test_throwing_exception_if_mapping_field_is_not_provided() { + $user = array(); + $actual = $this->auth->get_login_url($user); + } + + /** + * + */ + public function test_text_of_throwing_exception_if_mapping_field_is_not_provided() { + $user = array(); + + try { + $actual = $this->auth->get_login_url($user); + } catch (\invalid_parameter_exception $e) { + $actual = $e->getMessage(); + $expected = 'Invalid parameter value detected (User field "email" is not set or empty.)'; + + $this->assertEquals($expected, $actual); + } + + set_config('mappingfield', 'username', 'auth_userkey'); + $this->auth = new auth_plugin_userkey(); + try { + $actual = $this->auth->get_login_url($user); + } catch (\invalid_parameter_exception $e) { + $actual = $e->getMessage(); + $expected = 'Invalid parameter value detected (User field "username" is not set or empty.)'; + + $this->assertEquals($expected, $actual); + } + } + + + +// public function test_throwing_exception_if_matching_field_is_not_provided() { +// global $CFG; +// +// $user = array(); +// +// $expected = $CFG->wwwroot . '/auth/userkey/login.php?key='; +// $actual = $this->auth->get_login_url($user); +// +// $this->assertEquals($expected, $actual); +// } }