-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
91 changed files
with
1,663 additions
and
2,369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
XIAOJU_SURVEY_MONGO_DB_NAME=xiaojuSurvey | ||
XIAOJU_SURVEY_MONGO_URL=mongodb://localhost:27017 | ||
XIAOJU_SURVEY_MONGO_AUTH_SOURCE=admin | ||
XIAOJU_SURVEY_MONGO_DB_NAME= # xiaojuSurvey | ||
XIAOJU_SURVEY_MONGO_URL= # mongodb://localhost:27017 # 建议设置强密码 | ||
XIAOJU_SURVEY_MONGO_AUTH_SOURCE= # admin | ||
|
||
XIAOJU_SURVEY_REDIS_HOST= | ||
XIAOJU_SURVEY_REDIS_PORT= | ||
XIAOJU_SURVEY_REDIS_USERNAME= | ||
XIAOJU_SURVEY_REDIS_PASSWORD= | ||
XIAOJU_SURVEY_REDIS_DB= | ||
|
||
XIAOJU_SURVEY_RESPONSE_AES_ENCRYPT_SECRET_KEY=dataAesEncryptSecretKey | ||
|
||
XIAOJU_SURVEY_RESPONSE_AES_ENCRYPT_SECRET_KEY= # dataAesEncryptSecretKey | ||
XIAOJU_SURVEY_HTTP_DATA_ENCRYPT_TYPE=rsa | ||
|
||
XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret | ||
XIAOJU_SURVEY_JWT_EXPIRES_IN=8h | ||
|
||
XIAOJU_SURVEY_LOGGER_FILENAME=./logs/app.log | ||
XIAOJU_SURVEY_LOGGER_FILENAME=./logs/app.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { get } from 'lodash'; | ||
import { NoPermissionException } from 'src/exceptions/noPermissionException'; | ||
import { SurveyNotFoundException } from 'src/exceptions/surveyNotFoundException'; | ||
import { SessionService } from 'src/modules/survey/services/session.service'; | ||
import { SurveyMetaService } from 'src/modules/survey/services/surveyMeta.service'; | ||
import { WorkspaceMemberService } from 'src/modules/workspace/services/workspaceMember.service'; | ||
import { CollaboratorService } from 'src/modules/survey/services/collaborator.service'; | ||
|
||
@Injectable() | ||
export class SessionGuard implements CanActivate { | ||
constructor( | ||
private reflector: Reflector, | ||
private readonly sessionService: SessionService, | ||
private readonly surveyMetaService: SurveyMetaService, | ||
private readonly workspaceMemberService: WorkspaceMemberService, | ||
private readonly collaboratorService: CollaboratorService, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const user = request.user; | ||
const sessionIdKey = this.reflector.get<string>( | ||
'sessionId', | ||
context.getHandler(), | ||
); | ||
|
||
const sessionId = get(request, sessionIdKey); | ||
|
||
if (!sessionId) { | ||
throw new NoPermissionException('没有权限'); | ||
} | ||
|
||
const saveSession = await this.sessionService.findOne(sessionId); | ||
|
||
request.saveSession = saveSession; | ||
|
||
const surveyId = saveSession.surveyId; | ||
|
||
const surveyMeta = await this.surveyMetaService.getSurveyById({ surveyId }); | ||
|
||
if (!surveyMeta) { | ||
throw new SurveyNotFoundException('问卷不存在'); | ||
} | ||
|
||
request.surveyMeta = surveyMeta; | ||
|
||
// 兼容老的问卷没有ownerId | ||
if ( | ||
surveyMeta.ownerId === user._id.toString() || | ||
surveyMeta.owner === user.username | ||
) { | ||
// 问卷的owner,可以访问和操作问卷 | ||
return true; | ||
} | ||
|
||
if (surveyMeta.workspaceId) { | ||
const memberInfo = await this.workspaceMemberService.findOne({ | ||
workspaceId: surveyMeta.workspaceId, | ||
userId: user._id.toString(), | ||
}); | ||
if (!memberInfo) { | ||
throw new NoPermissionException('没有权限'); | ||
} | ||
return true; | ||
} | ||
|
||
const permissions = this.reflector.get<string[]>( | ||
'surveyPermission', | ||
context.getHandler(), | ||
); | ||
|
||
if (!Array.isArray(permissions) || permissions.length === 0) { | ||
throw new NoPermissionException('没有权限'); | ||
} | ||
|
||
const info = await this.collaboratorService.getCollaborator({ | ||
surveyId, | ||
userId: user._id.toString(), | ||
}); | ||
|
||
if (!info) { | ||
throw new NoPermissionException('没有权限'); | ||
} | ||
request.collaborator = info; | ||
if ( | ||
permissions.some((permission) => info.permissions.includes(permission)) | ||
) { | ||
return true; | ||
} | ||
throw new NoPermissionException('没有权限'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { Provider } from '@nestjs/common'; | ||
|
||
import { Logger } from './index'; | ||
import { XiaojuSurveyLogger } from './index'; | ||
|
||
export const LoggerProvider: Provider = { | ||
provide: Logger, | ||
useClass: Logger, | ||
provide: XiaojuSurveyLogger, | ||
useClass: XiaojuSurveyLogger, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.