Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ on: [push, pull_request]

jobs:
ci:
uses: catalyst/catalyst-moodle-workflows/.github/workflows/ci.yml@main
uses: catalyst/catalyst-moodle-workflows/.github/workflows/ci.yml@main
secrets:
moodle_org_token: ${{ secrets.MOODLE_ORG_TOKEN }}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ application. The main idea is to make a web call to moodle and provide one of th
fields to find required user and generate one time login URL. A user can be redirected to this
URL to be log in to Moodle without typing username and password.

# Versions and branches

| Moodle Version | Branch |
|------------------|-------------------|
| Moodle 3.3 - 4.1 | MOODLE_33PLUS |
| Moodle 4.5+ | MOODLE_405_STABLE |

Using
-----
Expand Down
56 changes: 28 additions & 28 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
use auth_userkey\userkey_manager_interface;
use core_external\external_value;

require_once($CFG->libdir.'/authlib.php');
require_once($CFG->libdir . '/authlib.php');
require_once($CFG->dirroot . '/user/lib.php');

/**
* User key authentication plugin.
*/
class auth_plugin_userkey extends auth_plugin_base {

/**
* Default mapping field.
*/
Expand All @@ -53,7 +52,7 @@ class auth_plugin_userkey extends auth_plugin_base {
*
* @var array
*/
protected $defaults = array(
protected $defaults = [
'mappingfield' => self::DEFAULT_MAPPING_FIELD,
'keylifetime' => 60,
'iprestriction' => 0,
Expand All @@ -62,7 +61,7 @@ class auth_plugin_userkey extends auth_plugin_base {
'ssourl' => '',
'createuser' => false,
'updateuser' => false,
);
];

/**
* Constructor.
Expand Down Expand Up @@ -297,14 +296,16 @@ protected function create_user(array $data) {
throw new invalid_parameter_exception('Unable to create user, missing value(s): ' . implode(',', $missingfields));
}

if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
throw new invalid_parameter_exception('Username already exists: '.$user['username']);
if ($DB->record_exists('user', ['username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id])) {
throw new invalid_parameter_exception('Username already exists: ' . $user['username']);
}
if (!validate_email($user['email'])) {
throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
} else if (empty($CFG->allowaccountssameemail) &&
$DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) {
throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
throw new invalid_parameter_exception('Email address is invalid: ' . $user['email']);
} else if (
empty($CFG->allowaccountssameemail) &&
$DB->record_exists('user', ['email' => $user['email'], 'mnethostid' => $user['mnethostid']])
) {
throw new invalid_parameter_exception('Email address already exists: ' . $user['email']);
}

$userid = user_create_user($user);
Expand Down Expand Up @@ -341,20 +342,20 @@ protected function update_user(\stdClass $user, array $data) {
if (
$user->username != $userdata['username']
&&
$DB->record_exists('user', array('username' => $userdata['username'], 'mnethostid' => $CFG->mnet_localhost_id))
$DB->record_exists('user', ['username' => $userdata['username'], 'mnethostid' => $CFG->mnet_localhost_id])
) {
throw new invalid_parameter_exception('Username already exists: '.$userdata['username']);
throw new invalid_parameter_exception('Username already exists: ' . $userdata['username']);
}
if (!validate_email($userdata['email'])) {
throw new invalid_parameter_exception('Email address is invalid: '.$userdata['email']);
throw new invalid_parameter_exception('Email address is invalid: ' . $userdata['email']);
} else if (
empty($CFG->allowaccountssameemail)
&&
$user->email != $userdata['email']
&&
$DB->record_exists('user', array('email' => $userdata['email'], 'mnethostid' => $CFG->mnet_localhost_id))
$DB->record_exists('user', ['email' => $userdata['email'], 'mnethostid' => $CFG->mnet_localhost_id])
) {
throw new invalid_parameter_exception('Email address already exists: '.$userdata['email']);
throw new invalid_parameter_exception('Email address already exists: ' . $userdata['email']);
}
$userdata['id'] = $user->id;

Expand Down Expand Up @@ -402,10 +403,10 @@ protected function get_user(array $data) {

$mappingfield = $this->get_mapping_field();

$params = array(
$params = [
$mappingfield => $data[$mappingfield],
'mnethostid' => $CFG->mnet_localhost_id,
);
];

$user = $DB->get_record('user', $params);

Expand Down Expand Up @@ -476,11 +477,11 @@ public function get_login_url($data) {
* @return array
*/
public function get_allowed_mapping_fields() {
return array(
return [
'username' => get_string('username'),
'email' => get_string('email'),
'idnumber' => get_string('idnumber'),
);
];
}

/**
Expand All @@ -493,34 +494,34 @@ protected function get_mapping_parameter() {

switch ($mappingfield) {
case 'username':
$parameter = array(
$parameter = [
'username' => new external_value(
PARAM_USERNAME,
'Username'
),
);
];
break;

case 'email':
$parameter = array(
$parameter = [
'email' => new external_value(
PARAM_EMAIL,
'A valid email address'
),
);
];
break;

case 'idnumber':
$parameter = array(
$parameter = [
'idnumber' => new external_value(
PARAM_RAW,
'An arbitrary ID code number perhaps from the institution'
),
);
];
break;

default:
$parameter = array();
$parameter = [];
break;
}

Expand All @@ -533,7 +534,7 @@ protected function get_mapping_parameter() {
* @return array
*/
protected function get_user_fields_parameters() {
$parameters = array();
$parameters = [];

if ($this->is_ip_restriction_enabled()) {
$parameters['ip'] = new external_value(
Expand Down Expand Up @@ -594,7 +595,6 @@ protected function should_login_redirect() {
if (isset($this->config->ssourl) && $this->config->ssourl != '' && !$skipsso) {
return true;
}

}

/**
Expand Down
9 changes: 4 additions & 5 deletions classes/core_userkey_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_userkey_manager implements userkey_manager_interface {

/**
* This script script required by core create_user_key().
*/
Expand Down Expand Up @@ -108,10 +107,10 @@ public function delete_keys($userid) {
public function validate_key($keyvalue) {
global $DB;

$options = array(
$options = [
'script' => self::CORE_USER_KEY_MANAGER_SCRIPT,
'value' => $keyvalue
);
'value' => $keyvalue,
];

if (!$key = $DB->get_record('user_private_key', $options)) {
throw new \moodle_exception('invalidkey');
Expand All @@ -123,7 +122,7 @@ public function validate_key($keyvalue) {

$this->validate_ip_address($key);

if (!$user = $DB->get_record('user', array('id' => $key->userid))) {
if (!$user = $DB->get_record('user', ['id' => $key->userid])) {
throw new \moodle_exception('invaliduserid');
}
return $key;
Expand Down
2 changes: 0 additions & 2 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements null_provider {

use legacy_polyfill;

/**
Expand All @@ -47,5 +46,4 @@ class provider implements null_provider {
public static function _get_reason() {
return 'privacy:metadata';
}

}
1 change: 0 additions & 1 deletion classes/userkey_manager_interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ public function delete_keys($userid);
* @throws \moodle_exception If provided key is not valid.
*/
public function validate_key($keyvalue);

}
14 changes: 7 additions & 7 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

defined('MOODLE_INTERNAL') || die();

$capabilities = array(
'auth/userkey:generatekey' => array(
'riskbitmask' => RISK_PERSONAL | RISK_SPAM | RISK_XSS ,
$capabilities = [
'auth/userkey:generatekey' => [
'riskbitmask' => RISK_PERSONAL | RISK_SPAM | RISK_XSS,

'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
),
),
);
'archetypes' => [
],
],
];
18 changes: 9 additions & 9 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@

defined('MOODLE_INTERNAL') || die;

$functions = array(
'auth_userkey_request_login_url' => array(
$functions = [
'auth_userkey_request_login_url' => [
'classname' => 'auth_userkey_external',
'methodname' => 'request_login_url',
'classpath' => 'auth/userkey/externallib.php',
'description' => 'Return one time key based login URL',
'type' => 'write',
'capabilities' => 'auth/userkey:generatekey',
)
);
],
];

$services = array(
'User key authentication web service' => array(
'functions' => array ('auth_userkey_request_login_url'),
$services = [
'User key authentication web service' => [
'functions' => ['auth_userkey_request_login_url'],
'restrictedusers' => 1,
'enabled' => 1,
)
);
],
];
2 changes: 1 addition & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function xmldb_auth_userkey_upgrade($oldversion) {

if ($oldversion < 2018050200) {
// Confirm all previously created users.
$DB->execute("UPDATE {user} SET confirmed=? WHERE auth=?", array(1, 'userkey'));
$DB->execute("UPDATE {user} SET confirmed=? WHERE auth=?", [1, 'userkey']);
upgrade_plugin_savepoint(true, 2018050200, 'auth', 'userkey');
}

Expand Down
16 changes: 7 additions & 9 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class auth_userkey_external extends external_api {

/**
* Return request_login_url webservice parameters.
*
* @return \external_function_parameters
*/
public static function request_login_url_parameters() {
return new external_function_parameters(
array(
[
'user' => new external_single_structure(
get_auth_plugin('userkey')->get_request_login_url_user_parameters()
)
)
),
]
);
}

Expand All @@ -77,9 +76,9 @@ public static function request_login_url($user) {
$auth = get_auth_plugin('userkey');
$loginurl = $auth->get_login_url($user);

return array(
return [
'loginurl' => $loginurl,
);
];
}

/**
Expand All @@ -89,10 +88,9 @@ public static function request_login_url($user) {
*/
public static function request_login_url_returns() {
return new external_single_structure(
array(
[
'loginurl' => new external_value(PARAM_RAW, 'Login URL for a user to log in'),
)
]
);
}

}
Loading
Loading