|
| 1 | +import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; |
| 2 | +import { Reflector } from '@nestjs/core'; |
| 3 | +import { get } from 'lodash'; |
| 4 | +import { NoPermissionException } from 'src/exceptions/noPermissionException'; |
| 5 | +import { SurveyNotFoundException } from 'src/exceptions/surveyNotFoundException'; |
| 6 | +import { SessionService } from 'src/modules/survey/services/session.service'; |
| 7 | +import { SurveyMetaService } from 'src/modules/survey/services/surveyMeta.service'; |
| 8 | +import { WorkspaceMemberService } from 'src/modules/workspace/services/workspaceMember.service'; |
| 9 | +import { CollaboratorService } from 'src/modules/survey/services/collaborator.service'; |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class SessionGuard implements CanActivate { |
| 13 | + constructor( |
| 14 | + private reflector: Reflector, |
| 15 | + private readonly sessionService: SessionService, |
| 16 | + private readonly surveyMetaService: SurveyMetaService, |
| 17 | + private readonly workspaceMemberService: WorkspaceMemberService, |
| 18 | + private readonly collaboratorService: CollaboratorService, |
| 19 | + ) {} |
| 20 | + |
| 21 | + async canActivate(context: ExecutionContext): Promise<boolean> { |
| 22 | + const request = context.switchToHttp().getRequest(); |
| 23 | + const user = request.user; |
| 24 | + const sessionIdKey = this.reflector.get<string>( |
| 25 | + 'sessionId', |
| 26 | + context.getHandler(), |
| 27 | + ); |
| 28 | + |
| 29 | + const sessionId = get(request, sessionIdKey); |
| 30 | + |
| 31 | + if (!sessionId) { |
| 32 | + throw new NoPermissionException('没有权限'); |
| 33 | + } |
| 34 | + |
| 35 | + const saveSession = await this.sessionService.findOne(sessionId); |
| 36 | + |
| 37 | + request.saveSession = saveSession; |
| 38 | + |
| 39 | + const surveyId = saveSession.surveyId; |
| 40 | + |
| 41 | + const surveyMeta = await this.surveyMetaService.getSurveyById({ surveyId }); |
| 42 | + |
| 43 | + if (!surveyMeta) { |
| 44 | + throw new SurveyNotFoundException('问卷不存在'); |
| 45 | + } |
| 46 | + |
| 47 | + request.surveyMeta = surveyMeta; |
| 48 | + |
| 49 | + // 兼容老的问卷没有ownerId |
| 50 | + if ( |
| 51 | + surveyMeta.ownerId === user._id.toString() || |
| 52 | + surveyMeta.owner === user.username |
| 53 | + ) { |
| 54 | + // 问卷的owner,可以访问和操作问卷 |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + if (surveyMeta.workspaceId) { |
| 59 | + const memberInfo = await this.workspaceMemberService.findOne({ |
| 60 | + workspaceId: surveyMeta.workspaceId, |
| 61 | + userId: user._id.toString(), |
| 62 | + }); |
| 63 | + if (!memberInfo) { |
| 64 | + throw new NoPermissionException('没有权限'); |
| 65 | + } |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + const permissions = this.reflector.get<string[]>( |
| 70 | + 'surveyPermission', |
| 71 | + context.getHandler(), |
| 72 | + ); |
| 73 | + |
| 74 | + if (!Array.isArray(permissions) || permissions.length === 0) { |
| 75 | + throw new NoPermissionException('没有权限'); |
| 76 | + } |
| 77 | + |
| 78 | + const info = await this.collaboratorService.getCollaborator({ |
| 79 | + surveyId, |
| 80 | + userId: user._id.toString(), |
| 81 | + }); |
| 82 | + |
| 83 | + if (!info) { |
| 84 | + throw new NoPermissionException('没有权限'); |
| 85 | + } |
| 86 | + request.collaborator = info; |
| 87 | + if ( |
| 88 | + permissions.some((permission) => info.permissions.includes(permission)) |
| 89 | + ) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + throw new NoPermissionException('没有权限'); |
| 93 | + } |
| 94 | +} |
0 commit comments