-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternallib.php
More file actions
90 lines (79 loc) · 3.32 KB
/
externallib.php
File metadata and controls
90 lines (79 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Create a linked login in order to prevent automatic linked login creation
* by Moodle
*
* @package local_linkeduser
* @copyright 2019 David Bogner
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use auth_oauth2\api;
require_once($CFG->libdir . "/externallib.php");
require_once($CFG->libdir . "/classes/oauth2/api.php");
class local_linkeduser_external extends external_api {
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function create_linked_user_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The user id of the user a linked user is created for', VALUE_DEFAULT, 0),
'clientid' => new external_value(PARAM_ALPHANUMEXT, 'The client id of the issuer (OAuth2 Identity provider)', VALUE_DEFAULT, '')
)
);
}
/**
* Create a linked login for the user identiefied by $userid.
* This is used to prevent automatic creation of linked logins for
* OAuth2 identity providers, that use same email addresses for
* multiple users.
*
* @param int $userid The user params for the linked user
* @param string $clientid The OAuth2 client id
* @return bool success
*/
public static function create_linked_user(int $userid = 0, string $clientid = '') {
global $USER, $DB;
// Parameter validation.
$params = self::validate_parameters(self::create_linked_user_parameters(),
array(
'userid' => $userid,
'clientid' => $clientid
));
// Context validation.
$context = context_user::instance($USER->id);
self::validate_context($context);
// Capability checking.
if (!has_capability('moodle/user:create', $context)) {
throw new moodle_exception('cannotupdateprofile');
}
$issuerid = $DB->get_field_select('oauth2_issuer', 'id', 'clientid = :clientid', array('clientid' => $params['clientid']));
$issuer = \core\oauth2\api::get_issuer($issuerid);
$user = $DB->get_record_select('user', 'id = :userid', array('userid' => $params['userid']), 'username, email');
$userinfo = [];
$userinfo['email'] = $user->email;
$userinfo['username'] = $user->username;
api::link_login($userinfo, $issuer, $userid, true);
return true;
}
/**
* Returns description of method result value
* @return external_description
*/
public static function create_linked_user_returns() {
return new external_value(PARAM_BOOL, 'Success');
}
}