forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.php
277 lines (243 loc) · 10.2 KB
/
user.php
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.
/**
* User class
*
* @package core
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* User class to access user details.
*
* @todo move api's from user/lib.php and depreciate old ones.
* @package core
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_user {
/**
* No reply user id.
*/
const NOREPLY_USER = -10;
/**
* Support user id.
*/
const SUPPORT_USER = -20;
/** @var stdClass keep record of noreply user */
public static $noreplyuser = false;
/** @var stdClass keep record of support user */
public static $supportuser = false;
/**
* Return user object from db or create noreply or support user,
* if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER
* respectively. If userid is not found, then return false.
*
* @param int $userid user id
* @param string $fields A comma separated list of user fields to be returned, support and noreply user
* will not be filtered by this.
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
* MUST_EXIST means throw an exception if no user record or multiple records found.
* @return stdClass|bool user record if found, else false.
* @throws dml_exception if user record not found and respective $strictness is set.
*/
public static function get_user($userid, $fields = '*', $strictness = IGNORE_MISSING) {
global $DB;
// If noreply user then create fake record and return.
switch ($userid) {
case self::NOREPLY_USER:
return self::get_noreply_user();
break;
case self::SUPPORT_USER:
return self::get_support_user();
break;
default:
return $DB->get_record('user', array('id' => $userid), $fields, $strictness);
}
}
/**
* Return user object from db based on their username.
*
* @param string $username The username of the user searched.
* @param string $fields A comma separated list of user fields to be returned, support and noreply user.
* @param int $mnethostid The id of the remote host.
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
* MUST_EXIST means throw an exception if no user record or multiple records found.
* @return stdClass|bool user record if found, else false.
* @throws dml_exception if user record not found and respective $strictness is set.
*/
public static function get_user_by_username($username, $fields = '*', $mnethostid = null, $strictness = IGNORE_MISSING) {
global $DB, $CFG;
// Because we use the username as the search criteria, we must also restrict our search based on mnet host.
if (empty($mnethostid)) {
// If empty, we restrict to local users.
$mnethostid = $CFG->mnet_localhost_id;
}
return $DB->get_record('user', array('username' => $username, 'mnethostid' => $mnethostid), $fields, $strictness);
}
/**
* Helper function to return dummy noreply user record.
*
* @return stdClass
*/
protected static function get_dummy_user_record() {
global $CFG;
$dummyuser = new stdClass();
$dummyuser->id = self::NOREPLY_USER;
$dummyuser->email = $CFG->noreplyaddress;
$dummyuser->firstname = get_string('noreplyname');
$dummyuser->username = 'noreply';
$dummyuser->lastname = '';
$dummyuser->confirmed = 1;
$dummyuser->suspended = 0;
$dummyuser->deleted = 0;
$dummyuser->picture = 0;
$dummyuser->auth = 'manual';
$dummyuser->firstnamephonetic = '';
$dummyuser->lastnamephonetic = '';
$dummyuser->middlename = '';
$dummyuser->alternatename = '';
$dummyuser->imagealt = '';
return $dummyuser;
}
/**
* Return noreply user record, this is currently used in messaging
* system only for sending messages from noreply email.
* It will return record of $CFG->noreplyuserid if set else return dummy
* user object with hard-coded $user->emailstop = 1 so noreply can be sent to user.
*
* @return stdClass user record.
*/
public static function get_noreply_user() {
global $CFG;
if (!empty(self::$noreplyuser)) {
return self::$noreplyuser;
}
// If noreply user is set then use it, else create one.
if (!empty($CFG->noreplyuserid)) {
self::$noreplyuser = self::get_user($CFG->noreplyuserid);
}
if (empty(self::$noreplyuser)) {
self::$noreplyuser = self::get_dummy_user_record();
self::$noreplyuser->maildisplay = '1'; // Show to all.
}
self::$noreplyuser->emailstop = 1; // Force msg stop for this user.
return self::$noreplyuser;
}
/**
* Return support user record, this is currently used in messaging
* system only for sending messages to support email.
* $CFG->supportuserid is set then returns user record
* $CFG->supportemail is set then return dummy record with $CFG->supportemail
* else return admin user record with hard-coded $user->emailstop = 0, so user
* gets support message.
*
* @return stdClass user record.
*/
public static function get_support_user() {
global $CFG;
if (!empty(self::$supportuser)) {
return self::$supportuser;
}
// If custom support user is set then use it, else if supportemail is set then use it, else use noreply.
if (!empty($CFG->supportuserid)) {
self::$supportuser = self::get_user($CFG->supportuserid, '*', MUST_EXIST);
}
// Try sending it to support email if support user is not set.
if (empty(self::$supportuser) && !empty($CFG->supportemail)) {
self::$supportuser = self::get_dummy_user_record();
self::$supportuser->id = self::SUPPORT_USER;
self::$supportuser->email = $CFG->supportemail;
if ($CFG->supportname) {
self::$supportuser->firstname = $CFG->supportname;
}
self::$supportuser->username = 'support';
self::$supportuser->maildisplay = '1'; // Show to all.
}
// Send support msg to admin user if nothing is set above.
if (empty(self::$supportuser)) {
self::$supportuser = get_admin();
}
// Unset emailstop to make sure support message is sent.
self::$supportuser->emailstop = 0;
return self::$supportuser;
}
/**
* Reset self::$noreplyuser and self::$supportuser.
* This is only used by phpunit, and there is no other use case for this function.
* Please don't use it outside phpunit.
*/
public static function reset_internal_users() {
if (PHPUNIT_TEST) {
self::$noreplyuser = false;
self::$supportuser = false;
} else {
debugging('reset_internal_users() should not be used outside phpunit.', DEBUG_DEVELOPER);
}
}
/**
* Return true is user id is greater than self::NOREPLY_USER and
* alternatively check db.
*
* @param int $userid user id.
* @param bool $checkdb if true userid will be checked in db. By default it's false, and
* userid is compared with NOREPLY_USER for performance.
* @return bool true is real user else false.
*/
public static function is_real_user($userid, $checkdb = false) {
global $DB;
if ($userid < 0) {
return false;
}
if ($checkdb) {
return $DB->record_exists('user', array('id' => $userid));
} else {
return true;
}
}
/**
* Check if the given user is an active user in the site.
*
* @param stdClass $user user object
* @param boolean $checksuspended whether to check if the user has the account suspended
* @param boolean $checknologin whether to check if the user uses the nologin auth method
* @throws moodle_exception
* @since Moodle 3.0
*/
public static function require_active_user($user, $checksuspended = false, $checknologin = false) {
if (!self::is_real_user($user->id)) {
throw new moodle_exception('invaliduser', 'error');
}
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (empty($user->confirmed)) {
throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
}
if (isguestuser($user)) {
throw new moodle_exception('guestsarenotallowed', 'error');
}
if ($checksuspended and $user->suspended) {
throw new moodle_exception('suspended', 'auth');
}
if ($checknologin and $user->auth == 'nologin') {
throw new moodle_exception('suspended', 'auth');
}
}
}