Add allowedips parameter to the core key manager
This commit is contained in:
parent
816c6f92e9
commit
ec059a2191
2 changed files with 62 additions and 2 deletions
|
@ -76,15 +76,20 @@ class core_userkey_manager implements userkey_manager_interface {
|
|||
*
|
||||
* @param $userid
|
||||
* @param \stdClass $config
|
||||
* @param null|string $allowedips
|
||||
*
|
||||
* @throws \Exception If there is no user with provided userid.
|
||||
*/
|
||||
public function __construct($userid, \stdClass $config) {
|
||||
public function __construct($userid, \stdClass $config, $allowedips = null) {
|
||||
$this->userid = $userid;
|
||||
$this->config = $config;
|
||||
|
||||
if (isset($config->iprestriction) && !empty($config->iprestriction)) {
|
||||
$this->iprestriction = getremoteaddr(null);
|
||||
if ($allowedips) {
|
||||
$this->iprestriction = $allowedips;
|
||||
} else {
|
||||
$this->iprestriction = getremoteaddr($this->iprestriction);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config->keylifetime) && (int)$config->keylifetime > 0) {
|
||||
|
|
|
@ -130,6 +130,33 @@ class core_userkey_manager_testcase extends advanced_testcase {
|
|||
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that key gets created correctly if config option iprestriction is set to true and we set allowedips.
|
||||
*/
|
||||
public function test_create_correct_key_if_iprestriction_is_true_and_we_set_allowedips() {
|
||||
global $DB;
|
||||
|
||||
$this->config->iprestriction = true;
|
||||
$manager = new core_userkey_manager($this->user->id, $this->config, '192.168.1.3');
|
||||
$value = $manager->create_key();
|
||||
|
||||
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
||||
|
||||
$expectedvalue = $value;
|
||||
$expecteduserid = $this->user->id;
|
||||
$expectedscript = 'auth/userkey';
|
||||
$expectedinstance = $this->user->id;
|
||||
$expectediprestriction = '192.168.1.3';
|
||||
$expectedvaliduntil = time() + 60;
|
||||
|
||||
$this->assertEquals($expectedvalue, $actualkey->value);
|
||||
$this->assertEquals($expecteduserid, $actualkey->userid);
|
||||
$this->assertEquals($expectedscript, $actualkey->script);
|
||||
$this->assertEquals($expectedinstance, $actualkey->instance);
|
||||
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
||||
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that key gets created correctly if config option iprestriction is set to false.
|
||||
*/
|
||||
|
@ -158,6 +185,34 @@ class core_userkey_manager_testcase extends advanced_testcase {
|
|||
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that key gets created correctly if config option iprestriction is set to false and we set allowedips.
|
||||
*/
|
||||
public function test_create_correct_key_if_iprestriction_is_falseand_we_set_allowedips() {
|
||||
global $DB;
|
||||
|
||||
$this->config->iprestriction = false;
|
||||
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
||||
$manager = new core_userkey_manager($this->user->id, $this->config, '192.168.1.1');
|
||||
$value = $manager->create_key();
|
||||
|
||||
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
||||
|
||||
$expectedvalue = $value;
|
||||
$expecteduserid = $this->user->id;
|
||||
$expectedscript = 'auth/userkey';
|
||||
$expectedinstance = $this->user->id;
|
||||
$expectediprestriction = null;
|
||||
$expectedvaliduntil = time() + 60;
|
||||
|
||||
$this->assertEquals($expectedvalue, $actualkey->value);
|
||||
$this->assertEquals($expecteduserid, $actualkey->userid);
|
||||
$this->assertEquals($expectedscript, $actualkey->script);
|
||||
$this->assertEquals($expectedinstance, $actualkey->instance);
|
||||
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
||||
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that key gets created correctly if config option iprestriction is set to a string.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue