mirror of
https://github.com/certd/certd.git
synced 2026-07-10 23:27:34 +08:00
chore: format
This commit is contained in:
@@ -1,34 +1,33 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
/**
|
||||
*/
|
||||
@Entity('user_settings')
|
||||
@Entity("user_settings")
|
||||
export class UserSettingsEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column({ name: 'user_id', comment: '用户id' })
|
||||
@Column({ name: "user_id", comment: "用户id" })
|
||||
userId: number;
|
||||
@Column({ comment: 'key', length: 100 })
|
||||
@Column({ comment: "key", length: 100 })
|
||||
key: string;
|
||||
@Column({ comment: '名称', length: 100 })
|
||||
@Column({ comment: "名称", length: 100 })
|
||||
title: string;
|
||||
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
|
||||
@Column({ name: "setting", comment: "设置", length: 1024, nullable: true })
|
||||
setting: string;
|
||||
|
||||
@Column({ name: 'project_id', comment: '项目Id' })
|
||||
@Column({ name: "project_id", comment: "项目Id" })
|
||||
projectId: number;
|
||||
|
||||
@Column({
|
||||
name: 'create_time',
|
||||
comment: '创建时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
name: "create_time",
|
||||
comment: "创建时间",
|
||||
default: () => "CURRENT_TIMESTAMP",
|
||||
})
|
||||
createTime: Date;
|
||||
@Column({
|
||||
name: 'update_time',
|
||||
comment: '修改时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
name: "update_time",
|
||||
comment: "修改时间",
|
||||
default: () => "CURRENT_TIMESTAMP",
|
||||
})
|
||||
updateTime: Date;
|
||||
|
||||
}
|
||||
|
||||
@@ -4,65 +4,57 @@ export type TwoFactorAuthenticator = {
|
||||
enabled: boolean;
|
||||
secret?: string;
|
||||
type?: string;
|
||||
verified?:boolean;
|
||||
}
|
||||
verified?: boolean;
|
||||
};
|
||||
|
||||
export class UserTwoFactorSetting extends BaseSettings {
|
||||
static __title__ = "用户多重认证设置";
|
||||
static __key__ = "user.two.factor";
|
||||
|
||||
authenticator: TwoFactorAuthenticator = {
|
||||
enabled:false,
|
||||
verified:false,
|
||||
enabled: false,
|
||||
verified: false,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class UserSiteMonitorSetting extends BaseSettings {
|
||||
static __title__ = "站点监控设置";
|
||||
static __key__ = "user.site.monitor";
|
||||
|
||||
notificationId?:number= 0;
|
||||
cron?:string = undefined;
|
||||
retryTimes?:number = 3;
|
||||
dnsServer?:string[] = undefined;
|
||||
certValidDays?:number = 14;
|
||||
notificationId?: number = 0;
|
||||
cron?: string = undefined;
|
||||
retryTimes?: number = 3;
|
||||
dnsServer?: string[] = undefined;
|
||||
certValidDays?: number = 14;
|
||||
}
|
||||
|
||||
|
||||
export class UserDomainMonitorSetting extends BaseSettings {
|
||||
static __title__ = "域名到期监控设置";
|
||||
static __key__ = "user.domain.monitor";
|
||||
|
||||
enabled?:boolean = false;
|
||||
notificationId?:number= 0;
|
||||
cron?:string = undefined;
|
||||
willExpireDays?:number = 30;
|
||||
enabled?: boolean = false;
|
||||
notificationId?: number = 0;
|
||||
cron?: string = undefined;
|
||||
willExpireDays?: number = 30;
|
||||
}
|
||||
|
||||
|
||||
export class UserEmailSetting extends BaseSettings {
|
||||
static __title__ = "用户邮箱设置";
|
||||
static __key__ = "user.email";
|
||||
|
||||
list:string[] = [];
|
||||
list: string[] = [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class UserGrantSetting extends BaseSettings {
|
||||
static __title__ = "用户授权设置";
|
||||
static __key__ = "user.grant";
|
||||
|
||||
allowAdminViewCerts:boolean = false;
|
||||
allowAdminViewCerts = false;
|
||||
}
|
||||
|
||||
|
||||
export class UserDomainImportSetting extends BaseSettings {
|
||||
static __title__ = "用户域名导入设置";
|
||||
static __key__ = "user.domain.import";
|
||||
|
||||
domainImportList:{dnsProviderType:string,dnsProviderAccessId:number,key:string,title:string,icon?:string}[];
|
||||
domainImportList: { dnsProviderType: string; dnsProviderAccessId: number; key: string; title: string; icon?: string }[];
|
||||
}
|
||||
|
||||
@@ -14,15 +14,14 @@ export class TwoFactorService {
|
||||
@Inject()
|
||||
userService: UserService;
|
||||
|
||||
|
||||
async getAuthenticatorQrCode(userId: any) {
|
||||
const setting = await this.getSetting(userId)
|
||||
const setting = await this.getSetting(userId);
|
||||
|
||||
const authenticatorSetting = setting.authenticator;
|
||||
if (!authenticatorSetting.secret) {
|
||||
const { authenticator } = await import("otplib");
|
||||
const { authenticator } = await import("otplib");
|
||||
|
||||
authenticatorSetting.secret = authenticator.generateSecret()
|
||||
authenticatorSetting.secret = authenticator.generateSecret();
|
||||
await this.userSettingsService.saveSetting(userId, null, setting);
|
||||
}
|
||||
|
||||
@@ -34,14 +33,13 @@ export class TwoFactorService {
|
||||
//生成qrcode base64
|
||||
const qrcode = await import("qrcode");
|
||||
const qrcodeBase64 = await qrcode.toDataURL(qrcodeContent);
|
||||
return {qrcode:qrcodeBase64,link:qrcodeContent,secret}
|
||||
|
||||
return { qrcode: qrcodeBase64, link: qrcodeContent, secret };
|
||||
}
|
||||
|
||||
async saveAuthenticator(req: { userId: any; verifyCode: any }) {
|
||||
const userId = req.userId;
|
||||
const { authenticator } = await import("otplib");
|
||||
const setting = await this.getSetting(userId)
|
||||
const setting = await this.getSetting(userId);
|
||||
|
||||
const authenticatorSetting = setting.authenticator;
|
||||
if (!authenticatorSetting.secret) {
|
||||
@@ -62,26 +60,25 @@ export class TwoFactorService {
|
||||
await this.userSettingsService.saveSetting(userId, null, setting);
|
||||
}
|
||||
|
||||
async offAuthenticator(userId:number) {
|
||||
async offAuthenticator(userId: number) {
|
||||
if (!userId || userId <= 0) {
|
||||
throw new Error("userId is required");
|
||||
}
|
||||
|
||||
const setting = await this.getSetting(userId)
|
||||
const setting = await this.getSetting(userId);
|
||||
setting.authenticator.enabled = false;
|
||||
setting.authenticator.verified = false;
|
||||
setting.authenticator.secret = '';
|
||||
setting.authenticator.secret = "";
|
||||
await this.userSettingsService.saveSetting(userId, null, setting);
|
||||
}
|
||||
|
||||
async getSetting(userId:number) {
|
||||
async getSetting(userId: number) {
|
||||
return await this.userSettingsService.getSetting<UserTwoFactorSetting>(userId, null, UserTwoFactorSetting);
|
||||
|
||||
}
|
||||
|
||||
async verifyAuthenticatorCode(userId: any, verifyCode: string) {
|
||||
const { authenticator } = await import("otplib");
|
||||
const setting = await this.getSetting(userId)
|
||||
const setting = await this.getSetting(userId);
|
||||
if (!setting.authenticator.enabled) {
|
||||
throw new Error("authenticator 未开启");
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Repository } from "typeorm";
|
||||
import { BaseService, BaseSettings } from "@certd/lib-server";
|
||||
import { UserSettingsEntity } from "../entity/user-settings.js";
|
||||
import { LocalCache, mergeUtils } from "@certd/basic";
|
||||
const {merge} = mergeUtils
|
||||
const { merge } = mergeUtils;
|
||||
|
||||
const UserSettingCache = new LocalCache({
|
||||
clearInterval: 5 * 60 * 1000,
|
||||
@@ -33,13 +33,13 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
const setting = JSON.parse(entity.setting);
|
||||
return {
|
||||
id: entity.id,
|
||||
...setting
|
||||
...setting,
|
||||
};
|
||||
}
|
||||
|
||||
async getByKey(key: string, userId: number, projectId: number): Promise<UserSettingsEntity | null> {
|
||||
if(userId == null){
|
||||
throw new Error('userId is required');
|
||||
if (userId == null) {
|
||||
throw new Error("userId is required");
|
||||
}
|
||||
if (!key) {
|
||||
return null;
|
||||
@@ -48,14 +48,14 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
where: {
|
||||
key,
|
||||
userId,
|
||||
projectId
|
||||
}
|
||||
projectId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getSettingByKey(key: string, userId: number, projectId: number): Promise<any | null> {
|
||||
if(userId == null){
|
||||
throw new Error('userId is required');
|
||||
if (userId == null) {
|
||||
throw new Error("userId is required");
|
||||
}
|
||||
const entity = await this.getByKey(key, userId, projectId);
|
||||
if (!entity) {
|
||||
@@ -69,8 +69,8 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
where: {
|
||||
key: bean.key,
|
||||
userId: bean.userId,
|
||||
projectId: bean.projectId
|
||||
}
|
||||
projectId: bean.projectId,
|
||||
},
|
||||
});
|
||||
if (entity) {
|
||||
entity.setting = bean.setting;
|
||||
@@ -81,15 +81,14 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getSetting<T>( userId: number, projectId: number,type: any, cache:boolean = false): Promise<T> {
|
||||
if(userId==null){
|
||||
throw new Error('userId is required');
|
||||
async getSetting<T>(userId: number, projectId: number, type: any, cache = false): Promise<T> {
|
||||
if (userId == null) {
|
||||
throw new Error("userId is required");
|
||||
}
|
||||
const key = type.__key__;
|
||||
let cacheKey = key + '_' + userId ;
|
||||
let cacheKey = key + "_" + userId;
|
||||
if (projectId) {
|
||||
cacheKey += '_' + projectId;
|
||||
cacheKey += "_" + projectId;
|
||||
}
|
||||
|
||||
if (cache) {
|
||||
@@ -109,23 +108,23 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
return newSetting;
|
||||
}
|
||||
|
||||
async saveSetting<T extends BaseSettings>(userId:number, projectId: number,bean: T) {
|
||||
if(userId == null){
|
||||
throw new Error('userId is required');
|
||||
async saveSetting<T extends BaseSettings>(userId: number, projectId: number, bean: T) {
|
||||
if (userId == null) {
|
||||
throw new Error("userId is required");
|
||||
}
|
||||
const old = await this.getSetting(userId, projectId,bean.constructor)
|
||||
bean = merge(old,bean)
|
||||
const old = await this.getSetting(userId, projectId, bean.constructor);
|
||||
bean = merge(old, bean);
|
||||
|
||||
const type: any = bean.constructor;
|
||||
const key = type.__key__;
|
||||
if(!key){
|
||||
if (!key) {
|
||||
throw new Error(`${type.name} must have __key__`);
|
||||
}
|
||||
const entity = await this.getByKey(key,userId, projectId);
|
||||
const entity = await this.getByKey(key, userId, projectId);
|
||||
const newEntity = new UserSettingsEntity();
|
||||
if (entity) {
|
||||
newEntity.id = entity.id;
|
||||
}else{
|
||||
} else {
|
||||
newEntity.key = key;
|
||||
newEntity.title = type.__title__;
|
||||
newEntity.userId = userId;
|
||||
@@ -134,5 +133,4 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
|
||||
newEntity.setting = JSON.stringify(bean);
|
||||
await this.repository.save(newEntity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user