2024-10-03 22:03:49 +08:00
|
|
|
import { Autoload, Config, Init, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
2024-06-16 02:06:44 +08:00
|
|
|
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
|
2025-09-27 01:47:53 +08:00
|
|
|
import { CommonException, SysSettingsService } from "@certd/lib-server";
|
2024-10-07 03:21:16 +08:00
|
|
|
import { UserService } from '../../modules/sys/authority/service/user-service.js';
|
2024-11-04 16:39:02 +08:00
|
|
|
import { logger } from '@certd/basic';
|
2025-07-09 14:49:11 +08:00
|
|
|
import {UserSettingsService} from "../../modules/mine/service/user-settings-service.js";
|
2024-06-16 02:06:44 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置密码模式
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Autoload()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
|
|
|
|
export class ResetPasswdMiddleware implements IWebMiddleware {
|
|
|
|
|
@Inject()
|
|
|
|
|
userService: UserService;
|
2025-07-09 14:49:11 +08:00
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
userSettingsService: UserSettingsService;
|
2025-09-27 01:47:53 +08:00
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
2025-07-09 14:49:11 +08:00
|
|
|
|
2024-06-16 02:06:44 +08:00
|
|
|
@Config('system.resetAdminPasswd')
|
|
|
|
|
private resetAdminPasswd: boolean;
|
|
|
|
|
resolve() {
|
|
|
|
|
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
|
|
|
|
|
if (this.resetAdminPasswd === true) {
|
2024-10-03 22:03:49 +08:00
|
|
|
throw new CommonException('1号管理员密码已修改为123456,当前为重置密码模式,无法响应请求,请关闭重置密码模式恢复正常服务');
|
2024-06-16 02:06:44 +08:00
|
|
|
}
|
2024-06-16 02:12:02 +08:00
|
|
|
await next();
|
2024-06-16 02:06:44 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Init()
|
|
|
|
|
async init() {
|
|
|
|
|
if (this.resetAdminPasswd === true) {
|
|
|
|
|
logger.info('开始重置1号管理员用户的密码');
|
|
|
|
|
const newPasswd = '123456';
|
|
|
|
|
await this.userService.resetPassword(1, newPasswd);
|
2024-10-28 10:26:14 +08:00
|
|
|
await this.userService.updateStatus(1, 1);
|
2025-07-09 14:49:11 +08:00
|
|
|
await this.userSettingsService.deleteWhere({
|
|
|
|
|
userId: 1,
|
|
|
|
|
key:"user.two.factor"
|
|
|
|
|
})
|
2025-09-27 01:47:53 +08:00
|
|
|
const publicSettings = await this.sysSettingsService.getPublicSettings()
|
|
|
|
|
publicSettings.captchaEnabled = false
|
|
|
|
|
await this.sysSettingsService.savePublicSettings(publicSettings);
|
|
|
|
|
|
2025-01-16 11:49:09 +08:00
|
|
|
const user = await this.userService.info(1);
|
2025-09-27 01:47:53 +08:00
|
|
|
logger.info(`重置1号管理员用户的密码完成,2FA设置已删除,验证码登录已禁用,用户名:${user.username},新密码:${newPasswd},请在登录进去之后尽快修改密码`);
|
2024-06-16 02:06:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|