Commit eed3242a authored by nanahira's avatar nanahira

collaborators

parent f7d37863
...@@ -60,6 +60,7 @@ export class ParticipantService extends CrudService(Participant, { ...@@ -60,6 +60,7 @@ export class ParticipantService extends CrudService(Participant, {
id: true, id: true,
creator: true, creator: true,
status: true, status: true,
collaborators: true,
}, },
}, },
relations: ['tournament'], relations: ['tournament'],
......
import { Entity, Index, OneToMany, SelectQueryBuilder } from 'typeorm'; import { Column, Entity, Index, OneToMany, SelectQueryBuilder } from 'typeorm';
import { import {
applyQueryProperty, applyQueryProperty,
BlankReturnMessageDto, BlankReturnMessageDto,
...@@ -12,6 +12,8 @@ import { ...@@ -12,6 +12,8 @@ import {
import { MycardUser } from 'nestjs-mycard'; import { MycardUser } from 'nestjs-mycard';
import { DescBase } from '../../utility/NamedBase.entity'; import { DescBase } from '../../utility/NamedBase.entity';
import { Participant } from '../../participant/entities/participant.entity'; import { Participant } from '../../participant/entities/participant.entity';
import { IsArray, IsInt, IsPositive } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export enum TournamentRule { export enum TournamentRule {
SingleElimination = 'SingleElimination', SingleElimination = 'SingleElimination',
...@@ -61,6 +63,14 @@ export class Tournament extends DescBase { ...@@ -61,6 +63,14 @@ export class Tournament extends DescBase {
}) })
creator: number; 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() @NotWritable()
@Index() @Index()
@DateColumn({ description: '创建时间', columnExtras: { nullable: false } }) @DateColumn({ description: '创建时间', columnExtras: { nullable: false } })
...@@ -98,7 +108,7 @@ export class Tournament extends DescBase { ...@@ -98,7 +108,7 @@ export class Tournament extends DescBase {
}); });
} else { } else {
qb.andWhere( qb.andWhere(
`(${entityName}.visibility != :private OR ${entityName}.creator = :self)`, `(${entityName}.visibility != :private OR ${entityName}.creator = :self OR :self = ANY(${entityName}.collaborators))`,
{ {
private: TournamentVisibility.Private, private: TournamentVisibility.Private,
self: user.id, self: user.id,
...@@ -108,7 +118,11 @@ export class Tournament extends DescBase { ...@@ -108,7 +118,11 @@ export class Tournament extends DescBase {
} }
checkPermission(user: MycardUser) { 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(); throw new BlankReturnMessageDto(403, '您无权操作该比赛。').toException();
} }
return this; return this;
......
...@@ -46,7 +46,7 @@ export class TournamentService extends CrudService(Tournament, { ...@@ -46,7 +46,7 @@ export class TournamentService extends CrudService(Tournament, {
async checkPermissionOfTournament(id: number, user: MycardUser) { async checkPermissionOfTournament(id: number, user: MycardUser) {
const tournament = await this.repo.findOne({ const tournament = await this.repo.findOne({
where: { id }, where: { id },
select: ['id', 'creator'], select: ['id', 'creator', 'collaborators'],
}); });
if (!tournament) { if (!tournament) {
throw new BlankReturnMessageDto(404, '未找到该比赛。').toException(); 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