-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth.php
More file actions
189 lines (167 loc) · 5.79 KB
/
Copy pathauth.php
File metadata and controls
189 lines (167 loc) · 5.79 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Auto enrol mentors, parents or managers based on a custom profile field.
*
* @package auth
* @subpackage enrolmentor
* @copyright 2013 Virgil Ashruf (v.ashruf@avetica.nl)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/auth/enrolmentor/class/helper.php');
class auth_plugin_enrolmentor extends auth_plugin_base {
/**
* Constructor.
*/
function auth_plugin_enrolmentor() {
$this->authtype = 'enrolmentor';
$this->config = get_config('auth_enrolmentor');
}
/**
* Returns true if the username and password work and false if they are
* wrong or don't exist. (Non-mnet accounts only!)
*
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
function user_login($username, $password) {
global $CFG, $DB, $USER;
if (!$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
return false;
}
if (!validate_internal_user_password($user, $password)) {
return false;
}
if ($password === 'changeme') {
// force the change - this is deprecated and it makes sense only for manual auth,
// because most other plugins can not change password easily or
// passwords are always specified by users
set_user_preference('auth_forcepasswordchange', true, $user->id);
}
return true;
}
/**
* Updates the user's password.
*
* Called when the user password is updated.
*
* @param object $user User table object
* @param string $newpassword Plaintext password
* @return boolean result
*/
function user_update_password($user, $newpassword) {
$user = get_complete_user_data('id', $user->id);
return update_internal_user_password($user, $newpassword);
}
function prevent_local_passwords() {
return false;
}
/**
* Returns true if this authentication plugin is 'internal'.
*
* @return bool
*/
function is_internal() {
return true;
}
/**
* Returns true if this authentication plugin can change the user's
* password.
*
* @return bool
*/
function can_change_password() {
return true;
}
/**
* Returns the URL for changing the user's pw, or empty if the default can
* be used.
*
* @return moodle_url
*/
function change_password_url() {
return null;
}
/**
* Returns true if plugin allows resetting of internal password.
*
* @return bool
*/
function can_reset_password() {
return true;
}
/**
* Confirm the new user as registered. This should normally not be used,
* but it may be necessary if the user auth_method is changed to manual
* before the user is confirmed.
*
* @param string $username
* @param string $confirmsecret
*/
function user_confirm($username, $confirmsecret = null) {
global $DB;
$user = get_complete_user_data('username', $username);
if (!empty($user)) {
if ($user->confirmed) {
return AUTH_CONFIRM_ALREADY;
} else {
$DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
$DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
return AUTH_CONFIRM_OK;
}
} else {
return AUTH_CONFIRM_ERROR;
}
}
/**
* Processes and stores configuration data for this authentication plugin.
* $this->config->somefield
*/
function process_config($config) {
return true;
}
/**
* Called when the user record is updated.
* Modifies user in external database. It takes olduser (before changes) and newuser (after changes)
* compares information saved modified information to external db.
*
* @param mixed $olduser Userobject before modifications (without system magic quotes)
* @param mixed $newuser Userobject new modified userobject (without system magic quotes)
* @return boolean true if updated or update ignored; false if error
*
*/
function user_update($olduser, $newuser) {
return true;
}
/**
* Post authentication hook.
* This method is called from authenticate_user_login() for all enabled auth plugins.
*
* @param object $user user object, later used for $USER
* @param string $username (with system magic quotes)
* @param string $password plain text password (with system magic quotes)
*/
function user_authenticated_hook(&$user, $username, $password) {
global $DB, $SESSION;
//Get the roleid we're going to assign.
$roleid = $this->config->role;
if($this->config->profile_field != null) {
// Get all the user ids that we're a parent of.
$lista = enrolmentor_helper::get_list_employees($user, $username, $this->config);
// Get all the user ids that we're currently a parent of.
$listb = enrolmentor_helper::get_enrolled_employees($roleid, $user->id);
// Compare the lists
$toEnrol = array_diff($lista, $listb);
$toUnenrol = array_diff($listb, $lista);
if(count($toEnrol) > 0) {
enrolmentor_helper::doEnrol($toEnrol, $roleid, $user);
}
if(count($toUnenrol) > 0) {
enrolmentor_helper::doUnenrol($toUnenrol, $roleid, $user);
}
}
}
}