Skip to content

Commit 1cdfbfc

Browse files
authored
Merge pull request #111 from MagnusSamuelsson/feature/add-id-auth
Added possibility to sign in users by internal user database id
2 parents b661ae3 + 95598bc commit 1cdfbfc

File tree

6 files changed

+81
-16
lines changed

6 files changed

+81
-16
lines changed

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ URL to be log in to Moodle without typing username and password.
1212

1313
# Versions and branches
1414

15-
| Moodle Version | Branch |
15+
| Moodle Version | Branch |
1616
|------------------|-------------------|
17-
| Moodle 4.5+ | MOODLE_405_STABLE |
17+
| Moodle 4.5+ | MOODLE_405_STABLE |
1818
| Moodle 3.3 - 4.1 | MOODLE_33PLUS |
1919

2020
Using
@@ -64,6 +64,14 @@ For example XML-RPC (PHP structure) description for different mapping field sett
6464
[idnumber] => string
6565
)
6666

67+
***Database Id***
68+
69+
[user] =>
70+
Array
71+
(
72+
[id] => int
73+
)
74+
6775
***Web service will return following structure or standard Moodle webservice error message.***
6876

6977
Array
@@ -104,21 +112,21 @@ E.g. http://yourmoodle.com/login/index.php?enrolkey_skipsso=1
104112

105113
**Logout URL**
106114

107-
If you need to logout users after they logged out from the external application, you can redirect them
108-
to logout script with required parameter "return".
115+
If you need to logout users after they logged out from the external application, you can redirect them
116+
to logout script with required parameter "return".
109117

110-
E.g. http://yourmoodle.com/auth/userkey/logout.php?return=www.google.com
118+
E.g. http://yourmoodle.com/auth/userkey/logout.php?return=www.google.com
111119

112120

113-
Users will be logged out from Moodle and then redirected to the provided URL.
114-
In case when a user session is already expired, the user will be still redirected.
115-
121+
Users will be logged out from Moodle and then redirected to the provided URL.
122+
In case when a user session is already expired, the user will be still redirected.
123+
116124

117125
**Example client**
118126

119127
**Note:** the code below is not for production use. It's just a quick and dirty way to test the functionality.
120128

121-
The code below defines a function that can be used to obtain a login url.
129+
The code below defines a function that can be used to obtain a login url.
122130
You will need to add/remove parameters depending on whether you have update/create user enabled and which mapping field you are using.
123131

124132
The required library curl can be obtained from https://github.com/moodlehq/sample-ws-clients
@@ -136,7 +144,7 @@ The required library curl can be obtained from https://github.com/moodlehq/sampl
136144
*/
137145
function getloginurl($useremail, $firstname, $lastname, $username, $courseid = null, $modname = null, $activityid = null) {
138146
require_once('curl.php');
139-
147+
140148
$token = 'YOUR_TOKEN';
141149
$domainname = 'http://MOODLE_WWW_ROOT';
142150
$functionname = 'auth_userkey_request_login_url';
@@ -145,19 +153,19 @@ function getloginurl($useremail, $firstname, $lastname, $username, $courseid = n
145153
'user' => [
146154
'firstname' => $firstname, // You will not need this parameter, if you are not creating/updating users
147155
'lastname' => $lastname, // You will not need this parameter, if you are not creating/updating users
148-
'username' => $username,
156+
'username' => $username,
149157
'email' => $useremail,
150158
]
151159
];
152160

153161
$serverurl = $domainname . '/webservice/rest/server.php' . '?wstoken=' . $token . '&wsfunction=' . $functionname . '&moodlewsrestformat=json';
154-
$curl = new curl; // The required library curl can be obtained from https://github.com/moodlehq/sample-ws-clients
162+
$curl = new curl; // The required library curl can be obtained from https://github.com/moodlehq/sample-ws-clients
155163

156164
try {
157165
$resp = $curl->post($serverurl, $param);
158166
$resp = json_decode($resp);
159167
if ($resp && !empty($resp->loginurl)) {
160-
$loginurl = $resp->loginurl;
168+
$loginurl = $resp->loginurl;
161169
}
162170
} catch (Exception $ex) {
163171
return false;
@@ -192,7 +200,7 @@ https://www.catalyst-au.net/
192200

193201
# Contributing and Support
194202

195-
Issues, and pull requests using github are welcome and encouraged!
203+
Issues, and pull requests using github are welcome and encouraged!
196204

197205
https://github.com/catalyst/moodle-auth_userkey/issues
198206

auth.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ public function get_allowed_mapping_fields() {
481481
'username' => get_string('username'),
482482
'email' => get_string('email'),
483483
'idnumber' => get_string('idnumber'),
484+
'id' => get_string('userid', 'auth_userkey'),
484485
];
485486
}
486487

@@ -519,6 +520,15 @@ protected function get_mapping_parameter() {
519520
),
520521
];
521522
break;
523+
case 'id':
524+
$parameter = [
525+
'id' => new external_value(
526+
PARAM_INT,
527+
'Database ID of the user'
528+
),
529+
];
530+
break;
531+
522532

