Skip to content

Commit 33f82b4

Browse files
committed
feat: Allow spectators to read Jobs
1 parent 079d31a commit 33f82b4

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/job/job.controller.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {EmpireService} from '../empire/empire.service';
2020
import {JobType} from './job-type.enum';
2121
import {EmpireDocument} from '../empire/empire.schema';
2222
import {SystemService} from '../system/system.service';
23-
import {UserService} from '../user/user.service';
2423
import {JobLogicService} from './job-logic.service';
24+
import {MemberService} from '../member/member.service';
2525

2626
@Controller('games/:game/empires/:empire/jobs')
2727
@ApiTags('Jobs')
@@ -32,7 +32,7 @@ export class JobController {
3232
private readonly jobService: JobService,
3333
private readonly jobLogicService: JobLogicService,
3434
private readonly empireService: EmpireService,
35-
private readonly userService: UserService,
35+
private readonly memberService: MemberService,
3636
private readonly systemService: SystemService,
3737
) {
3838
}
@@ -63,7 +63,7 @@ export class JobController {
6363
@Query('system', OptionalObjectIdPipe) system?: Types.ObjectId | undefined,
6464
@Query('type') type?: string,
6565
): Promise<Job[]> {
66-
await this.checkUserAccess(game, user, empire);
66+
await this.checkUserRead(user, empire);
6767
return this.jobService.findAll({game, empire, system, type}, {sort: {priority: 1, createdAt: 1}});
6868
}
6969

@@ -79,7 +79,7 @@ export class JobController {
7979
@Param('id', ObjectIdPipe) id: Types.ObjectId,
8080
@AuthUser() user: User,
8181
): Promise<Job | null> {
82-
await this.checkUserAccess(game, user, empire);
82+
await this.checkUserRead(user, empire);
8383
return this.jobService.find(id);
8484
}
8585

@@ -96,7 +96,7 @@ export class JobController {
9696
@Body() dto: CreateJobDto,
9797
): Promise<Job | null> {
9898
const [empireDoc, system] = await Promise.all([
99-
this.checkUserAccess(game, user, empire),
99+
this.checkUserWrite(user, empire),
100100
dto.system ? this.systemService.find(dto.system) : Promise.resolve(undefined),
101101
]);
102102
const result = await this.jobService.createJob(dto, empireDoc, system ?? undefined);
@@ -117,7 +117,7 @@ export class JobController {
117117
@Body() dto: UpdateJobDto,
118118
@AuthUser() user: User,
119119
): Promise<Job | null> {
120-
await this.checkUserAccess(game, user, empire);
120+
await this.checkUserWrite(user, empire);
121121
return this.jobService.update(id, dto);
122122
}
123123

@@ -133,7 +133,7 @@ export class JobController {
133133
@Param('id', ObjectIdPipe) id: Types.ObjectId,
134134
@AuthUser() user: User,
135135
): Promise<Job | null> {
136-
const userEmpire = await this.checkUserAccess(game, user, empire);
136+
const userEmpire = await this.checkUserWrite(user, empire);
137137
const job = await this.jobService.find(id) ?? notFound('Job not found.');
138138
if (job.cost && job.progress < job.total) {
139139
this.jobLogicService.refundResources(userEmpire, job);
@@ -142,16 +142,22 @@ export class JobController {
142142
return this.jobService.delete(id);
143143
}
144144

145-
private async checkUserAccess(game: Types.ObjectId, user: User, empire: Types.ObjectId): Promise<EmpireDocument> {
146-
const userEmpire = await this.empireService.findOne({game, user: user._id});
147-
if (!userEmpire) {
148-
throw new ForbiddenException('You do not own an empire in this game.');
145+
private async checkUserWrite(user: User, empire: Types.ObjectId): Promise<EmpireDocument> {
146+
const requestedEmpire = await this.empireService.find(empire) ?? notFound(empire);
147+
if (requestedEmpire.user.equals(user._id)) {
148+
return requestedEmpire;
149149
}
150+
throw new ForbiddenException('You can only modify jobs for your own empire.');
151+
}
150152

151-
const requestedEmpire = await this.empireService.findOne({_id: empire, game});
152-
if (!requestedEmpire || !requestedEmpire._id.equals(userEmpire._id)) {
153-
throw new ForbiddenException('You can only access jobs for your own empire.');
153+
private async checkUserRead(user: User, empire: Types.ObjectId): Promise<void> {
154+
const requestedEmpire = await this.empireService.findOne(empire) ?? notFound(empire);
155+
if (requestedEmpire.user.equals(user._id)) {
156+
return;
157+
}
158+
if (await this.memberService.isSpectator(requestedEmpire.game, user._id)) {
159+
return;
154160
}
155-
return userEmpire;
161+
throw new ForbiddenException('You can only modify jobs for your own empire.');
156162
}
157163
}

src/job/job.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {JobHandler} from './job.handler';
88
import {SystemModule} from '../system/system.module';
99
import {UserModule} from '../user/user.module';
1010
import {JobLogicService} from './job-logic.service';
11+
import {MemberModule} from '../member/member.module';
1112

1213
@Module({
1314
imports: [
1415
MongooseModule.forFeature([{name: Job.name, schema: JobSchema}]),
1516
UserModule,
1617
EmpireModule,
1718
SystemModule,
19+
MemberModule,
1820
],
1921
controllers: [JobController],
2022
providers: [JobService, JobHandler, JobLogicService],

0 commit comments

Comments
 (0)