Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | 20x 20x 20x 20x 20x 20x 20x 16x 16x 16x 16x 32x 16x 20x 11x 11x 11x 11x 20x 32x 32x 32x 32x 20x 28x 28x 28x 28x 14x 14x 28x 20x 13x 13x 13x 13x 20x 20x 20x 20x 20x 20x 7x 7x 7x 7x 7x 7x 3x 4x 4x 1x 3x 3x 6x 3x 20x 2x 2x 2x 2x 2x 2x 20x 10x 10x 10x 10x 20x 32x 17x 17x 34x 20x 3x 3x 3x 3x 20x 7x 4x 4x 8x 20x 1x 1x 1x 20x 5x 5x 5x 5x 20x 3x 3x 6x 20x 4x 4x 4x 4x 4x 4x 20x 2x 2x 2x 2x 2x 2x 4x 2x 4x 20x 16x 16x 16x 16x 16x 32x 16x 32x 20x 2x 2x 2x 4x 20x 10x 10x 20x 20x | /** * Services for the user table. * @packageDocumentation */ import { BaseService, getTime, newUniqueID, hashPassword, checkPassword, } from "./util"; import { Image } from "./image"; /** * Login status codes. */ export enum LoginStatus { Success, BadLogin, AccountSuspended, } /** * User architecture. */ export interface User { id: string; firstname: string; lastname: string; email: string; password: string; statusID: number; verified: boolean; approved: boolean; admin: boolean; imageID: string | null; joinTime: number; lastLoginTime: number | null; lastPostTime: number | null; } /** * User with only ID architecture. */ interface UserID { id: string; } /** * User with only email architecture. */ interface UserEmail { email: string; } /** * User with only status ID architecture. */ interface UserStatusID { statusID: number; } /** * User with only verified architecture. */ interface UserVerified { verified: boolean; } /** * User with only approved architecture. */ interface UserApproved { approved: boolean; } /** * User with only admin architecture. */ interface UserAdmin { admin: boolean; } /** * User with only image ID architecture. */ interface UserImageID { imageID: string | null; } /** * User with only ID and password architecture. */ interface UserIDPassword { id: string; password: string; } /** * Unapproved users architecture. */ export interface UnapprovedUsers { userID: string; firstname: string; lastname: string; email: string; status: string; joinTime: number; } /** * User services. */ export class UserService extends BaseService { /** * Create a user. * * @param firstname The user's first name. * @param lastname The user's last name. * @param email The user's email address. * @param password The user's password. * @param statusID The status ID of the user. * @returns The new user's ID. */ public async createUser( firstname: string, lastname: string, email: string, password: string, statusID: number ): Promise<string> { const userID = await newUniqueID(this.dbm, "User"); const hashedPassword = await hashPassword(this.dbm, password); const sql = ` INSERT INTO User ( id, firstname, lastname, email, password, statusID, joinTime ) VALUES ( ?, ?, ?, ?, ?, ?, ? ); `; const params = [ userID, firstname, lastname, email, hashedPassword, statusID, getTime(), ]; await this.dbm.execute(sql, params); return userID; } /** * Check if a user exists. * * @param userID A user's ID. * @returns Whether or not the user exists. */ public async userExists(userID: string): Promise<boolean> { const sql = `SELECT id FROM User WHERE id = ?;`; const params = [userID]; const rows: UserID[] = await this.dbm.execute(sql, params); return rows.length > 0; } /** * Get a user. * * @param userID A user's ID. * @returns The user. */ public async getUser(userID: string): Promise<User> { const sql = `SELECT * FROM User WHERE id = ?;`; const params = [userID]; const rows: User[] = await this.dbm.execute(sql, params); return rows[0]; } /** * Delete a user. * * @param userID A user's ID. */ public async deleteUser(userID: string): Promise<void> { await this.dbm.sessionService.deleteUserSessions(userID); await this.dbm.postService.deleteUserPosts(userID); await this.dbm.postVoteService.deleteUserVotes(userID); await this.deleteUserImage(userID); const sql = `DELETE FROM User WHERE id = ?;`; const params = [userID]; await this.dbm.execute(sql, params); } /** * Get a user by their email address. * * @param email An email address. * @returns The user. */ public async getUserByEmail(email: string): Promise<User> { const sql = `SELECT * FROM User WHERE email = ?;`; const params = [email]; const rows: User[] = await this.dbm.execute(sql, params); return rows[0]; } /** * Make sure an email address is not yet in use. * * @param email An email address. * @returns Whether or not the email address is unique. */ public async uniqueEmail(email: string): Promise<boolean> { const sql = `SELECT email FROM User WHERE email = ?;`; const params = [email]; const rows: UserEmail[] = await this.dbm.execute(sql, params); return rows.length === 0; } /** * Log a user in. * * @param email The user's email address. * @param password The user's password. * @returns A login status code. */ public async login(email: string, password: string): Promise<LoginStatus> { let sql = `SELECT id, password FROM User WHERE email = ? AND verified = TRUE AND approved = TRUE;`; let params: any[] = [email]; let rows: UserIDPassword[] = await this.dbm.execute(sql, params); const hash = rows[0]?.password || ""; const same = await checkPassword(password, hash); if (rows.length === 0 || !same) { return LoginStatus.BadLogin; } const suspended = await this.dbm.suspendedService.userIsSuspended( rows[0].id ); if (suspended) { return LoginStatus.AccountSuspended; } sql = `UPDATE User SET lastLoginTime = ? WHERE email = ?;`; params = [getTime(), email]; await this.dbm.execute(sql, params); return LoginStatus.Success; } /** * Get the name of a user's status. * * @param userID A user's ID. * @returns The name of the user's status. */ public async getUserStatusName(userID: string): Promise<string> { const sql = `SELECT statusID FROM User WHERE id = ?;`; const params = [userID]; const rows: UserStatusID[] = await this.dbm.execute(sql, params); const statusID = rows[0]?.statusID; const statusName = await this.dbm.userStatusService.getStatusName(statusID); return statusName; } /** * Check if a user is verified. * * @param userID A user's ID. * @returns Whether or not the user's account has been verified. */ public async isVerified(userID: string): Promise<boolean> { const sql = `SELECT verified FROM User WHERE id = ?;`; const params = [userID]; const rows: UserVerified[] = await this.dbm.execute(sql, params); return !!rows[0]?.verified; } /** * Set a user's verification status. * * @param userID A user's ID. * @param verified Verification status. */ public async setVerified( userID: string, verified: boolean = true ): Promise<void> { const sql = `UPDATE User SET verified = ? WHERE id = ?;`; const params = [verified, userID]; await this.dbm.execute(sql, params); } /** * Check if a user's account has been approved. * * @param userID A user's ID. * @return Whether or not the user's account has been approved. */ public async isApproved(userID: string): Promise<boolean> { const sql = `SELECT approved FROM User WHERE id = ?;`; const params = [userID]; const rows: UserApproved[] = await this.dbm.execute(sql, params); return !!rows[0]?.approved; } /** * Set a user's approved status. * * @param userID A user's ID. * @param approved Approved status. */ public async setApproved( userID: string, approved: boolean = true ): Promise<void> { const sql = `UPDATE User SET approved = ? WHERE id = ?;`; const params = [approved, userID]; await this.dbm.execute(sql, params); } /** * Get all unapproved users. * * @returns All unapproved users. */ public async getUnapproved(): Promise<UnapprovedUsers[]> { const sql = ` SELECT User.id AS userID, firstname, lastname, email, name AS status, joinTime FROM User JOIN UserStatus ON User.statusID = UserStatus.id WHERE verified = TRUE AND approved = FALSE ORDER BY joinTime; `; const rows: UnapprovedUsers[] = await this.dbm.execute(sql); return rows; } /** * Check if a user is an admin. * * @param userID A user's ID. * @returns Whether or not the user is an admin. */ public async isAdmin(userID: string): Promise<boolean> { const sql = `SELECT admin FROM User WHERE id = ?;`; const params = [userID]; const rows: UserAdmin[] = await this.dbm.execute(sql, params); return !!rows[0]?.admin; } /** * Set a user's admin status. * * @param userID A user's ID. * @param admin Admin status. */ public async setAdmin(userID: string, admin: boolean = true): Promise<void> { const sql = `UPDATE User SET admin = ? WHERE id = ?;`; const params = [admin, userID]; await this.dbm.execute(sql, params); } /** * Get a user's image. * * @param userID A user's ID. * @returns The user's profile image. */ public async getUserImage(userID: string): Promise<Image> { const sql = `SELECT imageID from User WHERE id = ?;`; const params = [userID]; const rows: UserImageID[] = await this.dbm.execute(sql, params); const imageID = rows[0]?.imageID; const image = await this.dbm.imageService.getImage(imageID); return image; } /** * Set a user's image. * * @param userID A user's ID. * @param imageData The new binary image data. */ public async setUserImage(userID: string, imageData: Buffer): Promise<void> { let sql = `SELECT imageID from User WHERE id = ?;`; let params = [userID]; const rows: UserImageID[] = await this.dbm.execute(sql, params); const newImageID = await this.dbm.imageService.createImage(imageData); sql = `UPDATE User SET imageID = ? WHERE id = ?`; params = [newImageID, userID]; await this.dbm.execute(sql, params); const imageID = rows[0]?.imageID; await this.dbm.imageService.deleteImage(imageID); } /** * Delete a user's image. * * @param userID A user's ID. */ public async deleteUserImage(userID: string): Promise<void> { let sql = `SELECT imageID from User WHERE id = ?;`; let params = [userID]; const rows: UserImageID[] = await this.dbm.execute(sql, params); sql = `UPDATE User SET imageID = ? WHERE id = ?`; params = [null, userID]; await this.dbm.execute(sql, params); const imageID = rows[0]?.imageID; await this.dbm.imageService.deleteImage(imageID); } /** * Set a user's password. * * @param userID A user's ID. * @param password The user's new password. */ public async setUserPassword( userID: string, password: string ): Promise<void> { const hashedPassword = await hashPassword(this.dbm, password); const sql = `UPDATE User SET password = ? WHERE id = ?;`; const params = [hashedPassword, userID]; await this.dbm.execute(sql, params); } /** * Update a user's last post timestamp. * * @param userID A user's ID. */ public async updateLastPostTime(userID: string): Promise<void> { const sql = `UPDATE User SET lastPostTime = ? WHERE id = ?;`; const params = [getTime(), userID]; await this.dbm.execute(sql, params); } } |