523533
default:
524534
$parameter = [];

lang/en/auth_userkey.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@
5252
$string['ssourl_desc'] = 'URL of the SSO host to redirect users to. If defined users will be redirected here on login instead of the Moodle Login page';
5353
$string['updateuser'] = 'Update user?';
5454
$string['updateuser_desc'] = 'If enabled, users will be updated with the properties supplied when the webservice is called.';
55+
$string['userid'] = 'User ID';
5556
$string['userkey:generatekey'] = 'Generate login user key';

tests/auth_plugin_test.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ public function test_throwing_exception_if_mapping_field_idnumber_is_not_provide
231231
$actual = $this->auth->get_login_url($user);
232232
}
233233

234+
/**
235+
* Test that auth plugin throws correct exception if id mapping field is not provided, but set in configs.
236+
*/
237+
public function test_throwing_exception_if_mapping_field_id_is_not_provided(): void {
238+
$user = [];
239+
set_config('mappingfield', 'id', 'auth_userkey');
240+
$this->auth = new auth_plugin_userkey();
241+
242+
$this->expectException(invalid_parameter_exception::class);
243+
$this->expectExceptionMessage('Invalid parameter value detected (Required field "id" is not set or empty.)');
244+
245+
$actual = $this->auth->get_login_url($user);
246+
}
247+
234248
/**
235249
* Test that auth plugin throws correct exception if we trying to request not existing user.
236250
*/
@@ -572,6 +586,7 @@ public function test_get_allowed_mapping_fields_list(): void {
572586
'username' => 'Username',
573587
'email' => 'Email address',
574588
'idnumber' => 'ID number',
589+
'id' => 'User ID',
575590
];
576591

577592
$actual = $this->auth->get_allowed_mapping_fields();
@@ -622,6 +637,20 @@ public function test_get_request_login_url_user_parameters_based_on_plugin_confi
622637
$actual = $this->auth->get_request_login_url_user_parameters();
623638
$this->assertEquals($expected, $actual);
624639

640+
// Check user id.
641+
set_config('mappingfield', 'id', 'auth_userkey');
642+
$this->auth = new auth_plugin_userkey();
643+
644+
$expected = [
645+
'id' => new external_value(
646+
PARAM_INT,
647+
'Database ID of the user'
648+
),
649+
];
650+
651+
$actual = $this->auth->get_request_login_url_user_parameters();
652+
$this->assertEquals($expected, $actual);
653+
625654
// Check some junk field name.
626655
set_config('mappingfield', 'junkfield', 'auth_userkey');
627656
$this->auth = new auth_plugin_userkey();

tests/externallib_test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ public function test_successful_webservice_calls(): void {
138138
$this->assertTrue(key_exists('loginurl', $result));
139139
$this->assertEquals($expectedurl, $result['loginurl']);
140140

141+
// Database Id.
142+
set_config('mappingfield', 'id', 'auth_userkey');
143+
$params = [
144+
'id' => $this->user->id,
145+
];
146+
147+
// Simulate the web service server.
148+
$result = auth_userkey_external::request_login_url($params);
149+
$result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result);
150+
151+
$actualkey = $DB->get_record('user_private_key', ['userid' => $this->user->id]);
152+
$expectedurl = $CFG->wwwroot . '/auth/userkey/login.php?key=' . $actualkey->value;
153+
154+
$this->assertTrue(is_array($result));
155+
$this->assertTrue(key_exists('loginurl', $result));
156+
$this->assertEquals($expectedurl, $result['loginurl']);
157+
141158
// IP restriction.
142159
set_config('iprestriction', true, 'auth_userkey');
143160
set_config('mappingfield', 'idnumber', 'auth_userkey');

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

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

27-
$plugin->version = 2022081902; // The current plugin version (Date: YYYYMMDDXX).
28-
$plugin->release = 2022081902; // Match release exactly to version.
27+
$plugin->version = 2025120300; // The current plugin version (Date: YYYYMMDDXX).
28+
$plugin->release = 2025120300; // Match release exactly to version.
2929
$plugin->requires = 2017051500; // Requires Moodle 3.3 version.
3030
$plugin->component = 'auth_userkey'; // Full name of the plugin (used for diagnostics).
3131
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)