Update core key manager in case to implement a new interface
This commit is contained in:
parent
1c8f115935
commit
04a4f4f573
1 changed files with 61 additions and 21 deletions
|
@ -74,24 +74,11 @@ class core_userkey_manager implements userkey_manager_interface {
|
|||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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, $allowedips = null) {
|
||||
$this->userid = $userid;
|
||||
public function __construct(\stdClass $config) {
|
||||
$this->config = $config;
|
||||
|
||||
if (isset($config->iprestriction) && !empty($config->iprestriction)) {
|
||||
if ($allowedips) {
|
||||
$this->iprestriction = $allowedips;
|
||||
} else {
|
||||
$this->iprestriction = getremoteaddr($this->iprestriction);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config->keylifetime) && (int)$config->keylifetime > 0) {
|
||||
$this->validuntil = time() + $config->keylifetime;
|
||||
} else {
|
||||
|
@ -102,14 +89,26 @@ class core_userkey_manager implements userkey_manager_interface {
|
|||
/**
|
||||
* Create a user key.
|
||||
*
|
||||
* @param int $userid User ID.
|
||||
* @param null|array $allowedips A list of allowed ips for this key.
|
||||
*
|
||||
* @return string Generated key.
|
||||
*/
|
||||
public function create_key() {
|
||||
$this->delete_key();
|
||||
public function create_key($userid, $allowedips = null) {
|
||||
$this->delete_keys($userid);
|
||||
|
||||
if (isset($this->config->iprestriction) && !empty($this->config->iprestriction)) {
|
||||
if ($allowedips) {
|
||||
$this->iprestriction = $allowedips;
|
||||
} else {
|
||||
$this->iprestriction = getremoteaddr($this->iprestriction);
|
||||
}
|
||||
}
|
||||
|
||||
$this->userkey = create_user_key(
|
||||
self::CORE_USER_KEY_MANAGER_SCRIPT,
|
||||
$this->userid,
|
||||
$this->userid,
|
||||
$userid,
|
||||
$userid,
|
||||
$this->iprestriction,
|
||||
$this->validuntil
|
||||
);
|
||||
|
@ -118,10 +117,51 @@ class core_userkey_manager implements userkey_manager_interface {
|
|||
}
|
||||
|
||||
/**
|
||||
* Delete all user keys.
|
||||
* Delete all keys for a specific user.
|
||||
*
|
||||
* @param int $userid User ID.
|
||||
*/
|
||||
public function delete_key() {
|
||||
delete_user_key(self::CORE_USER_KEY_MANAGER_SCRIPT, $this->userid);
|
||||
public function delete_keys($userid) {
|
||||
delete_user_key(self::CORE_USER_KEY_MANAGER_SCRIPT, $userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates key and returns key data object if valid.
|
||||
*
|
||||
* @param string $keyvalue User key value.
|
||||
*
|
||||
* @return object Key object including userid property.
|
||||
*
|
||||
* @throws \moodle_exception If provided key is not valid.
|
||||
*/
|
||||
public function validate_key($keyvalue) {
|
||||
global $DB;
|
||||
|
||||
$options = array(
|
||||
'script' => self::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');
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue