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');
28 /** Can rehash servers */
29 define('PERMISSION_REHASH', 'rhs');
32 * This is the User class for the SQL_Auth plugin
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 = [];
47 * Find a user in the database by name or ID
51 function __construct(string $name = NULL, int $id = NULL)
53 $user["name"] = $name;
55 $user["object"] = NULL;
56 Hook
::run(HOOKTYPE_USER_LOOKUP
, $user);
57 if ($user['object'] === null)
58 return; /* no auth module loaded? */
59 foreach ($user['object'] as $key => $value)
64 * Verify a user's password
65 * @param string $input
68 function password_verify(string $password, bool &$hash_needs_updating = false) : bool
71 $hash_needs_updating = false;
73 if (str_starts_with($this->passhash
, "peppered:"))
75 /* Argon2 with pepper */
76 $password = hash_hmac("sha256", $password, $config['secrets']['pepper']);
77 if (password_verify($password, substr($this->passhash
,9)))
80 /* Old standard argon2 */
81 if (password_verify($password, $this->passhash
))
83 $hash_needs_updating = true;
91 * Generate hash of user's password
92 * @param string $password
95 public static function password_hash(string $password) : string
98 $input = hash_hmac("sha256", $password, $config['secrets']['pepper']);
99 return "peppered:".password_hash($input, PASSWORD_ARGON2ID
);
105 * @param string $value
107 function add_meta(string $key, string $value)
110 if (!$key || !$value)
119 $array['meta'] = $meta;
120 $array['user'] = $this;
121 Hook
::run(HOOKTYPE_USERMETA_ADD
, $array);
126 * Delete user meta data by key
129 function delete_meta(string $key)
138 $array['meta'] = $meta;
139 $array['user'] = $this;
140 Hook
::run(HOOKTYPE_USERMETA_DEL
, $array);
146 function add_permission($permission)
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
154 function delete_permission($permission)
156 $meta = (isset($this->user_meta
['permissions'])) ? unserialize($this->user_meta
['permissions']) : [];
157 foreach($meta as $key => $value)
159 if (!strcmp($permission, $value))
162 $this->add_meta("permissions", serialize($meta));
163 $this->user_meta
['permissions'] = serialize($meta);
166 /** Updates core user info.
167 * CAUTION: Updating a non-existent column will crash
170 function update_core_info($array)
172 $arr = ['info' => $array, 'user' => $this];
173 Hook
::run(HOOKTYPE_EDIT_USER
, $arr);
179 * This class looks up and returns any user meta.
180 * This is used by PanelUser, so you won't need to
181 * call it separately from PanelUser.
186 function __construct($id)
190 $arr['meta'] = &$array;
191 Hook
::run(HOOKTYPE_USERMETA_GET
, $arr);
192 $this->list = $arr['meta'];
212 function create_new_user(array &$user) : bool
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");
217 $user['user_name'] = htmlspecialchars($user['user_name']);
218 $user['user_pass'] = PanelUser
::password_hash($user['user_pass']);
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;
222 $user['email'] = (isset($user['user_email'])) ? htmlspecialchars($user['user_email']) : NULL;
224 if (($u = new PanelUser($user['user_name']))->id
)
226 $user['err'] = "User already exists";
230 $user['success'] = false;
231 $user['errmsg'] = [];
233 Hook
::run(HOOKTYPE_USER_CREATE
, $user);
234 if (!$user['success'])
241 * Gets the user object for the current session
242 * @return PanelUser|bool
244 function unreal_get_current_user() : PanelUser
|bool
246 if (isset($_SESSION) && isset($_SESSION['id']))
248 $user = new PanelUser(NULL, $_SESSION['id']);
256 * Checks if a user can do something
257 * @param string $permission
260 function current_user_can($permission) : bool
262 $user = unreal_get_current_user();
265 return user_can($user, $permission);
269 * Checks if a user can do something
270 * @param string $permission
273 function user_can(PanelUser
$user, $permission) : bool
279 if (isset($user->user_meta
['role']))
281 if ($user->user_meta
['role'] == "Super-Admin")
284 else if ($user->user_meta
['role'] == "Read-Only")
287 else if (in_array($permission, $config['user_roles'][$user->user_meta
['role']]))
293 /* compatibility fallback */
294 if (isset($user->user_meta
['permissions']))
296 $perms = unserialize($user->user_meta
['permissions']);
297 if (in_array($permission, $perms))
304 * Delete a user and related meta
305 * @param int $id The ID of the user in the SQL database.
306 * @param array $info This will fill with a response.
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]
314 function delete_user(int $id, &$info = []) : int
316 $user = new PanelUser(NULL, $id);
318 $info[] = "Could not find user";
321 $arr = ["user" => $user, "info" => &$info, "boolint" => 0];
322 Hook
::run(HOOKTYPE_USER_DELETE
, $arr);
323 return $arr["boolint"];
326 function get_panel_user_permission_list()
329 "Can add/delete/edit Admin Panel users" => PERMISSION_MANAGE_USERS
,
330 "Can ban/kill IRC users" => PERMISSION_BAN_USERS
,
331 "Can change properties of a user, i.e. vhost, modes and more" => PERMISSION_EDIT_USER
,
332 "Can change properties of a channel, i.e. topic, modes and more" => PERMISSION_EDIT_CHANNEL
,
333 "Can change properties of a user on a channel i.e give/remove voice or ops and more" => PERMISSION_EDIT_CHANNEL_USER
,
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
343 Hook
::run(HOOKTYPE_USER_PERMISSION_LIST
, $list); // so plugin writers can add their own permissions
347 function generate_panel_user_permission_table($user)
350 $list = get_panel_user_permission_list();
351 foreach($list as $desc => $slug)
354 $attributes .= (current_user_can(PERMISSION_MANAGE_USERS
)) ? "" : "disabled ";
356 <div
class="input-group">
357 <div
class="input-group-prepend">
358 <div
class="input-group-text">
360 $attributes .= (user_can($user, $slug)) ? "checked" : "";
362 ?> name
="permissions[]" value
="<?php echo $slug; ?>" type
="checkbox">
365 <input type
="text" readonly
class="form-control" value
="<?php echo "$desc ($slug)"; ?>">
372 function get_panel_user_roles_list()
376 "Super-Admin" => get_panel_user_permission_list(), // SuperAdmin can do everything
377 "Read-Only" => [], // Read Only can do nothing
380 Hook
::run(HOOKTYPE_USER_ROLE_LIST
, $list);
384 function generate_role_list($list)
386 $list2 = get_panel_user_permission_list();
390 <div
class="container-xxl" style
="max-width: 1430px;">
391 <div
class="accordion" id
="roles_accord">
393 <?php
foreach($list as $role => $slug) {?>
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); ?>">
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">
405 <?php
if ($role !== "Super-Admin" && $role !== "Read-Only") { ?>
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
>
412 <div id
="<?php echo $role; ?>_input_area"><?php
413 foreach($list2 as $desc => $slug)
416 $attributes .= ($role == "Super-Admin" || $role == "Read-Only") ? "disabled " : "";
419 <div
class="input-group">
420 <div
class="input-group-prepend">
421 <div
class="input-group-text">
423 $attributes .= (in_array($slug, $list[$role])) ? "checked" : "";
425 ?> name
="permissions[]" value
="<?php echo $slug; ?>" type
="checkbox">
428 <input type
="text" readonly
class="form-control" value
="<?php echo "$desc ($slug)"; ?>">