diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d462cb17..897766dfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ## main(v0.9.0) - feat: | Worker | 支持多语言 +- feat: | Worker | `NO_LIMIT_SEND_ROLE` 配置支持多角色, 逗号分割 ## v0.8.7 diff --git a/vitepress-docs/docs/en/cli.md b/vitepress-docs/docs/en/cli.md index ed0a0144a..57c8c6459 100644 --- a/vitepress-docs/docs/en/cli.md +++ b/vitepress-docs/docs/en/cli.md @@ -121,7 +121,8 @@ ENABLE_AUTO_REPLY = false # DISABLE_SHOW_GITHUB = true # Disable Show GitHub link # default send balance, if not set, it will be 0 # DEFAULT_SEND_BALANCE = 1 -# NO_LIMIT_SEND_ROLE = "vip" # the role which can send emails without limit +# the role which can send emails without limit, multiple roles can be separated by , +# NO_LIMIT_SEND_ROLE = "vip" # Turnstile verification configuration # CF_TURNSTILE_SITE_KEY = "" # CF_TURNSTILE_SECRET_KEY = "" diff --git a/vitepress-docs/docs/zh/guide/worker-vars.md b/vitepress-docs/docs/zh/guide/worker-vars.md index bd9a8c253..cd4f1624a 100644 --- a/vitepress-docs/docs/zh/guide/worker-vars.md +++ b/vitepress-docs/docs/zh/guide/worker-vars.md @@ -75,7 +75,7 @@ | `ADMIN_USER_ROLE` | 文本 | admin 角色配置, 如果用户角色等于 ADMIN_USER_ROLE 则可以访问 admin 控制台 | `admin` | | `USER_ROLES` | JSON | - | 见下方 | | `DISABLE_ANONYMOUS_USER_CREATE_EMAIL` | 文本/JSON | 禁用匿名用户创建邮箱,如果设置为 true,则用户只能在登录后创建邮箱地址 | `true` | -| `NO_LIMIT_SEND_ROLE` | 文本 | 可以无限发送邮件的角色 | `vip` | +| `NO_LIMIT_SEND_ROLE` | 文本 | 可以无限发送邮件的角色, 多个角色使用逗号分割 `vip,admin` | `vip` | > [!NOTE] USER_ROLES 用户角色配置说明 > diff --git a/worker/src/admin_api/worker_config.ts b/worker/src/admin_api/worker_config.ts index 7bfa5acb3..30013a7a9 100644 --- a/worker/src/admin_api/worker_config.ts +++ b/worker/src/admin_api/worker_config.ts @@ -1,7 +1,7 @@ import { Context } from 'hono'; import { HonoCustomType } from '../types'; -import { getAdminPasswords, getBooleanValue, getDefaultDomains, getDomains, getIntValue, getPasswords, getStringArray, getStringValue, getUserRoles, getAnotherWorkerList } from '../utils'; +import { getAdminPasswords, getBooleanValue, getDefaultDomains, getDomains, getIntValue, getPasswords, getStringArray, getStringValue, getUserRoles, getAnotherWorkerList, getSplitStringListValue } from '../utils'; import { CONSTANTS } from '../constants'; import { isS3Enabled } from '../mails_api/s3_attachment'; @@ -30,7 +30,7 @@ export default { "ADMIN_USER_ROLE": getStringValue(c.env.ADMIN_USER_ROLE), "USER_DEFAULT_ROLE": getStringValue(c.env.USER_DEFAULT_ROLE), "USER_ROLES": getUserRoles(c), - "NO_LIMIT_SEND_ROLE": getStringValue(c.env.NO_LIMIT_SEND_ROLE), + "NO_LIMIT_SEND_ROLE": getSplitStringListValue(c.env.NO_LIMIT_SEND_ROLE), "ADMIN_CONTACT": c.env.ADMIN_CONTACT, "ENABLE_USER_CREATE_EMAIL": getBooleanValue(c.env.ENABLE_USER_CREATE_EMAIL), diff --git a/worker/src/mails_api/index.ts b/worker/src/mails_api/index.ts index d083bf48f..b89b53121 100644 --- a/worker/src/mails_api/index.ts +++ b/worker/src/mails_api/index.ts @@ -2,7 +2,7 @@ import { Hono } from 'hono' import i18n from '../i18n'; import { HonoCustomType } from "../types"; -import { getBooleanValue, getJsonSetting, checkCfTurnstile, getStringValue } from '../utils'; +import { getBooleanValue, getJsonSetting, checkCfTurnstile, getStringValue, getSplitStringListValue } from '../utils'; import { newAddress, handleListQuery, deleteAddressWithData, getAddressPrefix, getAllowDomains } from '../common' import { CONSTANTS } from '../constants' import auto_reply from './auto_reply' @@ -97,7 +97,8 @@ api.get('/api/settings', async (c) => { } catch (e) { console.warn("Failed to update address") } - const is_no_limit_send_balance = user_role && user_role === getStringValue(c.env.NO_LIMIT_SEND_ROLE); + const no_limit_roles = getSplitStringListValue(c.env.NO_LIMIT_SEND_ROLE); + const is_no_limit_send_balance = user_role && no_limit_roles.includes(user_role); const balance = is_no_limit_send_balance ? 99999 : await c.env.DB.prepare( `SELECT balance FROM address_sender where address = ? and enabled = 1` ).bind(address).first("balance"); diff --git a/worker/src/mails_api/send_mail_api.ts b/worker/src/mails_api/send_mail_api.ts index 9f00a43c5..067bc497d 100644 --- a/worker/src/mails_api/send_mail_api.ts +++ b/worker/src/mails_api/send_mail_api.ts @@ -6,7 +6,7 @@ import { WorkerMailer, WorkerMailerOptions } from 'worker-mailer'; import i18n from '../i18n'; import { CONSTANTS } from '../constants' -import { getJsonSetting, getDomains, getIntValue, getBooleanValue, getStringValue, getJsonObjectValue } from '../utils'; +import { getJsonSetting, getDomains, getIntValue, getBooleanValue, getStringValue, getJsonObjectValue, getSplitStringListValue } from '../utils'; import { GeoData } from '../models' import { handleListQuery } from '../common' import { HonoCustomType } from '../types'; @@ -137,7 +137,8 @@ export const sendMail = async ( throw new Error("Invalid domain") } const user_role = c.get("userRolePayload"); - const is_no_limit_send_balance = user_role && user_role === getStringValue(c.env.NO_LIMIT_SEND_ROLE); + const no_limit_roles = getSplitStringListValue(c.env.NO_LIMIT_SEND_ROLE); + const is_no_limit_send_balance = user_role && no_limit_roles.includes(user_role); // no need find noLimitSendAddressList if is_no_limit_send_balance const noLimitSendAddressList = is_no_limit_send_balance ? [] : await getJsonSetting(c, CONSTANTS.NO_LIMIT_SEND_ADDRESS_LIST_KEY) || []; diff --git a/worker/src/utils.ts b/worker/src/utils.ts index b9273bd33..344286804 100644 --- a/worker/src/utils.ts +++ b/worker/src/utils.ts @@ -1,6 +1,6 @@ import { Context } from "hono"; import { createMimeMessage } from "mimetext"; -import { HonoCustomType, UserRole,AnotherWorker } from "./types"; +import { HonoCustomType, UserRole, AnotherWorker } from "./types"; export const getJsonObjectValue = ( value: string | any @@ -69,6 +69,15 @@ export const getStringValue = (value: any): string => { return ""; } +export const getSplitStringListValue = ( + value: any, demiliter: string = "," +): string[] => { + const valueToSplit = getStringValue(value); + return valueToSplit.split(demiliter) + .map((item: string) => item.trim()) + .filter((item: string) => item.length > 0); +} + export const getBooleanValue = ( value: boolean | string | any ): boolean => { diff --git a/worker/wrangler.toml.template b/worker/wrangler.toml.template index ff01b7abc..f29df687c 100644 --- a/worker/wrangler.toml.template +++ b/worker/wrangler.toml.template @@ -63,7 +63,8 @@ ENABLE_AUTO_REPLY = false # DISABLE_SHOW_GITHUB = true # default send balance, if not set, it will be 0 # DEFAULT_SEND_BALANCE = 1 -# NO_LIMIT_SEND_ROLE = "vip" # the role which can send emails without limit +# the role which can send emails without limit, multiple roles can be separated by , +# NO_LIMIT_SEND_ROLE = "vip" # Turnstile verification # CF_TURNSTILE_SITE_KEY = "" # CF_TURNSTILE_SECRET_KEY = ""