lint
This commit is contained in:
@@ -25,7 +25,7 @@ import { AuthGuard } from '@nestjs/passport';
|
||||
@UseGuards(AuthGuard('jwt'), RolesGuard)
|
||||
@Roles(Role.Admin)
|
||||
export class AdminUserController {
|
||||
constructor(private readonly userService: UserService) { }
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Get()
|
||||
async list(@Query() listDto: ListDto) {
|
||||
|
||||
@@ -20,7 +20,7 @@ import { ResourceService } from 'src/resource/resource.service';
|
||||
@UseGuards(AuthGuard('jwt'), RolesGuard)
|
||||
@Roles(Role.Admin)
|
||||
export class AdminWebResourceController {
|
||||
constructor(private readonly resourceService: ResourceService) { }
|
||||
constructor(private readonly resourceService: ResourceService) {}
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
|
||||
@@ -30,10 +30,12 @@ import { ThrottlerModule } from '@nestjs/throttler';
|
||||
}),
|
||||
PassportModule.register({ defaultStrategy: 'jwt' }),
|
||||
ThrottlerModule.forRoot({
|
||||
throttlers: [{
|
||||
limit: 1000,
|
||||
ttl: 60000, // 1 minute
|
||||
}],
|
||||
throttlers: [
|
||||
{
|
||||
limit: 1000,
|
||||
ttl: 60000, // 1 minute
|
||||
},
|
||||
],
|
||||
}),
|
||||
UserModule,
|
||||
AuthModule,
|
||||
@@ -47,4 +49,4 @@ import { ThrottlerModule } from '@nestjs/throttler';
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule { }
|
||||
export class AppModule {}
|
||||
|
||||
@@ -17,7 +17,7 @@ export class AuthController {
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly userSessionService: UserSessionService,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
@Post('login')
|
||||
@UseGuards(ThrottlerGuard)
|
||||
|
||||
@@ -10,7 +10,6 @@ import { JwtStrategy } from './strategies/jwt.strategy';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { VerificationModule } from 'src/verification/verification.module';
|
||||
import { OptionalAuthGuard } from './strategies/OptionalAuthGuard';
|
||||
import { NotificationModule } from 'src/notification/notification.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export enum Role {
|
||||
Admin = 'admin',
|
||||
}
|
||||
Admin = 'admin',
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
@@ -37,7 +41,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
|
||||
return {
|
||||
...user,
|
||||
sessionId
|
||||
sessionId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Ip,
|
||||
Param,
|
||||
ParseUUIDPipe,
|
||||
Post,
|
||||
@@ -21,7 +20,7 @@ export class BlogController {
|
||||
constructor(
|
||||
private readonly blogService: BlogService,
|
||||
private readonly userService: UserService,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
getBlogs() {
|
||||
|
||||
@@ -11,7 +11,7 @@ export class BlogService {
|
||||
private readonly blogRepository: Repository<Blog>,
|
||||
@InjectRepository(BlogComment)
|
||||
private readonly blogCommentRepository: Repository<BlogComment>,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
async list() {
|
||||
return this.blogRepository.find({
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
import { BadRequestException, CanActivate, ExecutionContext, ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { Role } from 'src/auth/role.enum';
|
||||
import { User } from 'src/user/entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(
|
||||
private reflector: Reflector,
|
||||
) { }
|
||||
constructor(private reflector: Reflector) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const requiredRoles = this.reflector.getAllAndOverride<Role[] | undefined>('roles', [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
const requiredRoles = this.reflector.getAllAndOverride<Role[] | undefined>(
|
||||
'roles',
|
||||
[context.getHandler(), context.getClass()],
|
||||
);
|
||||
|
||||
if (!requiredRoles) return true;
|
||||
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user as (User | void);
|
||||
const user = request.user as User | void;
|
||||
|
||||
if (!user) {
|
||||
throw new BadRequestException('服务器内部错误');
|
||||
}
|
||||
|
||||
if (!requiredRoles.some(role => user.roles.includes(role))) {
|
||||
if (!requiredRoles.some((role) => user.roles.includes(role))) {
|
||||
throw new ForbiddenException('权限不足');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
|
||||
import Dm20151123, * as $Dm20151123 from '@alicloud/dm20151123';
|
||||
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
|
||||
import Client, * as $dm from "@alicloud/dm20151123";
|
||||
import Util, * as $Util from '@alicloud/tea-util';
|
||||
import * as $OpenApi from '@alicloud/openapi-client';
|
||||
// import Client, * as $dm from '@alicloud/dm20151123';
|
||||
import * as $Util from '@alicloud/tea-util';
|
||||
import Credential, { Config } from '@alicloud/credentials';
|
||||
|
||||
@Injectable()
|
||||
export class NotificationService {
|
||||
|
||||
private dm: Dm20151123;
|
||||
|
||||
constructor() {
|
||||
@@ -23,7 +22,7 @@ export class NotificationService {
|
||||
this.dm = new Dm20151123(config);
|
||||
}
|
||||
|
||||
private getMailHtmlBody(option: { type: 'login-verify', code: string }) {
|
||||
private getMailHtmlBody(option: { type: 'login-verify'; code: string }) {
|
||||
if (option.type === 'login-verify') {
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -76,25 +75,31 @@ export class NotificationService {
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>`
|
||||
</html>`;
|
||||
} else {
|
||||
throw new Error('未配置的模版');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async sendMail(option: { type: 'login-verify', targetMail: string, code: string; }) {
|
||||
async sendMail(option: {
|
||||
type: 'login-verify';
|
||||
targetMail: string;
|
||||
code: string;
|
||||
}) {
|
||||
const runtime = new $Util.RuntimeOptions({});
|
||||
|
||||
const singleSendMailRequest = new $Dm20151123.SingleSendMailRequest({
|
||||
accountName: "security@tonesc.cn",
|
||||
accountName: 'security@tonesc.cn',
|
||||
addressType: 1,
|
||||
replyToAddress: false,
|
||||
toAddress: `${option.targetMail}`,
|
||||
subject: "【特恩的日志】登陆验证码",
|
||||
htmlBody: this.getMailHtmlBody({ type: 'login-verify', code: option.code }),
|
||||
textBody: "",
|
||||
})
|
||||
subject: '【特恩的日志】登陆验证码',
|
||||
htmlBody: this.getMailHtmlBody({
|
||||
type: 'login-verify',
|
||||
code: option.code,
|
||||
}),
|
||||
textBody: '',
|
||||
});
|
||||
|
||||
try {
|
||||
await this.dm.singleSendMailWithOptions(singleSendMailRequest, runtime);
|
||||
|
||||
@@ -28,4 +28,4 @@ export class UserSession {
|
||||
|
||||
/**
|
||||
* 考虑是否使用sessionId代替id,以节省存储空间
|
||||
*/
|
||||
*/
|
||||
|
||||
@@ -18,7 +18,7 @@ export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @deprecated 尽量不使用该方法
|
||||
@@ -40,7 +40,7 @@ export class UserService {
|
||||
return this.userRepository.findOne({
|
||||
where: {
|
||||
userId,
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import { BadRequestException, Body, Controller, Post, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Controller,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { SendVerificationCodeDto } from './dto/send-verification-code.dto';
|
||||
import { VerificationService } from './verification.service';
|
||||
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
|
||||
|
||||
@Controller('verification')
|
||||
export class VerificationController {
|
||||
constructor(private readonly verificationService: VerificationService) { }
|
||||
constructor(private readonly verificationService: VerificationService) {}
|
||||
|
||||
@Post('send')
|
||||
@UseGuards(ThrottlerGuard)
|
||||
|
||||
@@ -5,7 +5,7 @@ import { NotificationService } from 'src/notification/notification.service';
|
||||
export class VerificationService {
|
||||
private readonly logger = new Logger(VerificationService.name);
|
||||
|
||||
constructor(private readonly notificationService: NotificationService) { }
|
||||
constructor(private readonly notificationService: NotificationService) {}
|
||||
|
||||
private pool: Map<
|
||||
string,
|
||||
@@ -51,10 +51,12 @@ export class VerificationService {
|
||||
this.saveCode(key, code);
|
||||
this.logger.log(`Email[${email}] code: ${code}`);
|
||||
// 发送验证码
|
||||
await this.notificationService.sendMail({ type: 'login-verify', targetMail: email, code, }).catch(() => {
|
||||
this.clearCode(key);
|
||||
throw new BadRequestException('发送失败,请稍后再试');
|
||||
})
|
||||
await this.notificationService
|
||||
.sendMail({ type: 'login-verify', targetMail: email, code })
|
||||
.catch(() => {
|
||||
this.clearCode(key);
|
||||
throw new BadRequestException('发送失败,请稍后再试');
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ export default function Page() {
|
||||
refreshSTSToken: async () => {
|
||||
await storeMeta.refresh();
|
||||
if (!storeMeta.stsTokenData) throw new Error();
|
||||
const { AccessKeyId, AccessKeySecret, SecurityToken } = data;
|
||||
const { AccessKeyId, AccessKeySecret, SecurityToken } = storeMeta.stsTokenData;
|
||||
return {
|
||||
accessKeyId: AccessKeyId,
|
||||
accessKeySecret: AccessKeySecret,
|
||||
@@ -65,6 +65,7 @@ export default function Page() {
|
||||
ossStore.setStore(store);
|
||||
ossStore.setWorkDir(`tone-page/${data.userId}`)
|
||||
ossStore.loadObjectList();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- storeMeta引用会导致无限循环,依赖stsTokenData即可
|
||||
}, [storeMeta.stsTokenData]);
|
||||
|
||||
const handleRefreshFileList = async () => ossStore.loadObjectList().catch(e => toast.error(e.message));
|
||||
|
||||
Reference in New Issue
Block a user