新增权限域名限制和链接背景上传

This commit is contained in:
eoao
2025-07-26 23:11:00 +08:00
parent 0e86b93e0c
commit 12833cd901
36 changed files with 584 additions and 354 deletions

View File

@@ -48,9 +48,17 @@ const accountService = {
const userRow = await userService.selectById(c, userId);
const roleRow = await roleService.selectById(c, userRow.type);
if (roleRow.accountCount && userRow.email !== c.env.admin) {
const userAccountCount = await accountService.countUserAccount(c, userId)
if(userAccountCount >= roleRow.accountCount) throw new BizError(t('accountLimit'), 403);
if (userRow.email !== c.env.admin) {
if (roleRow.accountCount > 0) {
const userAccountCount = await accountService.countUserAccount(c, userId)
if(userAccountCount >= roleRow.accountCount) throw new BizError(t('accountLimit'), 403);
}
if(!roleService.hasAvailDomainPerm(roleRow.availDomain, email)) {
throw new BizError(t('noDomainPermAdd'),403)
}
}
if (await settingService.isAddEmailVerify(c)) {

View File

@@ -159,6 +159,7 @@ const emailService = {
}
if (attDataList.length > 0 && !r2Domain) {
throw new BizError(t('noOsDomainSendPic'));
}
@@ -186,6 +187,18 @@ const emailService = {
throw new BizError(t('senderAccountNotExist'));
}
if (accountRow.userId !== userId) {
throw new BizError(t('sendEmailNotCurUser'));
}
if (c.env.admin !== userRow.email) {
if(!roleService.hasAvailDomainPerm(roleRow.availDomain, accountRow.email)) {
throw new BizError(t('noDomainPermSend'),403)
}
}
const domain = emailUtils.getDomain(accountRow.email);
const resendToken = resendTokens[domain];
@@ -194,10 +207,6 @@ const emailService = {
}
if (accountRow.userId !== userId) {
throw new BizError(t('sendEmailNotCurUser'));
}
if (!name) {
name = emailUtils.getName(accountRow.email);
}

View File

@@ -25,7 +25,11 @@ const loginService = {
const { email, password, token, code } = params;
const {regKey, register} = await settingService.query(c)
const {regKey, register, registerVerify} = await settingService.query(c)
if (registerVerify === settingConst.registerVerify.OPEN) {
await turnstileService.verify(c,token)
}
if (register === settingConst.register.CLOSE) {
throw new BizError(t('regDisabled'));
@@ -76,10 +80,6 @@ const loginService = {
throw new BizError(t('isRegAccount'));
}
if (await settingService.isRegisterVerify(c)) {
await turnstileService.verify(c,token)
}
const { salt, hash } = await saltHashUtils.hashPassword(password);
let defType = null
@@ -89,6 +89,21 @@ const loginService = {
defType = roleRow.roleId
}
const roleRow = await roleService.selectById(c, type || defType);
if(!roleService.hasAvailDomainPerm(roleRow.availDomain, email)) {
if (type) {
throw new BizError(t('noDomainPermRegKey'),403)
}
if (defType) {
throw new BizError(t('noDomainPermReg'),403)
}
}
const userId = await userService.insert(c, { email, regKeyId,password: hash, salt, type: type || defType });
await userService.updateUserInfo(c, userId, true);

View File

@@ -9,12 +9,13 @@ import userService from './user-service';
import user from '../entity/user';
import verifyUtils from '../utils/verify-utils';
import { t } from '../i18n/i18n.js';
import emailUtils from '../utils/email-utils';
const roleService = {
async add(c, params, userId) {
let { name, permIds, banEmail } = params;
let { name, permIds, banEmail, availDomain } = params;
if (!name) {
throw new BizError(t('emptyRoleName'));
@@ -32,9 +33,11 @@ const roleService = {
throw new BizError(t('notEmail'));
}
banEmail = banEmail.join(',')
banEmail = banEmail.join(',');
roleRow = await orm(c).insert(role).values({...params, banEmail, userId}).returning().get();
availDomain = availDomain.join(',');
roleRow = await orm(c).insert(role).values({...params, banEmail, availDomain, userId}).returning().get();
if (permIds.length === 0) {
return;
@@ -55,7 +58,8 @@ const roleService = {
.where(eq(perm.type, permConst.type.BUTTON)).all();
roleList.forEach(role => {
role.banEmail = role.banEmail.split(",").filter(item => item !== "")
role.banEmail = role.banEmail.split(",").filter(item => item !== "");
role.availDomain = role.availDomain.split(",").filter(item => item !== "");
role.permIds = permList.filter(perm => perm.roleId === role.roleId).map(perm => perm.permId);
});
@@ -64,7 +68,7 @@ const roleService = {
async setRole(c, params) {
let { name, permIds, roleId, banEmail } = params;
let { name, permIds, roleId, banEmail, availDomain } = params;
if (!name) {
throw new BizError(t('emptyRoleName'));
@@ -80,7 +84,9 @@ const roleService = {
banEmail = banEmail.join(',')
await orm(c).update(role).set({...params, banEmail}).where(eq(role.roleId, roleId)).run();
availDomain = availDomain.join(',')
await orm(c).update(role).set({...params, banEmail, availDomain}).where(eq(role.roleId, roleId)).run();
await orm(c).delete(rolePerm).where(eq(rolePerm.roleId, roleId)).run();
if (permIds.length > 0) {
@@ -150,6 +156,24 @@ const roleService = {
selectByUserId(c, userId) {
return orm(c).select(role).from(user).leftJoin(role, eq(role.roleId, user.type)).where(eq(user.userId, userId)).get();
},
hasAvailDomainPerm(availDomain, email) {
availDomain = availDomain.split(',').filter(item => item !== '');
if (availDomain.length === 0) {
return true
}
const availIndex = availDomain.findIndex(item => {
const domain = emailUtils.getDomain(email.toLowerCase());
const availDomainItem = emailUtils.getDomain(item.toLowerCase());
console.log(domain,availDomainItem)
return domain === availDomainItem
})
return availIndex > -1
}
};

View File

@@ -69,30 +69,41 @@ const settingService = {
const settingRow = await this.query(c);
let { background } = params
if (!background.startsWith('http')) {
if (!c.env.r2) {
throw new BizError(t('noOsUpBack'));
}
if (!settingRow.r2Domain) {
throw new BizError(t('noOsDomainUpBack'));
}
const file = fileUtils.base64ToFile(background)
const arrayBuffer = await file.arrayBuffer();
background = constant.BACKGROUND_PREFIX + await fileUtils.getBuffHash(arrayBuffer) + fileUtils.getExtFileName(file.name);
await r2Service.putObj(c, background, arrayBuffer, {
contentType: file.type
});
if (!c.env.r2) {
throw new BizError(t('noOsUpBack'));
}
if (!settingRow.r2Domain) {
throw new BizError(t('noOsDomainUpBack'));
}
const { background } = params;
const file = fileUtils.base64ToFile(background);
const arrayBuffer = await file.arrayBuffer();
const key = constant.BACKGROUND_PREFIX + await fileUtils.getBuffHash(arrayBuffer) + fileUtils.getExtFileName(file.name);
await r2Service.putObj(c, key, file, {
contentType: file.type
});
if (settingRow.background) {
await r2Service.delete(c, settingRow.background);
try {
await r2Service.delete(c, settingRow.background);
} catch (e) {
console.error(e)
}
}
await orm(c).update(setting).set({ background: key }).run();
await orm(c).update(setting).set({ background }).run();
await this.refresh(c);
return key;
return background;
},
async physicsDeleteAll(c) {