]>
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 | */ | |
68 | function password_verify(string $input) : bool | |
69 | { | |
70 | if (password_verify($input, $this->passhash)) | |
71 | return true; | |
72 | return false; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Add user meta data | |
77 | * @param string $key | |
78 | * @param string $value | |
d72d1923 VP |
79 | */ |
80 | function add_meta(string $key, string $value) | |
81 | { | |
6930484c | 82 | |
d72d1923 VP |
83 | if (!$key || !$value) |
84 | return false; | |
85 | ||
86 | $meta = [ | |
87 | "id" => $this->id, | |
88 | "key" => $key, | |
89 | "value" => $value | |
90 | ]; | |
d72d1923 | 91 | |
6930484c VP |
92 | $array['meta'] = $meta; |
93 | $array['user'] = $this; | |
94 | Hook::run(HOOKTYPE_USERMETA_ADD, $array); | |
95 | ||
d72d1923 VP |
96 | } |
97 | ||
98 | /** | |
99 | * Delete user meta data by key | |
100 | * @param string $key | |
d72d1923 VP |
101 | */ |
102 | function delete_meta(string $key) | |
103 | { | |
104 | if (!$key ) | |
105 | return false; | |
106 | ||
107 | $meta = [ | |
108 | "id" => $this->id, | |
109 | "key" => $key, | |
110 | ]; | |
6930484c | 111 | Hook::run(HOOKTYPE_USERMETA_DEL, $meta); |
d72d1923 VP |
112 | |
113 | } | |
114 | ||
115 | /** PERMISSIONS */ | |
116 | ||
117 | function add_permission($permission) | |
118 | { | |
119 | $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : []; | |
120 | if (!in_array($permission,$meta)) | |
121 | $meta[] = $permission; | |
122 | $this->add_meta("permissions", serialize($meta)); // updet de dettabess | |
123 | $this->user_meta['permissions'] = serialize($meta); // put it back in our object in case it still needs to be used | |
124 | } | |
125 | function delete_permission($permission) | |
126 | { | |
127 | $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : []; | |
128 | foreach($meta as $key => $value) | |
129 | { | |
130 | if (!strcmp($permission, $value)) | |
131 | unset($meta[$key]); | |
132 | } | |
133 | $this->add_meta("permissions", serialize($meta)); | |
134 | $this->user_meta['permissions'] = serialize($meta); | |
135 | } | |
136 | ||
5a7f0cde VP |
137 | /** Updates core user info. |
138 | * CAUTION: Updating a non-existent column will crash | |
139 | * your shit | |
140 | */ | |
141 | function update_core_info($array) | |
142 | { | |
143 | $arr = ['info' => $array, 'user' => $this]; | |
144 | Hook::run(HOOKTYPE_EDIT_USER, $arr); | |
145 | } | |
4d634d0a | 146 | } |
a3151e7c | 147 | |
6b3d3f83 VP |
148 | |
149 | /** | |
150 | * This class looks up and returns any user meta. | |
6930484c VP |
151 | * This is used by PanelUser, so you won't need to |
152 | * call it separately from PanelUser. | |
6b3d3f83 | 153 | */ |
6930484c | 154 | class PanelUser_Meta |
4d634d0a | 155 | { |
d72d1923 VP |
156 | public $list = []; |
157 | function __construct($id) | |
158 | { | |
6930484c VP |
159 | $array = []; |
160 | $arr["id"] = $id; | |
161 | $arr['meta'] = &$array; | |
162 | Hook::run(HOOKTYPE_USERMETA_GET, $arr); | |
6930484c VP |
163 | $this->list = $arr['meta']; |
164 | ||
d72d1923 | 165 | } |
a3151e7c VP |
166 | } |
167 | ||
4225314c VP |
168 | /** |
169 | * Array of user | |
170 | * | |
171 | * Required: | |
172 | * user_name | |
173 | * user_pass | |
174 | * | |
175 | * Optional: | |
176 | * user_fname | |
177 | * user_lname | |
178 | * | |
179 | * @param array $user | |
180 | * @throws Exception | |
181 | * @return bool | |
182 | */ | |
180b8ec1 | 183 | function create_new_user(array &$user) : bool |
4d634d0a | 184 | { |
d72d1923 VP |
185 | if (!isset($user['user_name']) || !isset($user['user_pass'])) |
186 | throw new Exception("Attempted to add user without specifying user_name or user_pass"); | |
187 | ||
180b8ec1 VP |
188 | $user['user_name'] = htmlspecialchars($user['user_name']); |
189 | $user['user_pass'] = password_hash($user['user_pass'], PASSWORD_ARGON2ID); | |
190 | $user['fname'] = (isset($user['fname'])) ? htmlspecialchars($user['fname']) : NULL; | |
191 | $last['lname'] = (isset($user['lname'])) ? htmlspecialchars($user['lname']) : NULL; | |
192 | $user['user_bio'] = (isset($user['user_bio'])) ? htmlspecialchars($user['user_bio']) : NULL; | |
9a674833 | 193 | $user['email'] = (isset($user['user_email'])) ? htmlspecialchars($user['user_email']) : NULL; |
d72d1923 | 194 | |
180b8ec1 VP |
195 | if (($u = new PanelUser($user['user_name']))->id) |
196 | { | |
197 | $user['err'] = "User already exists"; | |
198 | return false; | |
199 | } | |
200 | // internal use | |
201 | $user['success'] = false; | |
202 | $user['errmsg'] = []; | |
203 | ||
204 | Hook::run(HOOKTYPE_USER_CREATE, $user); | |
205 | if (!$user['success']) | |
206 | return false; | |
d72d1923 VP |
207 | |
208 | return true; | |
4d634d0a VP |
209 | } |
210 | ||
211 | /** | |
212 | * Gets the user object for the current session | |
6930484c | 213 | * @return PanelUser|bool |
4d634d0a | 214 | */ |
6930484c | 215 | function unreal_get_current_user() : PanelUser|bool |
a3151e7c | 216 | { |
d3e3ec08 | 217 | if (isset($_SESSION) && isset($_SESSION['id'])) |
d72d1923 | 218 | { |
6930484c | 219 | $user = new PanelUser(NULL, $_SESSION['id']); |
d72d1923 VP |
220 | if ($user->id) |
221 | return $user; | |
222 | } | |
223 | return false; | |
4d634d0a VP |
224 | } |
225 | ||
226 | /** | |
227 | * Checks if a user can do something | |
228 | * @param string $permission | |
229 | * @return bool | |
230 | */ | |
d72d1923 | 231 | function current_user_can($permission) : bool |
4d634d0a | 232 | { |
d72d1923 | 233 | $user = unreal_get_current_user(); |
6c67bf53 VP |
234 | if (!$user) |
235 | return false; | |
2405dc8e VP |
236 | return user_can($user, $permission); |
237 | } | |
238 | ||
239 | /** | |
240 | * Checks if a user can do something | |
241 | * @param string $permission | |
242 | * @return bool | |
243 | */ | |
244 | function user_can(PanelUser $user, $permission) : bool | |
245 | { | |
d72d1923 VP |
246 | if (!$user) |
247 | return false; | |
2405dc8e | 248 | |
d72d1923 VP |
249 | if (isset($user->user_meta['permissions'])) |
250 | { | |
251 | $perms = unserialize($user->user_meta['permissions']); | |
252 | if (in_array($permission, $perms)) | |
d72d1923 | 253 | return true; |
d72d1923 VP |
254 | } |
255 | return false; | |
4d634d0a VP |
256 | } |
257 | ||
7aad7c29 VP |
258 | /** |
259 | * Delete a user and related meta | |
260 | * @param int $id The ID of the user in the SQL database. | |
d72d1923 | 261 | * @param array $info This will fill with a response. |
7aad7c29 VP |
262 | * @return int |
263 | * | |
264 | * Return values: | |
265 | * 1 The user was successfully deleted. | |
266 | * 0 The user was not found | |
267 | * -1 The admin does not have permission to delete users [TODO] | |
268 | */ | |
269 | function delete_user(int $id, &$info = []) : int | |
270 | { | |
6930484c | 271 | $user = new PanelUser(NULL, $id); |
d72d1923 VP |
272 | if (!$user->id) { |
273 | $info[] = "Could not find user"; | |
274 | return 0; | |
275 | } | |
180b8ec1 VP |
276 | $arr = ["user" => $user, "info" => &$info, "boolint" => 0]; |
277 | Hook::run(HOOKTYPE_USER_DELETE, $arr); | |
278 | return $arr["boolint"]; | |
54b5ea90 VP |
279 | } |
280 | ||
2405dc8e VP |
281 | function get_panel_user_permission_list() |
282 | { | |
283 | $list = [ | |
284 | "Can add/delete/edit Admin Panel users" => PERMISSION_MANAGE_USERS, | |
285 | "Can ban/kill IRC users" => PERMISSION_BAN_USERS, | |
9616b8e6 | 286 | "Can change properties of a user, i.e. vhost, modes and more" => PERMISSION_EDIT_USER, |
2405dc8e | 287 | "Can change properties of a channel, i.e. topic, modes and more" => PERMISSION_EDIT_CHANNEL, |
462bd723 | 288 | "Can change properties of a user on a channel i.e give/remove voice or ops and more" => PERMISSION_EDIT_CHANNEL_USER, |
2405dc8e VP |
289 | "Can add manual bans, including G-Lines, Z-Lines and more" => PERMISSION_SERVER_BAN_ADD, |
290 | "Can remove set bans, including G-Lines, Z-Lines and more" => PERMISSION_SERVER_BAN_DEL, | |
291 | "Can forbid usernames and channels" => PERMISSION_NAME_BAN_ADD, | |
292 | "Can unforbid usernames and channels" => PERMISSION_NAME_BAN_DEL, | |
293 | "Can add server ban exceptions" => PERMISSION_BAN_EXCEPTION_ADD, | |
294 | "Can remove server ban exceptions" => PERMISSION_BAN_EXCEPTION_DEL, | |
295 | "Can add Spamfilter entries" => PERMISSION_SPAMFILTER_ADD, | |
296 | "Can remove Spamfilter entries" => PERMISSION_SPAMFILTER_DEL | |
297 | ]; | |
298 | Hook::run(HOOKTYPE_USER_PERMISSION_LIST, $list); // so plugin writers can add their own permissions | |
299 | return $list; | |
300 | } | |
301 | ||
302 | function generate_panel_user_permission_table($user) | |
303 | { | |
304 | ||
305 | $list = get_panel_user_permission_list(); | |
306 | foreach($list as $desc => $slug) | |
307 | { | |
308 | $attributes = ""; | |
309 | $attributes .= (current_user_can(PERMISSION_MANAGE_USERS)) ? "" : "disabled "; | |
310 | ?> | |
311 | <div class="input-group"> | |
312 | <div class="input-group-prepend"> | |
313 | <div class="input-group-text"> | |
314 | <input <?php | |
315 | $attributes .= (user_can($user, $slug)) ? "checked" : ""; | |
316 | echo $attributes; | |
317 | ?> name="permissions[]" value="<?php echo $slug; ?>" type="checkbox"> | |
318 | </div> | |
319 | </div> | |
320 | <input type="text" readonly class="form-control" value="<?php echo "$desc ($slug)"; ?>"> | |
321 | </div> | |
322 | ||
323 | <?php | |
324 | } | |
9616b8e6 | 325 | } |
4b48b46f VP |
326 | |
327 | function get_panel_user_roles_list() | |
328 | { | |
329 | /* Defaults */ | |
330 | $list = [ | |
331 | "Super Admin" => get_panel_user_permission_list(), // SuperAdmin can do everything | |
332 | "Read Only" => [], // Read Only can do nothing | |
333 | ]; | |
334 | ||
335 | Hook::run(HOOKTYPE_USER_ROLE_LIST, $list); | |
336 | return $list; | |
337 | } | |
338 | ||
339 | function generate_role_list($list) | |
340 | { | |
341 | $list2 = get_panel_user_permission_list(); | |
342 | ?> | |
343 | <h5>Roles List:</h5> | |
344 | <div id="permlist"> | |
345 | <div class="container-xxl"> | |
346 | <div class="accordion" id="roles_accord"> | |
347 | ||
348 | <?php foreach($list as $role => $slug) {?> | |
349 | <div class="card"> | |
350 | <div class="card-header" id="<?php echo to_slug($role); ?>_heading"> | |
351 | <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); ?>"> | |
352 | <?php echo $role ?> | |
353 | </div> | |
354 | </div> | |
355 | ||
356 | <div id="collapse_<?php echo to_slug($role); ?>" class="collapse" aria-labelledby="<?php echo to_slug($role); ?>_heading" data-parent="#roles_accord"> | |
357 | <div id="results_rpc" class="card-body"> | |
358 | <?php | |
359 | foreach($list2 as $desc => $slug) | |
360 | { | |
361 | $attributes = ""; | |
362 | $attributes .= ($role == "Super Admin" || $role == "Read Only") ? "disabled " : ""; | |
363 | ||
364 | ?> | |
365 | <div class="input-group"> | |
366 | <div class="input-group-prepend"> | |
367 | <div class="input-group-text"> | |
368 | <input <?php | |
369 | $attributes .= (in_array($slug, $list[$role])) ? "checked" : ""; | |
370 | echo $attributes; | |
371 | ?> name="<?php echo to_slug($role); ?>_permissions[]" value="<?php echo $slug; ?>" type="checkbox"> | |
372 | </div> | |
373 | </div> | |
374 | <input type="text" readonly class="form-control" value="<?php echo "$desc ($slug)"; ?>"> | |
375 | </div> | |
376 | ||
377 | <?php | |
378 | } | |
379 | ?> | |
380 | </div> | |
381 | </div> | |
382 | </div> | |
383 | <?php }?> | |
384 | ||
385 | </div></div><br> | |
386 | <button type="submit" class="btn btn-primary">Save changes</div> | |
387 | </div><?php | |
388 | ||
389 | } |