]>
Commit | Line | Data |
---|---|---|
961b0aa7 | 1 | <?php |
cc9898cc VP |
2 | /** Relating to Panel Access: Can add, delete and edit users. Big boss. */ |
3 | define('PERMISSION_MANAGE_USERS', 'manage_users'); | |
4 | /** Relating to Users tab: Can ban users connected to IRC */ | |
5 | define('PERMISSION_BAN_USERS', 'ban_users'); | |
6 | /** Change properties of a user, i.e. vhost, modes and more */ | |
7 | define('PERMISSION_EDIT_USER', 'edit_user'); | |
8 | /** Change properties of a channel, i.e. topic, modes and more */ | |
9 | define('PERMISSION_EDIT_CHANNEL', 'edit_channel'); | |
10 | /** Change properties of a user on a channel i.e give/remove voice or ops and more */ | |
11 | define('PERMISSION_EDIT_CHANNEL_USER', 'edit_channel_user'); | |
12 | /** Can add manual bans, including G-Lines, Z-Lines and more */ | |
13 | define('PERMISSION_SERVER_BAN_ADD', 'tkl_add'); | |
14 | /** Can remove set bans, including G-Lines, Z-Lines and more */ | |
15 | define('PERMISSION_SERVER_BAN_DEL', 'tkl_del'); | |
16 | /** Can add Name Bans (Q-Lines) */ | |
17 | define('PERMISSION_NAME_BAN_ADD', 'nb_add'); | |
18 | /** Can delete Name Bans (Q-Lines) */ | |
19 | define('PERMISSION_NAME_BAN_DEL', 'nb_del'); | |
20 | /** Can add ban exceptions (E-Lines) */ | |
21 | define('PERMISSION_BAN_EXCEPTION_ADD', 'be_add'); | |
22 | /** Can delete ban exceptions (E-Lines) */ | |
23 | define('PERMISSION_BAN_EXCEPTION_DEL', 'be_del'); | |
24 | /** Can add spamfilter entries */ | |
25 | define('PERMISSION_SPAMFILTER_ADD', 'sf_add'); | |
26 | /** Can delete spamfilter entries */ | |
27 | define('PERMISSION_SPAMFILTER_DEL', 'sf_del'); | |
5a7f0cde VP |
28 | /** Can rehash servers */ |
29 | define('PERMISSION_REHASH', 'rhs'); | |
6b3d3f83 | 30 | /** |
6930484c | 31 | * PanelUser |
6b3d3f83 VP |
32 | * This is the User class for the SQL_Auth plugin |
33 | */ | |
6930484c | 34 | class PanelUser |
961b0aa7 | 35 | { |
d72d1923 VP |
36 | public $id = NULL; |
37 | public $username = NULL; | |
38 | private $passhash = NULL; | |
39 | public $first_name = NULL; | |
40 | public $last_name = NULL; | |
41 | public $created = NULL; | |
42 | public $user_meta = []; | |
43 | public $bio = NULL; | |
0b546dde | 44 | public $email = NULL; |
d72d1923 VP |
45 | |
46 | /** | |
47 | * Find a user in the database by name or ID | |
48 | * @param string $name | |
49 | * @param mixed $id | |
50 | */ | |
51 | function __construct(string $name = NULL, int $id = NULL) | |
52 | { | |
6930484c VP |
53 | $user["name"] = $name; |
54 | $user["id"] = $id; | |
55 | $user["object"] = NULL; | |
6930484c | 56 | Hook::run(HOOKTYPE_USER_LOOKUP, $user); |
cd1dee97 BM |
57 | if ($user['object'] === null) |
58 | return; /* no auth module loaded? */ | |
6930484c VP |
59 | foreach ($user['object'] as $key => $value) |
60 | $this->$key = $value; | |
d72d1923 VP |
61 | } |
62 | ||
63 | /** | |
64 | * Verify a user's password | |
65 | * @param string $input | |
66 | * @return bool | |
67 | */ | |
6b08fcb9 | 68 | function password_verify(string $password, bool &$hash_needs_updating = false) : bool |
d72d1923 | 69 | { |
6b08fcb9 BM |
70 | GLOBAL $config; |
71 | $hash_needs_updating = false; | |
72 | ||
73 | if (str_starts_with($this->passhash, "peppered:")) | |
74 | { | |
75 | /* Argon2 with pepper */ | |
76 | $password = hash_hmac("sha256", $password, $config['secrets']['pepper']); | |
77 | if (password_verify($password, substr($this->passhash,9))) | |
78 | return true; | |
79 | } else { | |
80 | /* Old standard argon2 */ | |
81 | if (password_verify($password, $this->passhash)) | |
82 | { | |
83 | $hash_needs_updating = true; | |
84 | return true; | |
85 | } | |
86 | } | |
d72d1923 VP |
87 | return false; |
88 | } | |
89 | ||
6b08fcb9 BM |
90 | /** |
91 | * Generate hash of user's password | |
92 | * @param string $password | |
93 | * @return string | |
94 | */ | |
95 | public static function password_hash(string $password) : string | |
96 | { | |
97 | GLOBAL $config; | |
98 | $input = hash_hmac("sha256", $password, $config['secrets']['pepper']); | |
99 | return "peppered:".password_hash($input, PASSWORD_ARGON2ID); | |
100 | } | |
101 | ||
d72d1923 VP |
102 | /** |
103 | * Add user meta data | |
104 | * @param string $key | |
105 | * @param string $value | |
d72d1923 VP |
106 | */ |
107 | function add_meta(string $key, string $value) | |
108 | { | |
6930484c | 109 | |
d72d1923 VP |
110 | if (!$key || !$value) |
111 | return false; | |
112 | ||
113 | $meta = [ | |
114 | "id" => $this->id, | |
115 | "key" => $key, | |
116 | "value" => $value | |
117 | ]; | |
d72d1923 | 118 | |
6930484c VP |
119 | $array['meta'] = $meta; |
120 | $array['user'] = $this; | |
121 | Hook::run(HOOKTYPE_USERMETA_ADD, $array); | |
122 | ||
d72d1923 VP |
123 | } |
124 | ||
125 | /** | |
126 | * Delete user meta data by key | |
127 | * @param string $key | |
d72d1923 VP |
128 | */ |
129 | function delete_meta(string $key) | |
130 | { | |
131 | if (!$key ) | |
132 | return false; | |
133 | ||
134 | $meta = [ | |
135 | "id" => $this->id, | |
136 | "key" => $key, | |
137 | ]; | |
c53841fe BM |
138 | $array['meta'] = $meta; |
139 | $array['user'] = $this; | |
140 | Hook::run(HOOKTYPE_USERMETA_DEL, $array); | |
d72d1923 VP |
141 | |
142 | } | |
143 | ||
144 | /** PERMISSIONS */ | |
145 | ||
146 | function add_permission($permission) | |
147 | { | |
148 | $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : []; | |
149 | if (!in_array($permission,$meta)) | |
150 | $meta[] = $permission; | |
151 | $this->add_meta("permissions", serialize($meta)); // updet de dettabess | |
152 | $this->user_meta['permissions'] = serialize($meta); // put it back in our object in case it still needs to be used | |
153 | } | |
154 | function delete_permission($permission) | |
155 | { | |
156 | $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : []; | |
157 | foreach($meta as $key => $value) | |
158 | { | |
159 | if (!strcmp($permission, $value)) | |
160 | unset($meta[$key]); | |
161 | } | |
162 | $this->add_meta("permissions", serialize($meta)); | |
163 | $this->user_meta['permissions'] = serialize($meta); | |
164 | } | |
165 | ||
5a7f0cde VP |
166 | /** Updates core user info. |
167 | * CAUTION: Updating a non-existent column will crash | |
168 | * your shit | |
169 | */ | |
170 | function update_core_info($array) | |
171 | { | |
172 | $arr = ['info' => $array, 'user' => $this]; | |
173 | Hook::run(HOOKTYPE_EDIT_USER, $arr); | |
174 | } | |
4d634d0a | 175 | } |
a3151e7c | 176 | |
6b3d3f83 VP |
177 | |
178 | /** | |
179 | * This class looks up and returns any user meta. | |
6930484c VP |
180 | * This is used by PanelUser, so you won't need to |
181 | * call it separately from PanelUser. | |
6b3d3f83 | 182 | */ |
6930484c | 183 | class PanelUser_Meta |
4d634d0a | 184 | { |
d72d1923 VP |
185 | public $list = []; |
186 | function __construct($id) | |
187 | { | |
6930484c VP |
188 | $array = []; |
189 | $arr["id"] = $id; | |
190 | $arr['meta'] = &$array; | |
191 | Hook::run(HOOKTYPE_USERMETA_GET, $arr); | |
6930484c VP |
192 | $this->list = $arr['meta']; |
193 | ||
d72d1923 | 194 | } |
a3151e7c VP |
195 | } |
196 | ||
4225314c VP |
197 | /** |
198 | * Array of user | |
199 | * | |
200 | * Required: | |
201 | * user_name | |
202 | * user_pass | |
203 | * | |
204 | * Optional: | |
205 | * user_fname | |
206 | * user_lname | |
207 | * | |
208 | * @param array $user | |
209 | * @throws Exception | |
210 | * @return bool | |
211 | */ | |
180b8ec1 | 212 | function create_new_user(array &$user) : bool |
4d634d0a | 213 | { |
d72d1923 VP |
214 | if (!isset($user['user_name']) || !isset($user['user_pass'])) |
215 | throw new Exception("Attempted to add user without specifying user_name or user_pass"); | |
216 | ||
180b8ec1 | 217 | $user['user_name'] = htmlspecialchars($user['user_name']); |
6b08fcb9 | 218 | $user['user_pass'] = PanelUser::password_hash($user['user_pass']); |
180b8ec1 VP |
219 | $user['fname'] = (isset($user['fname'])) ? htmlspecialchars($user['fname']) : NULL; |
220 | $last['lname'] = (isset($user['lname'])) ? htmlspecialchars($user['lname']) : NULL; | |
221 | $user['user_bio'] = (isset($user['user_bio'])) ? htmlspecialchars($user['user_bio']) : NULL; | |
9a674833 | 222 | $user['email'] = (isset($user['user_email'])) ? htmlspecialchars($user['user_email']) : NULL; |
d72d1923 | 223 | |
180b8ec1 VP |
224 | if (($u = new PanelUser($user['user_name']))->id) |
225 | { | |
226 | $user['err'] = "User already exists"; | |
227 | return false; | |
228 | } | |
229 | // internal use | |
230 | $user['success'] = false; | |
231 | $user['errmsg'] = []; | |
232 | ||
233 | Hook::run(HOOKTYPE_USER_CREATE, $user); | |
234 | if (!$user['success']) | |
235 | return false; | |
d72d1923 VP |
236 | |
237 | return true; | |
4d634d0a VP |
238 | } |
239 | ||
240 | /** | |
241 | * Gets the user object for the current session | |
6930484c | 242 | * @return PanelUser|bool |
4d634d0a | 243 | */ |
6930484c | 244 | function unreal_get_current_user() : PanelUser|bool |
a3151e7c | 245 | { |
d3e3ec08 | 246 | if (isset($_SESSION) && isset($_SESSION['id'])) |
d72d1923 | 247 | { |
6930484c | 248 | $user = new PanelUser(NULL, $_SESSION['id']); |
d72d1923 VP |
249 | if ($user->id) |
250 | return $user; | |
251 | } | |
252 | return false; | |
4d634d0a VP |
253 | } |
254 | ||
255 | /** | |
256 | * Checks if a user can do something | |
257 | * @param string $permission | |
258 | * @return bool | |
259 | */ | |
d72d1923 | 260 | function current_user_can($permission) : bool |
4d634d0a | 261 | { |
d72d1923 | 262 | $user = unreal_get_current_user(); |
6c67bf53 VP |
263 | if (!$user) |
264 | return false; | |
2405dc8e VP |
265 | return user_can($user, $permission); |
266 | } | |
267 | ||
268 | /** | |
269 | * Checks if a user can do something | |
270 | * @param string $permission | |
271 | * @return bool | |
272 | */ | |
273 | function user_can(PanelUser $user, $permission) : bool | |
274 | { | |
6f0e7ce4 | 275 | global $config; |
d72d1923 VP |
276 | if (!$user) |
277 | return false; | |
2405dc8e | 278 | |
6f0e7ce4 VP |
279 | if (isset($user->user_meta['role'])) |
280 | { | |
281 | if ($user->user_meta['role'] == "Super-Admin") | |
282 | return true; | |
283 | ||
284 | else if ($user->user_meta['role'] == "Read-Only") | |
285 | return false; | |
286 | ||
287 | else if (in_array($permission, $config['user_roles'][$user->user_meta['role']])) | |
288 | return true; | |
289 | ||
290 | return false; | |
291 | } | |
292 | ||
293 | /* compatibility fallback */ | |
d72d1923 VP |
294 | if (isset($user->user_meta['permissions'])) |
295 | { | |
296 | $perms = unserialize($user->user_meta['permissions']); | |
297 | if (in_array($permission, $perms)) | |
d72d1923 | 298 | return true; |
d72d1923 VP |
299 | } |
300 | return false; | |
4d634d0a VP |
301 | } |
302 | ||
7aad7c29 VP |
303 | /** |
304 | * Delete a user and related meta | |
305 | * @param int $id The ID of the user in the SQL database. | |
d72d1923 | 306 | * @param array $info This will fill with a response. |
7aad7c29 VP |
307 | * @return int |
308 | * | |
309 | * Return values: | |
310 | * 1 The user was successfully deleted. | |
311 | * 0 The user was not found | |
312 | * -1 The admin does not have permission to delete users [TODO] | |
313 | */ | |
314 | function delete_user(int $id, &$info = []) : int | |
315 | { | |
6930484c | 316 | $user = new PanelUser(NULL, $id); |
d72d1923 VP |
317 | if (!$user->id) { |
318 | $info[] = "Could not find user"; | |
319 | return 0; | |
320 | } | |
180b8ec1 VP |
321 | $arr = ["user" => $user, "info" => &$info, "boolint" => 0]; |
322 | Hook::run(HOOKTYPE_USER_DELETE, $arr); | |
323 | return $arr["boolint"]; | |
54b5ea90 VP |
324 | } |
325 | ||
2405dc8e VP |
326 | function get_panel_user_permission_list() |
327 | { | |
328 | $list = [ | |
329 | "Can add/delete/edit Admin Panel users" => PERMISSION_MANAGE_USERS, | |
330 | "Can ban/kill IRC users" => PERMISSION_BAN_USERS, | |
9616b8e6 | 331 | "Can change properties of a user, i.e. vhost, modes and more" => PERMISSION_EDIT_USER, |
2405dc8e | 332 | "Can change properties of a channel, i.e. topic, modes and more" => PERMISSION_EDIT_CHANNEL, |
462bd723 | 333 | "Can change properties of a user on a channel i.e give/remove voice or ops and more" => PERMISSION_EDIT_CHANNEL_USER, |
2405dc8e VP |
334 | "Can add manual bans, including G-Lines, Z-Lines and more" => PERMISSION_SERVER_BAN_ADD, |
335 | "Can remove set bans, including G-Lines, Z-Lines and more" => PERMISSION_SERVER_BAN_DEL, | |
336 | "Can forbid usernames and channels" => PERMISSION_NAME_BAN_ADD, | |
337 | "Can unforbid usernames and channels" => PERMISSION_NAME_BAN_DEL, | |
338 | "Can add server ban exceptions" => PERMISSION_BAN_EXCEPTION_ADD, | |
339 | "Can remove server ban exceptions" => PERMISSION_BAN_EXCEPTION_DEL, | |
340 | "Can add Spamfilter entries" => PERMISSION_SPAMFILTER_ADD, | |
341 | "Can remove Spamfilter entries" => PERMISSION_SPAMFILTER_DEL | |
342 | ]; | |
343 | Hook::run(HOOKTYPE_USER_PERMISSION_LIST, $list); // so plugin writers can add their own permissions | |
344 | return $list; | |
345 | } | |
346 | ||
347 | function generate_panel_user_permission_table($user) | |
348 | { | |
349 | ||
350 | $list = get_panel_user_permission_list(); | |
351 | foreach($list as $desc => $slug) | |
352 | { | |
353 | $attributes = ""; | |
354 | $attributes .= (current_user_can(PERMISSION_MANAGE_USERS)) ? "" : "disabled "; | |
355 | ?> | |
356 | <div class="input-group"> | |
357 | <div class="input-group-prepend"> | |
358 | <div class="input-group-text"> | |
359 | <input <?php | |
360 | $attributes .= (user_can($user, $slug)) ? "checked" : ""; | |
361 | echo $attributes; | |
362 | ?> name="permissions[]" value="<?php echo $slug; ?>" type="checkbox"> | |
363 | </div> | |
364 | </div> | |
365 | <input type="text" readonly class="form-control" value="<?php echo "$desc ($slug)"; ?>"> | |
366 | </div> | |
367 | ||
368 | <?php | |
369 | } | |
9616b8e6 | 370 | } |
4b48b46f VP |
371 | |
372 | function get_panel_user_roles_list() | |
373 | { | |
374 | /* Defaults */ | |
375 | $list = [ | |
6f0e7ce4 VP |
376 | "Super-Admin" => get_panel_user_permission_list(), // SuperAdmin can do everything |
377 | "Read-Only" => [], // Read Only can do nothing | |
4b48b46f VP |
378 | ]; |
379 | ||
380 | Hook::run(HOOKTYPE_USER_ROLE_LIST, $list); | |
381 | return $list; | |
382 | } | |
383 | ||
384 | function generate_role_list($list) | |
385 | { | |
386 | $list2 = get_panel_user_permission_list(); | |
387 | ?> | |
388 | <h5>Roles List:</h5> | |
389 | <div id="permlist"> | |
088733d4 | 390 | <div class="container-xxl" style="max-width: 1430px;"> |
4b48b46f VP |
391 | <div class="accordion" id="roles_accord"> |
392 | ||
393 | <?php foreach($list as $role => $slug) {?> | |
394 | <div class="card"> | |
395 | <div class="card-header" id="<?php echo to_slug($role); ?>_heading"> | |
396 | <div class="btn-header-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapse_<?php echo to_slug($role); ?>" aria-expanded="true" aria-controls="collapse_<?php echo to_slug($role); ?>"> | |
397 | <?php echo $role ?> | |
1634b6ac | 398 | |
4b48b46f VP |
399 | </div> |
400 | </div> | |
401 | ||
402 | <div id="collapse_<?php echo to_slug($role); ?>" class="collapse" aria-labelledby="<?php echo to_slug($role); ?>_heading" data-parent="#roles_accord"> | |
403 | <div id="results_rpc" class="card-body"> | |
1634b6ac | 404 | <form method="post"> |
6f0e7ce4 | 405 | <?php if ($role !== "Super-Admin" && $role !== "Read-Only") { ?> |
1634b6ac VP |
406 | <div class="container row mb-2"> |
407 | <button id="update_role" name="update_role" value="<?php echo $role ?>" class="btn btn-primary ml-1 mr-2" >Update</button> | |
408 | <button id="delete_role" name="del_role_name" value="<?php echo $role ?>" class="btn btn-danger"><i class="fa fa-trash fa-1" aria-hidden="true"></i></button> | |
409 | </div> | |
410 | ||
411 | <?php } ?> | |
412 | <div id="<?php echo $role; ?>_input_area"><?php | |
4b48b46f VP |
413 | foreach($list2 as $desc => $slug) |
414 | { | |
415 | $attributes = ""; | |
6f0e7ce4 | 416 | $attributes .= ($role == "Super-Admin" || $role == "Read-Only") ? "disabled " : ""; |
4b48b46f VP |
417 | |
418 | ?> | |
419 | <div class="input-group"> | |
420 | <div class="input-group-prepend"> | |
421 | <div class="input-group-text"> | |
422 | <input <?php | |
423 | $attributes .= (in_array($slug, $list[$role])) ? "checked" : ""; | |
424 | echo $attributes; | |
1634b6ac | 425 | ?> name="permissions[]" value="<?php echo $slug; ?>" type="checkbox"> |
4b48b46f VP |
426 | </div> |
427 | </div> | |
428 | <input type="text" readonly class="form-control" value="<?php echo "$desc ($slug)"; ?>"> | |
429 | </div> | |
430 | ||
431 | <?php | |
432 | } | |
1634b6ac VP |
433 | ?> </div> |
434 | </form> | |
4b48b46f VP |
435 | </div> |
436 | </div> | |
437 | </div> | |
438 | <?php }?> | |
439 | ||
440 | </div></div><br> | |
1634b6ac | 441 | |
4b48b46f VP |
442 | </div><?php |
443 | ||
444 | } |