This commit is contained in:
xiaojunnuo
2024-07-18 11:17:13 +08:00
parent a438028002
commit 303097b835
14 changed files with 133 additions and 106 deletions
@@ -13,6 +13,8 @@ import { Constants } from '../../../basic/constants.js';
import { UserRoleEntity } from '../entity/user-role.js';
import { randomText } from 'svg-captcha';
import bcrypt from 'bcryptjs';
import { SysSettingsService } from '../../system/service/sys-settings-service.js';
import { SysInstallInfo } from '../../system/service/models.js';
/**
* 系统用户
@@ -29,6 +31,9 @@ export class UserService extends BaseService<UserEntity> {
@Inject()
userRoleService: UserRoleService;
@Inject()
sysSettingsService: SysSettingsService;
getRepository() {
return this.repository;
}
@@ -88,7 +93,7 @@ export class UserService extends BaseService<UserEntity> {
delete param.username;
if (!_.isEmpty(param.password)) {
param.passwordVersion = 2;
param.password = this.genPassword(param.password, param.passwordVersion);
param.password = await this.genPassword(param.password, param.passwordVersion);
} else {
delete param.password;
}
@@ -96,30 +101,33 @@ export class UserService extends BaseService<UserEntity> {
await this.roleService.updateRoles(param.id, param.roles);
}
private genPassword(plainPassword: any, passwordVersion: number) {
private async genPassword(rawPassword: any, passwordVersion: number) {
if (passwordVersion == null || passwordVersion <= 1) {
return md5(plainPassword);
return md5(rawPassword);
}
const salt = bcrypt.genSaltSync(10);
const plainPassword = await this.buildPlainPassword(rawPassword);
return bcrypt.hashSync(plainPassword, salt);
}
async findOne(param) {
async findOne(param: any) {
return this.repository.findOne({
where: param,
});
}
async checkPassword(
rawPassword: any,
hashPassword: any,
passwordVersion: number
) {
async checkPassword(rawPassword: any, hashPassword: any, passwordVersion: number) {
if (passwordVersion == null || passwordVersion <= 1) {
return this.genPassword(rawPassword, passwordVersion) === hashPassword;
return (await this.genPassword(rawPassword, passwordVersion)) === hashPassword;
}
const plainPassword = await this.buildPlainPassword(rawPassword);
return bcrypt.compareSync(plainPassword, hashPassword);
}
return bcrypt.compareSync(rawPassword, hashPassword); // true
async buildPlainPassword(rawPassword: string) {
const setting: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const prefixSiteId = setting.siteId.substring(1, 5);
return rawPassword + prefixSiteId;
}
/**
@@ -147,17 +155,14 @@ export class UserService extends BaseService<UserEntity> {
status: 1,
passwordVersion: 2,
});
newUser.password = this.genPassword(
newUser.password,
newUser.passwordVersion
);
if (!newUser.password) {
newUser.password = randomText(6);
}
newUser.password = await this.genPassword(newUser.password, newUser.passwordVersion);
await this.transaction(async txManager => {
newUser = await txManager.save(newUser);
const userRole: UserRoleEntity = UserRoleEntity.of(
newUser.id,
Constants.role.defaultUser
);
const userRole: UserRoleEntity = UserRoleEntity.of(newUser.id, Constants.role.defaultUser);
await txManager.save(userRole);
});
@@ -167,11 +172,7 @@ export class UserService extends BaseService<UserEntity> {
async changePassword(userId: any, form: any) {
const user = await this.info(userId);
const passwordChecked = await this.checkPassword(
form.password,
user.password,
user.passwordVersion
);
const passwordChecked = await this.checkPassword(form.password, user.password, user.passwordVersion);
if (!passwordChecked) {
throw new CommonException('原密码错误');
}
@@ -137,15 +137,14 @@ export class PipelineService extends BaseService<PipelineEntity> {
name: `pipeline.${id}.trigger.once`,
cron: null,
job: async () => {
logger.info('job准备启动,当前定时器数量:', this.cron.getListSize());
logger.info('用户手动启动job');
try {
await this.run(id, null);
} catch (e) {
logger.error('定时job执行失败:', e);
logger.error('手动job执行失败:', e);
}
},
});
logger.info('定时器数量:', this.cron.getListSize());
}
async delete(id: number) {
@@ -182,7 +181,11 @@ export class PipelineService extends BaseService<PipelineEntity> {
cron: cron,
job: async () => {
logger.info('定时任务触发:', pipelineId, trigger.id);
await this.run(pipelineId, trigger.id);
try {
await this.run(pipelineId, trigger.id);
} catch (e) {
logger.error('定时job执行失败:', e);
}
},
});
logger.info('当前定时器数量:', this.cron.getListSize());
@@ -244,17 +247,16 @@ export class PipelineService extends BaseService<PipelineEntity> {
const executor = runningTasks.get(historyId);
if (executor) {
await executor.cancel();
} else {
const entity = await this.historyService.info(historyId);
if (entity == null) {
return;
}
const pipeline: Pipeline = JSON.parse(entity.pipeline);
pipeline.status.status = ResultType.canceled;
pipeline.status.result = ResultType.canceled;
const runtime = new RunHistory(historyId, null, pipeline);
await this.saveHistory(runtime);
}
const entity = await this.historyService.info(historyId);
if (entity == null) {
return;
}
const pipeline: Pipeline = JSON.parse(entity.pipeline);
pipeline.status.status = ResultType.canceled;
pipeline.status.result = ResultType.canceled;
const runtime = new RunHistory(historyId, null, pipeline);
await this.saveHistory(runtime);
}
private getTriggerType(triggerId, pipeline) {
@@ -1,15 +1,9 @@
import {
ALL,
Body,
Controller,
Inject,
Post,
Provide,
Query,
} from '@midwayjs/core';
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { CrudController } from '../../../basic/crud-controller.js';
import { SysSettingsService } from '../service/sys-settings-service.js';
import { SysSettingsEntity } from '../entity/sys-settings.js';
import { SysPublicSettings } from '../service/models.js';
import * as _ from 'lodash-es';
/**
*/
@@ -74,7 +68,9 @@ export class SysSettingsController extends CrudController<SysSettingsService> {
// savePublicSettings
@Post('/savePublicSettings', { summary: 'sys:settings:edit' })
async savePublicSettings(@Body(ALL) body) {
await this.service.savePublicSettings(body);
const setting = new SysPublicSettings();
_.merge(setting, body);
await this.service.savePublicSettings(setting);
return this.ok({});
}
}