Typeorm loads all @Entities and decorator-defined relations into a global variable, and thus makes it impossible to override already defined variables. Will have to think about how to disable this behaviour in case the user of this library does not want to use predefined variables, but for now we will use the predefined defaults.
25 lines
622 B
TypeScript
25 lines
622 B
TypeScript
import createError from 'http-errors'
|
|
import {DB} from '../database/DB'
|
|
import {UserTeam} from '../entities/UserTeam'
|
|
import {IUserPermissions} from './IUserPermissions'
|
|
|
|
export class UserPermissions implements IUserPermissions {
|
|
constructor(protected readonly db: DB) {}
|
|
|
|
async belongsToTeam(params: {userId: number, teamId: number}) {
|
|
const {userId, teamId} = params
|
|
const result = await this.db.getRepository(UserTeam)
|
|
.findOne({
|
|
where: {userId, teamId},
|
|
})
|
|
|
|
this.assert(result)
|
|
}
|
|
|
|
protected assert(value: any) {
|
|
if (!value) {
|
|
throw createError(403, 'Forbidden')
|
|
}
|
|
}
|
|
}
|