From 4352e5cafb697b26b4b30093f5255cf453730c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Mon, 3 Jul 2023 23:36:58 +0200 Subject: [PATCH] User => Rename userID to userId + Add gitlabProfile getter --- ExpressAPI/src/controllers/Session.ts | 2 +- ExpressAPI/src/managers/UserManager.ts | 2 +- .../src/middlewares/SecurityMiddleware.ts | 2 +- ExpressAPI/src/models/EnonceStaff.ts | 10 +++--- ExpressAPI/src/models/User.ts | 35 +++++++++++-------- ExpressAPI/src/routes/EnonceRoutes.ts | 4 +-- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/ExpressAPI/src/controllers/Session.ts b/ExpressAPI/src/controllers/Session.ts index 2fd2747..aa6fdd3 100644 --- a/ExpressAPI/src/controllers/Session.ts +++ b/ExpressAPI/src/controllers/Session.ts @@ -32,7 +32,7 @@ class Session { if ( jwtData.profile ) { this.profile.importFromJsonObject(jwtData.profile); - this.profile = await UserManager.getById(this.profile.userID); + this.profile = await UserManager.getById(this.profile.userId); this.profile.currentSession = this; } } catch ( err ) { } diff --git a/ExpressAPI/src/managers/UserManager.ts b/ExpressAPI/src/managers/UserManager.ts index 8fe60b3..f100c2d 100644 --- a/ExpressAPI/src/managers/UserManager.ts +++ b/ExpressAPI/src/managers/UserManager.ts @@ -27,7 +27,7 @@ class UserManager { } async getById(id: number): Promise<User | undefined> { - const raw = await db<User>(User.tableName).where('userID', id).first(); + const raw = await db<User>(User.tableName).where('userId', id).first(); return raw ? this.createObjectFromRawSql(raw) : undefined; } diff --git a/ExpressAPI/src/middlewares/SecurityMiddleware.ts b/ExpressAPI/src/middlewares/SecurityMiddleware.ts index 67c03bd..cce4344 100644 --- a/ExpressAPI/src/middlewares/SecurityMiddleware.ts +++ b/ExpressAPI/src/middlewares/SecurityMiddleware.ts @@ -22,7 +22,7 @@ class SecurityMiddleware { check(checkIfConnected: boolean, ...checkTypes: Array<SecurityCheckType>): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void { return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => { if ( checkIfConnected ) { - if ( req.session.profile.userID === null ) { + if ( req.session.profile.userId === null ) { return req.session.sendResponse(res, StatusCodes.UNAUTHORIZED); } } diff --git a/ExpressAPI/src/models/EnonceStaff.ts b/ExpressAPI/src/models/EnonceStaff.ts index bbf01c7..7452394 100644 --- a/ExpressAPI/src/models/EnonceStaff.ts +++ b/ExpressAPI/src/models/EnonceStaff.ts @@ -6,12 +6,12 @@ class EnonceStaff extends Model { static tableName: string = 'EnonceStaff'; enonceName: string = null; - userID: number = null; + userId: number = null; public async toJsonObject(): Promise<Object> { const result = { 'enonceName': this.enonceName, - 'userID' : this.userID + 'userId' : this.userId }; return result; @@ -19,13 +19,13 @@ class EnonceStaff extends Model { public importFromJsonObject(jsonObject: any) { this.enonceName = jsonObject.enonceName; - this.userID = jsonObject.userID; + this.userId = jsonObject.userId; } public toDb(): any { return { enonceName: this.enonceName, - userID : this.userID + userId : this.userId }; } @@ -35,7 +35,7 @@ class EnonceStaff extends Model { } del(): Promise<void> { - return db(EnonceStaff.tableName).where('enonceName', this.enonceName).andWhere('userID', this.userID).del(); + return db(EnonceStaff.tableName).where('enonceName', this.enonceName).andWhere('userId', this.userId).del(); } } diff --git a/ExpressAPI/src/models/User.ts b/ExpressAPI/src/models/User.ts index 56fb3e3..9955b3e 100644 --- a/ExpressAPI/src/models/User.ts +++ b/ExpressAPI/src/models/User.ts @@ -1,15 +1,18 @@ -import Session from '../controllers/Session'; -import Config from '../config/Config'; -import Toolbox from '../shared/Toolbox'; -import * as bcrypt from 'bcryptjs'; -import Model from './Model'; -import db from '../helpers/DatabaseHelper'; +import Session from '../controllers/Session'; +import Config from '../config/Config'; +import Toolbox from '../shared/helpers/Toolbox'; +import * as bcrypt from 'bcryptjs'; +import Model from './Model'; +import db from '../helpers/DatabaseHelper'; +import LazyVal from '../shared/helpers/LazyVal'; +import GitlabUser from '../shared/types/Gitlab/GitlabUser'; +import GitlabManager from '../managers/GitlabManager'; class User extends Model { - static tableName: string = 'Users'; + static tableName: string = 'User'; - userID: number = null; + userId: number = null; userFirstName: string = ''; userLastName: string = ''; userMail: string = ''; @@ -23,9 +26,13 @@ class User extends Model { currentSession: Session = null; + gitlabProfile = new LazyVal<GitlabUser>(() => { + return GitlabManager.getUserById(this.userGitlabId); + }); + public async toJsonObject(): Promise<Object> { const result = { - 'id' : this.userID, + 'id' : this.userId, 'firstName' : this.userFirstName, 'lastName' : this.userLastName, 'mail' : this.userMail, @@ -47,7 +54,7 @@ class User extends Model { } public importFromJsonObject(jsonObject: any) { - this.userID = jsonObject.id; + this.userId = jsonObject.id; this.userFirstName = jsonObject.firstName; this.userLastName = jsonObject.lastName; this.userMail = jsonObject.mail; @@ -83,21 +90,21 @@ class User extends Model { async create(): Promise<User> { const id = await db(User.tableName).insert(this.toDb()); - this.userID = id[0]; + this.userId = id[0]; return this; } update(): Promise<void> { - return db(User.tableName).where('userID', this.userID).update(this.toDb()); + return db(User.tableName).where('userId', this.userId).update(this.toDb()); } updatePassword(): Promise<void> { - return db(User.tableName).where('userID', this.userID).update({ 'userPassword': this.userPassword }); + return db(User.tableName).where('userId', this.userId).update({ 'userPassword': this.userPassword }); } del(): Promise<void> { - return db(User.tableName).where('userID', this.userID).update({ 'userDeleted': true }); + return db(User.tableName).where('userId', this.userId).update({ 'userDeleted': true }); } } diff --git a/ExpressAPI/src/routes/EnonceRoutes.ts b/ExpressAPI/src/routes/EnonceRoutes.ts index 6585d07..0bda88a 100644 --- a/ExpressAPI/src/routes/EnonceRoutes.ts +++ b/ExpressAPI/src/routes/EnonceRoutes.ts @@ -100,10 +100,10 @@ class EnonceRoutes implements RoutesManager { }).create(); let dojoUsers: Array<User> = [ req.session.profile, ...(await UserManager.getFromGitlabUsers(params.members, true) as Array<User>) ]; - dojoUsers = dojoUsers.reduce((unique, user) => (unique.findIndex(uniqueUser => uniqueUser.userID === user.userID) !== -1 ? unique : [ ...unique, user ]), Array<User>()); + dojoUsers = dojoUsers.reduce((unique, user) => (unique.findIndex(uniqueUser => uniqueUser.userId === user.userId) !== -1 ? unique : [ ...unique, user ]), Array<User>()); await Promise.all(dojoUsers.map(dojoUser => EnonceStaff.createFromSql({ enonceName: enonce.enonceName, - userID : dojoUser.userID + userId : dojoUser.userId }).create())); return req.session.sendResponse(res, StatusCodes.OK, enonce.toJsonObject()); -- GitLab