mirror of
https://github.com/certd/certd.git
synced 2026-04-23 11:37:23 +08:00
perf: 增加系统设置,可以关闭自助注册功能
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { Rule, RuleType } from '@midwayjs/validate';
|
||||
import { Controller, Get, Inject, Provide } from '@midwayjs/decorator';
|
||||
import { BaseController } from '../../../basic/base-controller';
|
||||
import { Constants } from '../../../basic/constants';
|
||||
import { SysSettingsService } from '../../system/service/sys-settings-service';
|
||||
|
||||
export class SmsCodeReq {
|
||||
@Rule(RuleType.number().required())
|
||||
phoneCode: number;
|
||||
|
||||
@Rule(RuleType.string().required())
|
||||
mobile: string;
|
||||
|
||||
@Rule(RuleType.string().required().max(10))
|
||||
randomStr: string;
|
||||
|
||||
@Rule(RuleType.number().required().max(4))
|
||||
imgCode: string;
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
@Provide()
|
||||
@Controller('/api/basic/settings')
|
||||
export class BasicSettingsController extends BaseController {
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
|
||||
@Get('/public', { summary: Constants.per.guest })
|
||||
public async getSysPublic() {
|
||||
const settings = await this.sysSettingsService.readPublicSettings();
|
||||
return this.ok(settings);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@ import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import type { EmailSend } from '@certd/pipeline';
|
||||
import { IEmailService } from '@certd/pipeline';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { SettingsService } from '../../system/service/settings-service';
|
||||
import type SMTPConnection from 'nodemailer/lib/smtp-connection';
|
||||
import { logger } from '../../../utils/logger';
|
||||
import { UserSettingsService } from '../../mine/service/user-settings-service';
|
||||
|
||||
export type EmailConfig = {
|
||||
host: string;
|
||||
@@ -24,7 +24,7 @@ export type EmailConfig = {
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class EmailService implements IEmailService {
|
||||
@Inject()
|
||||
settingsService: SettingsService;
|
||||
settingsService: UserSettingsService;
|
||||
|
||||
/**
|
||||
*/
|
||||
|
||||
@@ -10,6 +10,7 @@ import { BaseController } from '../../../basic/base-controller';
|
||||
import { Constants } from '../../../basic/constants';
|
||||
import { UserService } from '../../authority/service/user-service';
|
||||
import { UserEntity } from '../../authority/entity/user';
|
||||
import { SysSettingsService } from '../../system/service/sys-settings-service';
|
||||
|
||||
/**
|
||||
*/
|
||||
@@ -18,11 +19,19 @@ import { UserEntity } from '../../authority/entity/user';
|
||||
export class RegisterController extends BaseController {
|
||||
@Inject()
|
||||
userService: UserService;
|
||||
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
|
||||
@Post('/register', { summary: Constants.per.guest })
|
||||
public async register(
|
||||
@Body(ALL)
|
||||
user: UserEntity
|
||||
) {
|
||||
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
|
||||
if (sysPublicSettings.registerEnabled === false) {
|
||||
throw new Error('当前站点已禁止自助注册功能');
|
||||
}
|
||||
const newUser = await this.userService.register(user);
|
||||
return this.ok(newUser);
|
||||
}
|
||||
|
||||
+9
-17
@@ -1,24 +1,16 @@
|
||||
import {
|
||||
ALL,
|
||||
Body,
|
||||
Controller,
|
||||
Inject,
|
||||
Post,
|
||||
Provide,
|
||||
Query,
|
||||
} from '@midwayjs/decorator';
|
||||
import { CrudController } from '../../../basic/crud-controller';
|
||||
import { SettingsService } from '../service/settings-service';
|
||||
import { SettingsEntity } from '../entity/settings';
|
||||
import { Constants } from '../../../basic/constants';
|
||||
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/decorator";
|
||||
import { CrudController } from "../../../basic/crud-controller";
|
||||
import { Constants } from "../../../basic/constants";
|
||||
import { UserSettingsService } from "../service/user-settings-service";
|
||||
import { UserSettingsEntity } from "../entity/user-settings";
|
||||
|
||||
/**
|
||||
*/
|
||||
@Provide()
|
||||
@Controller('/api/sys/settings')
|
||||
export class SettingsController extends CrudController<SettingsService> {
|
||||
@Controller('/api/user/settings')
|
||||
export class UserSettingsController extends CrudController<UserSettingsService> {
|
||||
@Inject()
|
||||
service: SettingsService;
|
||||
service: UserSettingsService;
|
||||
|
||||
getService() {
|
||||
return this.service;
|
||||
@@ -61,7 +53,7 @@ export class SettingsController extends CrudController<SettingsService> {
|
||||
}
|
||||
|
||||
@Post('/save', { summary: Constants.per.authOnly })
|
||||
async save(@Body(ALL) bean: SettingsEntity) {
|
||||
async save(@Body(ALL) bean: UserSettingsEntity) {
|
||||
bean.userId = this.ctx.user.id;
|
||||
await this.service.save(bean);
|
||||
return this.ok({});
|
||||
+2
-2
@@ -2,8 +2,8 @@ import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
/**
|
||||
*/
|
||||
@Entity('sys_settings')
|
||||
export class SettingsEntity {
|
||||
@Entity('user_settings')
|
||||
export class UserSettingsEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column({ name: 'user_id', comment: '用户id' })
|
||||
+7
-7
@@ -2,22 +2,22 @@ import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BaseService } from '../../../basic/base-service';
|
||||
import { SettingsEntity } from '../entity/settings';
|
||||
import { UserSettingsEntity } from '../entity/user-settings';
|
||||
|
||||
/**
|
||||
* 授权
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class SettingsService extends BaseService<SettingsEntity> {
|
||||
@InjectEntityModel(SettingsEntity)
|
||||
repository: Repository<SettingsEntity>;
|
||||
export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
@InjectEntityModel(UserSettingsEntity)
|
||||
repository: Repository<UserSettingsEntity>;
|
||||
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
}
|
||||
|
||||
async getById(id: any): Promise<SettingsEntity | null> {
|
||||
async getById(id: any): Promise<UserSettingsEntity | null> {
|
||||
const entity = await this.info(id);
|
||||
if (!entity) {
|
||||
return null;
|
||||
@@ -30,7 +30,7 @@ export class SettingsService extends BaseService<SettingsEntity> {
|
||||
};
|
||||
}
|
||||
|
||||
async getByKey(key: string, userId: number): Promise<SettingsEntity | null> {
|
||||
async getByKey(key: string, userId: number): Promise<UserSettingsEntity | null> {
|
||||
if (!key || !userId) {
|
||||
return null;
|
||||
}
|
||||
@@ -50,7 +50,7 @@ export class SettingsService extends BaseService<SettingsEntity> {
|
||||
return JSON.parse(entity.setting);
|
||||
}
|
||||
|
||||
async save(bean: SettingsEntity) {
|
||||
async save(bean: UserSettingsEntity) {
|
||||
const entity = await this.repository.findOne({
|
||||
where: {
|
||||
key: bean.key,
|
||||
@@ -0,0 +1,80 @@
|
||||
import {
|
||||
ALL,
|
||||
Body,
|
||||
Controller,
|
||||
Inject,
|
||||
Post,
|
||||
Provide,
|
||||
Query,
|
||||
} from '@midwayjs/decorator';
|
||||
import { CrudController } from '../../../basic/crud-controller';
|
||||
import { SysSettingsService } from '../service/sys-settings-service';
|
||||
import { SysSettingsEntity } from '../entity/sys-settings';
|
||||
|
||||
/**
|
||||
*/
|
||||
@Provide()
|
||||
@Controller('/api/sys/settings')
|
||||
export class SysSettingsController extends CrudController<SysSettingsService> {
|
||||
@Inject()
|
||||
service: SysSettingsService;
|
||||
|
||||
getService() {
|
||||
return this.service;
|
||||
}
|
||||
|
||||
@Post('/page', { summary: 'sys:settings:view' })
|
||||
async page(@Body(ALL) body) {
|
||||
body.query = body.query ?? {};
|
||||
body.query.userId = this.ctx.user.id;
|
||||
return super.page(body);
|
||||
}
|
||||
|
||||
@Post('/list', { summary: 'sys:settings:view' })
|
||||
async list(@Body(ALL) body) {
|
||||
body.userId = this.ctx.user.id;
|
||||
return super.list(body);
|
||||
}
|
||||
|
||||
@Post('/add', { summary: 'sys:settings:edit' })
|
||||
async add(@Body(ALL) bean) {
|
||||
bean.userId = this.ctx.user.id;
|
||||
return super.add(bean);
|
||||
}
|
||||
|
||||
@Post('/update', { summary: 'sys:settings:edit' })
|
||||
async update(@Body(ALL) bean) {
|
||||
await this.service.checkUserId(bean.id, this.ctx.user.id);
|
||||
return super.update(bean);
|
||||
}
|
||||
@Post('/info', { summary: 'sys:settings:view' })
|
||||
async info(@Query('id') id) {
|
||||
await this.service.checkUserId(id, this.ctx.user.id);
|
||||
return super.info(id);
|
||||
}
|
||||
|
||||
@Post('/delete', { summary: 'sys:settings:edit' })
|
||||
async delete(@Query('id') id) {
|
||||
await this.service.checkUserId(id, this.ctx.user.id);
|
||||
return super.delete(id);
|
||||
}
|
||||
|
||||
@Post('/save', { summary: 'sys:settings:edit' })
|
||||
async save(@Body(ALL) bean: SysSettingsEntity) {
|
||||
await this.service.save(bean);
|
||||
return this.ok({});
|
||||
}
|
||||
|
||||
@Post('/get', { summary: 'sys:settings:view' })
|
||||
async get(@Query('key') key: string) {
|
||||
const entity = await this.service.getByKey(key);
|
||||
return this.ok(entity);
|
||||
}
|
||||
|
||||
// savePublicSettings
|
||||
@Post('/savePublicSettings', { summary: 'sys:settings:edit' })
|
||||
async savePublicSettings(@Body(ALL) body) {
|
||||
await this.service.savePublicSettings(body);
|
||||
return this.ok({});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
/**
|
||||
*/
|
||||
@Entity('sys_settings')
|
||||
export class SysSettingsEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column({ comment: 'key', length: 100 })
|
||||
key: string;
|
||||
@Column({ comment: '名称', length: 100 })
|
||||
title: string;
|
||||
|
||||
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
|
||||
setting: string;
|
||||
|
||||
// public 公开读,私有写, private 私有读,私有写
|
||||
@Column({ name: 'access', comment: '访问权限' })
|
||||
access: string;
|
||||
|
||||
@Column({
|
||||
name: 'create_time',
|
||||
comment: '创建时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
createTime: Date;
|
||||
@Column({
|
||||
name: 'update_time',
|
||||
comment: '修改时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
updateTime: Date;
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BaseService } from '../../../basic/base-service';
|
||||
import { SysSettingsEntity } from '../entity/sys-settings';
|
||||
import { CacheManager } from '@midwayjs/cache';
|
||||
|
||||
const SYS_PUBLIC_KEY = 'sys.public';
|
||||
const SYS_PRIVATE_KEY = 'sys.private';
|
||||
const CACHE_SYS_PUBLIC_KEY = `settings.${SYS_PUBLIC_KEY}`;
|
||||
const CACHE_SYS_PRIVATE_KEY = `settings.${SYS_PRIVATE_KEY}`;
|
||||
export type SysPublicSettings = {
|
||||
registerEnabled: boolean;
|
||||
};
|
||||
|
||||
export type SysPrivateSettings = NonNullable<unknown>;
|
||||
|
||||
/**
|
||||
* 设置
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class SysSettingsService extends BaseService<SysSettingsEntity> {
|
||||
@InjectEntityModel(SysSettingsEntity)
|
||||
repository: Repository<SysSettingsEntity>;
|
||||
|
||||
@Inject()
|
||||
cache: CacheManager; // 依赖注入CacheManager
|
||||
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
}
|
||||
|
||||
async getById(id: any): Promise<SysSettingsEntity | null> {
|
||||
const entity = await this.info(id);
|
||||
if (!entity) {
|
||||
return null;
|
||||
}
|
||||
const setting = JSON.parse(entity.setting);
|
||||
return {
|
||||
id: entity.id,
|
||||
...setting,
|
||||
};
|
||||
}
|
||||
|
||||
async getByKey(key: string): Promise<SysSettingsEntity | null> {
|
||||
if (!key) {
|
||||
return null;
|
||||
}
|
||||
return await this.repository.findOne({
|
||||
where: {
|
||||
key,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getSettingByKey(key: string): Promise<any | null> {
|
||||
const entity = await this.getByKey(key);
|
||||
if (!entity) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(entity.setting);
|
||||
}
|
||||
|
||||
async save(bean: SysSettingsEntity) {
|
||||
const entity = await this.repository.findOne({
|
||||
where: {
|
||||
key: bean.key,
|
||||
},
|
||||
});
|
||||
if (entity) {
|
||||
entity.setting = bean.setting;
|
||||
await this.repository.save(entity);
|
||||
} else {
|
||||
bean.title = bean.key;
|
||||
await this.repository.save(bean);
|
||||
}
|
||||
}
|
||||
|
||||
async getPublicSettings(): Promise<SysPublicSettings> {
|
||||
const key = CACHE_SYS_PUBLIC_KEY;
|
||||
let settings: SysPublicSettings = await this.cache.get(key);
|
||||
if (settings == null) {
|
||||
settings = await this.readPublicSettings();
|
||||
await this.cache.set(key, settings);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
async readPublicSettings(): Promise<SysPublicSettings> {
|
||||
const key = SYS_PUBLIC_KEY;
|
||||
const entity = await this.getByKey(key);
|
||||
if (!entity) {
|
||||
return {
|
||||
registerEnabled: false,
|
||||
};
|
||||
}
|
||||
return JSON.parse(entity.setting);
|
||||
}
|
||||
|
||||
async savePublicSettings(bean: SysPublicSettings) {
|
||||
const key = SYS_PUBLIC_KEY;
|
||||
const entity = await this.getByKey(key);
|
||||
if (entity) {
|
||||
entity.setting = JSON.stringify(bean);
|
||||
await this.repository.save(entity);
|
||||
} else {
|
||||
const newEntity = new SysSettingsEntity();
|
||||
newEntity.key = key;
|
||||
newEntity.title = '系统公共设置';
|
||||
newEntity.setting = JSON.stringify(bean);
|
||||
newEntity.access = 'public';
|
||||
await this.repository.save(newEntity);
|
||||
}
|
||||
await this.cache.del(CACHE_SYS_PRIVATE_KEY);
|
||||
}
|
||||
|
||||
async readPrivateSettings(): Promise<SysPrivateSettings> {
|
||||
const key = SYS_PRIVATE_KEY;
|
||||
const entity = await this.getByKey(key);
|
||||
if (!entity) {
|
||||
return {};
|
||||
}
|
||||
return JSON.parse(entity.setting);
|
||||
}
|
||||
|
||||
async savePrivateSettings(bean: SysPrivateSettings) {
|
||||
const key = SYS_PRIVATE_KEY;
|
||||
const entity = await this.getByKey(key);
|
||||
if (entity) {
|
||||
entity.setting = JSON.stringify(bean);
|
||||
await this.repository.save(entity);
|
||||
} else {
|
||||
const newEntity = new SysSettingsEntity();
|
||||
newEntity.key = key;
|
||||
newEntity.title = '系统私有设置';
|
||||
newEntity.setting = JSON.stringify(bean);
|
||||
newEntity.access = 'private';
|
||||
await this.repository.save(newEntity);
|
||||
}
|
||||
await this.cache.del(CACHE_SYS_PRIVATE_KEY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user