Use UserPermissions in TeamRoutes
This commit is contained in:
parent
14e90f9559
commit
e18e00a6e5
@ -105,6 +105,7 @@ export class Application implements IApplication {
|
|||||||
|
|
||||||
router.use('/api', new team.TeamRoutes(
|
router.use('/api', new team.TeamRoutes(
|
||||||
this.teamService,
|
this.teamService,
|
||||||
|
this.userPermissions,
|
||||||
this.createTransactionalRouter(),
|
this.createTransactionalRouter(),
|
||||||
).handle)
|
).handle)
|
||||||
|
|
||||||
|
|||||||
@ -2,11 +2,13 @@ import {AsyncRouter} from '../router'
|
|||||||
import {BaseRoute} from '../routes/BaseRoute'
|
import {BaseRoute} from '../routes/BaseRoute'
|
||||||
import {IAPIDef} from '@rondo/common'
|
import {IAPIDef} from '@rondo/common'
|
||||||
import {ITeamService} from './ITeamService'
|
import {ITeamService} from './ITeamService'
|
||||||
|
import {IUserPermissions} from '../user/IUserPermissions'
|
||||||
import {ensureLoggedInApi} from '../middleware'
|
import {ensureLoggedInApi} from '../middleware'
|
||||||
|
|
||||||
export class TeamRoutes extends BaseRoute<IAPIDef> {
|
export class TeamRoutes extends BaseRoute<IAPIDef> {
|
||||||
constructor(
|
constructor(
|
||||||
protected readonly teamService: ITeamService,
|
protected readonly teamService: ITeamService,
|
||||||
|
protected readonly permissions: IUserPermissions,
|
||||||
protected readonly t: AsyncRouter<IAPIDef>,
|
protected readonly t: AsyncRouter<IAPIDef>,
|
||||||
) {
|
) {
|
||||||
super(t)
|
super(t)
|
||||||
@ -35,6 +37,12 @@ export class TeamRoutes extends BaseRoute<IAPIDef> {
|
|||||||
|
|
||||||
t.put('/teams/:id', async req => {
|
t.put('/teams/:id', async req => {
|
||||||
const id = Number(req.params.id)
|
const id = Number(req.params.id)
|
||||||
|
|
||||||
|
await this.permissions.belongsToTeam({
|
||||||
|
teamId: id,
|
||||||
|
userId: req.user!.id,
|
||||||
|
})
|
||||||
|
|
||||||
return this.teamService.update({
|
return this.teamService.update({
|
||||||
id,
|
id,
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
@ -44,6 +52,12 @@ export class TeamRoutes extends BaseRoute<IAPIDef> {
|
|||||||
|
|
||||||
t.delete('/teams/:id', async req => {
|
t.delete('/teams/:id', async req => {
|
||||||
const id = Number(req.params.id)
|
const id = Number(req.params.id)
|
||||||
|
|
||||||
|
await this.permissions.belongsToTeam({
|
||||||
|
teamId: id,
|
||||||
|
userId: req.user!.id,
|
||||||
|
})
|
||||||
|
|
||||||
return this.teamService.remove({
|
return this.teamService.remove({
|
||||||
id,
|
id,
|
||||||
userId: req.user!.id,
|
userId: req.user!.id,
|
||||||
|
|||||||
@ -3,24 +3,9 @@ import {ITeamService} from './ITeamService'
|
|||||||
import {IUserTeamParams} from './IUserTeamParams'
|
import {IUserTeamParams} from './IUserTeamParams'
|
||||||
import {Team} from '../entities/Team'
|
import {Team} from '../entities/Team'
|
||||||
import {UserTeam} from '../entities/UserTeam'
|
import {UserTeam} from '../entities/UserTeam'
|
||||||
import createError from 'http-errors'
|
|
||||||
|
|
||||||
export class TeamService extends BaseService implements ITeamService {
|
export class TeamService extends BaseService implements ITeamService {
|
||||||
|
|
||||||
protected async canModify({id, userId}: {id: number, userId: number}) {
|
|
||||||
const count = await this.getRepository(UserTeam)
|
|
||||||
.count({
|
|
||||||
where: {
|
|
||||||
teamId: id,
|
|
||||||
userId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if (count === 0) {
|
|
||||||
throw createError(403, 'Forbidden')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO check team limit per user
|
// TODO check team limit per user
|
||||||
async create({name, userId}: {name: string, userId: number}) {
|
async create({name, userId}: {name: string, userId: number}) {
|
||||||
const team = await this.getRepository(Team).save({
|
const team = await this.getRepository(Team).save({
|
||||||
@ -39,9 +24,6 @@ export class TeamService extends BaseService implements ITeamService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove({id, userId}: {id: number, userId: number}) {
|
async remove({id, userId}: {id: number, userId: number}) {
|
||||||
// TODO check for role
|
|
||||||
this.canModify({id, userId})
|
|
||||||
|
|
||||||
await this.getRepository(UserTeam)
|
await this.getRepository(UserTeam)
|
||||||
.delete({userId})
|
.delete({userId})
|
||||||
|
|
||||||
@ -50,8 +32,6 @@ export class TeamService extends BaseService implements ITeamService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update({id, name, userId}: {id: number, name: string, userId: number}) {
|
async update({id, name, userId}: {id: number, name: string, userId: number}) {
|
||||||
this.canModify({id, userId})
|
|
||||||
|
|
||||||
await this.getRepository(Team)
|
await this.getRepository(Team)
|
||||||
.update({
|
.update({
|
||||||
id,
|
id,
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
export interface IUserPermissions {
|
export interface IUserPermissions {
|
||||||
// TODO check for role too
|
// TODO check for role too
|
||||||
belongsToTeam(params: {userId: number, teamId: number}): void
|
belongsToTeam(params: {userId: number, teamId: number}): Promise<void>
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user