From 36ef372726518058e462973f59a5f5feb2c02f55 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Mon, 19 Jun 2023 14:32:23 -0400 Subject: [PATCH] Cleanup --- src/controllers/AuthenticationController.ts | 2 +- src/controllers/LdapController.ts | 6 +--- src/controllers/UserController.ts | 2 +- src/controllers/routes/UserRouteController.ts | 24 -------------- src/server.ts | 31 ------------------- 5 files changed, 3 insertions(+), 62 deletions(-) delete mode 100644 src/controllers/routes/UserRouteController.ts diff --git a/src/controllers/AuthenticationController.ts b/src/controllers/AuthenticationController.ts index 0f80b12..3563223 100644 --- a/src/controllers/AuthenticationController.ts +++ b/src/controllers/AuthenticationController.ts @@ -30,7 +30,7 @@ export class AuthenticationController { where: { username } }) - if (!user) return null + if (!user) return await UserController.importUser(username) return user } catch (error: any) { diff --git a/src/controllers/LdapController.ts b/src/controllers/LdapController.ts index 245b54e..496358f 100644 --- a/src/controllers/LdapController.ts +++ b/src/controllers/LdapController.ts @@ -6,7 +6,7 @@ const BIND_USER = process.env.AD_BIND_USER || '' const BIND_PASSWD = process.env.AD_BIND_PASSWORD || '' interface LdapClientInterface extends Client { - authenticate(username: string, password: string): Promise + authenticate(username: string, password: string): Promise getUser(username: string): Promise } @@ -96,10 +96,6 @@ export class LdapController extends Client implements LdapClientInterface { async authenticate(username: string, password: string) { await this.bind(`${DOMAIN}\\${username}`, password) - const user = await this.getUser(username) - await this.unbind() - - return user } } diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index bff8dcd..1ba2e7c 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -3,7 +3,7 @@ import { LdapController } from '../controllers/LdapController.js' import { prisma } from '../prisma.js' const ADMIN_GROUP = process.env.ADMIN_GROUP || 'PP-SERTI' -const INSPECTOR_GROUP = process.env.INSPECTOR_GROUP || 'inspector' +const INSPECTOR_GROUP = process.env.INSPECTOR_GROUP || 'Inspectors' const USER_GROUP = process.env.USER_GROUP || 'G_SERVIDORES' export class UserController { diff --git a/src/controllers/routes/UserRouteController.ts b/src/controllers/routes/UserRouteController.ts deleted file mode 100644 index 0f2aa8f..0000000 --- a/src/controllers/routes/UserRouteController.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { UserController } from '../UserController.js' -import { prisma } from '../../prisma.js' -import { Request, Response } from 'express' - -export class UserRouteController { - static async getOne(req: Request, res: Response) { - const { username } = req.params - - if (!username) return res.status(400).json({ error: 'Missing username' }) - - try { - const user = await prisma.user.findUnique({ - where: { username } - }) - - if (!user) return res.json(await UserController.importUser(username)) - else UserController.importUser(username) - - res.json(user) - } catch (error: any) { - res.status(500).json({ error: error.message }) - } - } -} diff --git a/src/server.ts b/src/server.ts index 93abfd7..a701932 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,10 +3,7 @@ import bodyParser from 'body-parser' import { injectUserMiddleware } from './middleware/injectUserMiddleware.js' import { authMiddleware } from './middleware/authMiddleware.js' -import { hasRolesMiddleware } from './middleware/hasRolesMiddleware.js' -import { UserRouteController } from './controllers/routes/UserRouteController.js' -import { AuthenticationController } from './controllers/AuthenticationController.js' import { LoginRouteController } from './controllers/routes/LoginRouteController.js' export const app = express() @@ -17,36 +14,8 @@ app.use(injectUserMiddleware) app.use(bodyParser.json()) -// Test route -app.get('/api/', async (req: Request, res: Response) => { - res.json({ message: 'Hello!' }) -}) - -// Login route app.post('/api/login', LoginRouteController.login) app.get('/api/me', authMiddleware, async (req: Request, res: Response) => res.json(res.locals.user) ) - -app.get( - '/api/protected', - authMiddleware, - async (req: Request, res: Response) => { - res.json('Hello protected world! ' + res.locals.user?.displayName) - } -) - -app.get( - '/api/admin', - hasRolesMiddleware(['ADMIN']), - async (req: Request, res: Response) => { - res.json('Hello Admin!' + res.locals.user?.username) - } -) - -app.get( - '/api/user/:username', - hasRolesMiddleware(['ADMIN']), - UserRouteController.getOne -)