Cleanup
This commit is contained in:
parent
c05c0d8f75
commit
36ef372726
|
@ -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) {
|
||||
|
|
|
@ -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<LdapUser | null>
|
||||
authenticate(username: string, password: string): Promise<void>
|
||||
getUser(username: string): Promise<any>
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 })
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user