perf: 支持重置管理员密码,忘记密码的补救方案

This commit is contained in:
xiaojunnuo
2024-06-16 02:06:44 +08:00
parent 5d2d0955b1
commit 732cbc5e92
8 changed files with 97 additions and 13 deletions
@@ -78,6 +78,9 @@ const development = {
certd: {
fileRootDir: '/app/data/files',
},
system: {
resetAdminPasswd: false,
},
} as MidwayConfig;
mergeConfig(development, 'development');
export default development;
@@ -16,6 +16,7 @@ import { AuthorityMiddleware } from './middleware/authority';
import * as staticFile from '@midwayjs/static-file';
import * as cron from './modules/plugin/cron';
import { logger } from './utils/logger';
import { ResetPasswdMiddleware } from './middleware/reset-passwd/middleware';
@Configuration({
imports: [koa, orm, cache, flyway, validateComp, cron, staticFile],
importConfigs: [
@@ -53,6 +54,9 @@ export class ContainerLifeCycle {
PreviewMiddleware,
//授权处理
AuthorityMiddleware,
//resetPasswd,重置密码模式下不提供服务
ResetPasswdMiddleware,
]);
logger.info('当前环境:', this.app.getEnv()); // prod
@@ -1,9 +1,5 @@
import { Config, Provide } from '@midwayjs/decorator';
import {
IMidwayKoaContext,
NextFunction,
IWebMiddleware,
} from '@midwayjs/koa';
import { IMidwayKoaContext, NextFunction, IWebMiddleware } from '@midwayjs/koa';
import { PreviewException } from '../basic/exception/preview-exception';
/**
@@ -20,6 +16,7 @@ export class PreviewMiddleware implements IWebMiddleware {
await next();
return;
}
// eslint-disable-next-line prefer-const
let { url, request } = ctx;
const body: any = request.body;
let id = body.id || request.query.id;
@@ -0,0 +1,45 @@
import {
Autoload,
Config,
Init,
Inject,
Provide,
Scope,
ScopeEnum,
} from '@midwayjs/decorator';
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
import { CommonException } from '../../basic/exception/common-exception';
import { UserService } from '../../modules/authority/service/user-service';
import { logger } from '../../utils/logger';
/**
* 重置密码模式
*/
@Provide()
@Autoload()
@Scope(ScopeEnum.Singleton)
export class ResetPasswdMiddleware implements IWebMiddleware {
@Inject()
userService: UserService;
@Config('system.resetAdminPasswd')
private resetAdminPasswd: boolean;
resolve() {
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
if (this.resetAdminPasswd === true) {
throw new CommonException(
'1号管理员密码已修改为123456,当前为重置密码模式,无法响应请求,请关闭重置密码模式恢复正常服务'
);
}
};
}
@Init()
async init() {
if (this.resetAdminPasswd === true) {
logger.info('开始重置1号管理员用户的密码');
const newPasswd = '123456';
await this.userService.resetPassword(1, newPasswd);
logger.info(`重置1号管理员用户的密码完成,新密码为:${newPasswd}`);
}
}
}
@@ -155,4 +155,12 @@ export class UserService extends BaseService<UserEntity> {
await this.update(param);
}
async resetPassword(userId: any, newPasswd: string) {
const param = {
id: userId,
password: newPasswd,
};
await this.update(param);
}
}