]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-paneluser.php
Move user functionality to the base
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-paneluser.php
1 <?php
2
3 define('PERMISSION_MANAGE_USERS', 'manage_users'); /** Relating to Panel Access: Can add, delete and edit users. Big boss. */
4 define('PERMISSION_BAN_USERS', 'ban_users'); /** Relating to Users tab: Can ban users connected to IRC */
5 define('PERMISSION_EDIT_USER', 'edit_user'); /** Change properties of a user, i.e. vhost, modes and more */
6 define('PERMISSION_EDIT_CHANNEL', 'edit_channel'); /** Change properties of a channel, i.e. topic, modes and more */
7 define('PERMISSION_EDIT_CHANNEL_USER', 'edit_channel_user'); /** Change properties of a user on a channel i.e give/remove voice or ops and more */
8 define('PERMISSION_SERVER_BAN_ADD', 'tkl_add'); /** Can add manual bans, including G-Lines, Z-Lines and more */
9 define('PERMISSION_SERVER_BAN_DEL', 'tkl_del'); /** Can remove set bans, including G-Lines, Z-Lines and more */
10 define('PERMISSION_NAME_BAN_ADD', 'nb_add'); /** Can add Name Bans (Q-Lines) */
11 define('PERMISSION_NAME_BAN_DEL', 'nb_del'); /** Can delete Name Bans (Q-Lines) */
12 define('PERMISSION_BAN_EXCEPTION_ADD', 'be_add'); /** Can add ban exceptions (E-Lines) */
13 define('PERMISSION_BAN_EXCEPTION_DEL', 'be_del'); /** Can delete ban exceptions (E-Lines) */
14 define('PERMISSION_SPAMFILTER_ADD', 'sf_add'); /** Can add spamfilter entries */
15 define('PERMISSION_SPAMFILTER_DEL', 'sf_del'); /** Can delete spamfilter entries */
16 /**
17 * PanelUser
18 * This is the User class for the SQL_Auth plugin
19 */
20 class PanelUser
21 {
22 public $id = NULL;
23 public $username = NULL;
24 private $passhash = NULL;
25 public $first_name = NULL;
26 public $last_name = NULL;
27 public $created = NULL;
28 public $user_meta = [];
29 public $bio = NULL;
30
31 /**
32 * Find a user in the database by name or ID
33 * @param string $name
34 * @param mixed $id
35 */
36 function __construct(string $name = NULL, int $id = NULL)
37 {
38 $user["name"] = $name;
39 $user["id"] = $id;
40 $user["object"] = NULL;
41
42 Hook::run(HOOKTYPE_USER_LOOKUP, $user);
43 foreach ($user['object'] as $key => $value)
44 $this->$key = $value;
45 }
46
47 /**
48 * Verify a user's password
49 * @param string $input
50 * @return bool
51 */
52 function password_verify(string $input) : bool
53 {
54 if (password_verify($input, $this->passhash))
55 return true;
56 return false;
57 }
58
59 /**
60 * Add user meta data
61 * @param string $key
62 * @param string $value
63 */
64 function add_meta(string $key, string $value)
65 {
66
67 if (!$key || !$value)
68 return false;
69
70 $meta = [
71 "id" => $this->id,
72 "key" => $key,
73 "value" => $value
74 ];
75
76 $array['meta'] = $meta;
77 $array['user'] = $this;
78 Hook::run(HOOKTYPE_USERMETA_ADD, $array);
79
80 }
81
82 /**
83 * Delete user meta data by key
84 * @param string $key
85 */
86 function delete_meta(string $key)
87 {
88 if (!$key )
89 return false;
90
91 $meta = [
92 "id" => $this->id,
93 "key" => $key,
94 ];
95 Hook::run(HOOKTYPE_USERMETA_DEL, $meta);
96
97 }
98
99 /** PERMISSIONS */
100
101 function add_permission($permission)
102 {
103 $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : [];
104 if (!in_array($permission,$meta))
105 $meta[] = $permission;
106 $this->add_meta("permissions", serialize($meta)); // updet de dettabess
107 $this->user_meta['permissions'] = serialize($meta); // put it back in our object in case it still needs to be used
108 }
109 function delete_permission($permission)
110 {
111 $meta = (isset($this->user_meta['permissions'])) ? unserialize($this->user_meta['permissions']) : [];
112 foreach($meta as $key => $value)
113 {
114 if (!strcmp($permission, $value))
115 unset($meta[$key]);
116 }
117 $this->add_meta("permissions", serialize($meta));
118 $this->user_meta['permissions'] = serialize($meta);
119 }
120
121 }
122
123
124 /**
125 * This class looks up and returns any user meta.
126 * This is used by PanelUser, so you won't need to
127 * call it separately from PanelUser.
128 */
129 class PanelUser_Meta
130 {
131 public $list = [];
132 function __construct($id)
133 {
134 $array = [];
135 $arr["id"] = $id;
136 $arr['meta'] = &$array;
137 Hook::run(HOOKTYPE_USERMETA_GET, $arr);
138 do_log($array);
139 $this->list = $arr['meta'];
140
141 }
142 }
143
144 /**
145 * Array of user
146 *
147 * Required:
148 * user_name
149 * user_pass
150 *
151 * Optional:
152 * user_fname
153 * user_lname
154 *
155 * @param array $user
156 * @throws Exception
157 * @return bool
158 */
159 function create_new_user(array $user) : bool
160 {
161 if (!isset($user['user_name']) || !isset($user['user_pass']))
162 throw new Exception("Attempted to add user without specifying user_name or user_pass");
163
164 $username = $user['user_name'];
165 $password = password_hash($user['user_pass'], PASSWORD_ARGON2ID);
166 $first_name = (isset($user['fname'])) ? $user['fname'] : NULL;
167 $last_name = (isset($user['lname'])) ? $user['lname'] : NULL;
168 $user_bio = (isset($user['user_bio'])) ? $user['user_bio'] : NULL;
169
170
171 $conn = sqlnew();
172 $prep = $conn->prepare("INSERT INTO " . SQL_PREFIX . "users (user_name, user_pass, user_fname, user_lname, user_bio, created) VALUES (:name, :pass, :fname, :lname, :user_bio, :created)");
173 $prep->execute(["name" => $username, "pass" => $password, "fname" => $first_name, "lname" => $last_name, "user_bio" => $user_bio, "created" => date("Y-m-d H:i:s")]);
174
175 return true;
176 }
177
178 /**
179 * Gets the user object for the current session
180 * @return PanelUser|bool
181 */
182 function unreal_get_current_user() : PanelUser|bool
183 {
184 if (!isset($_SESSION))
185 {
186 session_set_cookie_params(3600);
187 session_start();
188 }
189 if (isset($_SESSION['id']))
190 {
191 $user = new PanelUser(NULL, $_SESSION['id']);
192 if ($user->id)
193 return $user;
194 }
195 return false;
196 }
197
198 /**
199 * Checks if a user can do something
200 * @param string $permission
201 * @return bool
202 */
203 function current_user_can($permission) : bool
204 {
205 $user = unreal_get_current_user();
206 do_log($user);
207 if (!$user)
208 return false;
209 do_log($user);
210 if (isset($user->user_meta['permissions']))
211 {
212 $perms = unserialize($user->user_meta['permissions']);
213 if (in_array($permission, $perms))
214 {
215 return true;
216 }
217 }
218 return false;
219 }
220
221 /**
222 * Delete a user and related meta
223 * @param int $id The ID of the user in the SQL database.
224 * @param array $info This will fill with a response.
225 * @return int
226 *
227 * Return values:
228 * 1 The user was successfully deleted.
229 * 0 The user was not found
230 * -1 The admin does not have permission to delete users [TODO]
231 */
232 function delete_user(int $id, &$info = []) : int
233 {
234 $user = new PanelUser(NULL, $id);
235 if (!$user->id) {
236 $info[] = "Could not find user";
237 return 0;
238 }
239 $query = "DELETE FROM " . SQL_PREFIX . "users WHERE user_id = :id";
240 $conn = sqlnew();
241 $stmt = $conn->prepare($query);
242 $stmt->execute(["id" => $user->id]);
243 $deleted = $stmt->rowCount();
244 if ($user->id)
245 {
246 $info[] = "Successfully deleted user \"$user->username\"";
247 return 1;
248 }
249 $info[] = "Unknown error";
250 return 0;
251 }
252