Commit eed3242a authored by nanahira's avatar nanahira

collaborators

parent f7d37863
......@@ -60,6 +60,7 @@ export class ParticipantService extends CrudService(Participant, {
id: true,
creator: true,
status: true,
collaborators: true,
},
},
relations: ['tournament'],
......
import { Entity, Index, OneToMany, SelectQueryBuilder } from 'typeorm';
import { Column, Entity, Index, OneToMany, SelectQueryBuilder } from 'typeorm';
import {
applyQueryProperty,
BlankReturnMessageDto,
......@@ -12,6 +12,8 @@ import {
import { MycardUser } from 'nestjs-mycard';
import { DescBase } from '../../utility/NamedBase.entity';
import { Participant } from '../../participant/entities/participant.entity';
import { IsArray, IsInt, IsPositive } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export enum TournamentRule {
SingleElimination = 'SingleElimination',
......@@ -61,6 +63,14 @@ export class Tournament extends DescBase {
})
creator: number;
@Index()
@IsArray()
@IsPositive({ each: true })
@IsInt({ each: true })
@ApiProperty({ type: [Number], description: '协作者 MC ID' })
@Column('int', { array: true, default: [], comment: '协作者 MC ID' })
collaborators: number[];
@NotWritable()
@Index()
@DateColumn({ description: '创建时间', columnExtras: { nullable: false } })
......@@ -98,7 +108,7 @@ export class Tournament extends DescBase {
});
} else {
qb.andWhere(
`(${entityName}.visibility != :private OR ${entityName}.creator = :self)`,
`(${entityName}.visibility != :private OR ${entityName}.creator = :self OR :self = ANY(${entityName}.collaborators))`,
{
private: TournamentVisibility.Private,
self: user.id,
......@@ -108,7 +118,11 @@ export class Tournament extends DescBase {
}
checkPermission(user: MycardUser) {
if (this.creator !== user.id && !user.admin) {
if (
this.creator !== user.id &&
!user.admin &&
!this.collaborators.includes(user.id)
) {
throw new BlankReturnMessageDto(403, '您无权操作该比赛。').toException();
}
return this;
......
......@@ -46,7 +46,7 @@ export class TournamentService extends CrudService(Tournament, {
async checkPermissionOfTournament(id: number, user: MycardUser) {
const tournament = await this.repo.findOne({
where: { id },
select: ['id', 'creator'],
select: ['id', 'creator', 'collaborators'],
});
if (!tournament) {
throw new BlankReturnMessageDto(404, '未找到该比赛。').toException();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment