|
|
@@ -34,8 +34,7 @@ export class UsersService implements OnModuleInit {
|
|
|
private readonly userRepository: Repository<Users>,
|
|
|
private readonly hashingService: HashingService,
|
|
|
private readonly smsService: SmsService
|
|
|
- ) {
|
|
|
- }
|
|
|
+ ) {}
|
|
|
|
|
|
async onModuleInit() {
|
|
|
if (!(await this.userRepository.findOneBy({ username: 'admin' }))) {
|
|
|
@@ -116,11 +115,11 @@ export class UsersService implements OnModuleInit {
|
|
|
public async login(username: string, password: string, code: string): Promise<Users> {
|
|
|
let user = await this.userRepository.findOneBy({ username })
|
|
|
if (!user) {
|
|
|
- throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ throw new UnauthorizedException("Username and password doesn't match")
|
|
|
}
|
|
|
const isMatch = await this.hashingService.compare(password, user.password)
|
|
|
if (!isMatch) {
|
|
|
- throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ throw new UnauthorizedException("Username and password doesn't match")
|
|
|
}
|
|
|
if (!user.roles.includes(Role.Admin)) {
|
|
|
if (user.twoFactorCode === null || user.twoFactorCode === '') {
|
|
|
@@ -138,11 +137,11 @@ export class UsersService implements OnModuleInit {
|
|
|
public async binding(username: string, password: string) {
|
|
|
const users = await this.userRepository.findOneBy({ username })
|
|
|
if (!users) {
|
|
|
- throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ throw new UnauthorizedException("Username and password doesn't match")
|
|
|
}
|
|
|
const isMatch = await this.hashingService.compare(password, users.password)
|
|
|
if (!isMatch) {
|
|
|
- throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ throw new UnauthorizedException("Username and password doesn't match")
|
|
|
}
|
|
|
if (users.twoFactorCode) {
|
|
|
return 'success'
|
|
|
@@ -158,11 +157,11 @@ export class UsersService implements OnModuleInit {
|
|
|
public async handleConfirmBinding(username: string, password: string, bindingCode: string) {
|
|
|
const users = await this.userRepository.findOneBy({ username })
|
|
|
if (!users) {
|
|
|
- throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ throw new UnauthorizedException("Username and password doesn't match")
|
|
|
}
|
|
|
const isMatch = await this.hashingService.compare(password, users.password)
|
|
|
if (!isMatch) {
|
|
|
- throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ throw new UnauthorizedException("Username and password doesn't match")
|
|
|
}
|
|
|
if (users.twoFactorCode === null || users.twoFactorCode === '') {
|
|
|
throw new UnauthorizedException('请绑定谷歌验证器获取认证码.')
|
|
|
@@ -176,11 +175,19 @@ export class UsersService implements OnModuleInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ isValidPassword(password: string) {
|
|
|
+ return /[a-z]/.test(password) && /[A-Z]/.test(password) && /[0-9]/.test(password) && password.length >= 8
|
|
|
+ }
|
|
|
+
|
|
|
public async create(userDto: UserCreateDto): Promise<IUsers> {
|
|
|
- try {
|
|
|
- if (userDto.password) {
|
|
|
+ if (userDto.password) {
|
|
|
+ if (this.isValidPassword(userDto.password)) {
|
|
|
userDto.password = await this.hashingService.hash(userDto.password)
|
|
|
+ } else {
|
|
|
+ throw new BadRequestException('密码长度至少8位,且必须包含大小写字母和数字')
|
|
|
}
|
|
|
+ }
|
|
|
+ try {
|
|
|
return await this.userRepository.save(userDto)
|
|
|
} catch (err) {
|
|
|
throw new InternalServerErrorException(err.message)
|
|
|
@@ -210,13 +217,17 @@ export class UsersService implements OnModuleInit {
|
|
|
}
|
|
|
|
|
|
public async updatePassword(id: number, password: string): Promise<Users> {
|
|
|
- try {
|
|
|
- const user = await this.userRepository.findOneBy({ id })
|
|
|
- user.password = await this.hashingService.hash(password)
|
|
|
-
|
|
|
- return await this.userRepository.save(user)
|
|
|
- } catch (err) {
|
|
|
- throw new HttpException(err, HttpStatus.BAD_REQUEST)
|
|
|
+ if (this.isValidPassword(password)) {
|
|
|
+ try {
|
|
|
+ const user = await this.userRepository.findOneBy({ id })
|
|
|
+ user.password = await this.hashingService.hash(password)
|
|
|
+
|
|
|
+ return await this.userRepository.save(user)
|
|
|
+ } catch (err) {
|
|
|
+ throw new HttpException(err, HttpStatus.BAD_REQUEST)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new BadRequestException('密码长度至少8位,且必须包含大小写字母和数字')
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -341,5 +352,4 @@ export class UsersService implements OnModuleInit {
|
|
|
hasInvite: !!user
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|