Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: |Worker| NO_LIMIT_SEND_ROLE support multi role splited by ',' #588

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## main(v0.9.0)

- feat: | Worker | 支持多语言
- feat: | Worker | `NO_LIMIT_SEND_ROLE` 配置支持多角色, 逗号分割

## v0.8.7

Expand Down
3 changes: 2 additions & 1 deletion vitepress-docs/docs/en/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
2 changes: 1 addition & 1 deletion vitepress-docs/docs/zh/guide/worker-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 用户角色配置说明
>
Expand Down
4 changes: 2 additions & 2 deletions worker/src/admin_api/worker_config.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions worker/src/mails_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions worker/src/mails_api/send_mail_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) || [];
Expand Down
11 changes: 10 additions & 1 deletion worker/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = <T = any>(
value: string | any
Expand Down Expand Up @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion worker/wrangler.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down