fix(user): enforce address count limit when anonymous creation disabled (#819)

This commit is contained in:
Dream Hunter
2026-01-23 22:44:31 +08:00
committed by GitHub
parent decede7ed3
commit f0da9289fc
5 changed files with 89 additions and 45 deletions

View File

@@ -1,5 +1,7 @@
import { Context } from "hono";
import { createMimeMessage } from "mimetext";
import { UserSettings, RoleAddressConfig } from "./models";
import { CONSTANTS } from "./constants";
export const getJsonObjectValue = <T = any>(
value: string | any
@@ -303,6 +305,45 @@ export const hashPassword = async (password: string): Promise<string> => {
return hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
}
export const getMaxAddressCount = async (
c: Context<HonoCustomType>,
userRole: string | null | undefined,
settings: UserSettings
): Promise<number> => {
if (!userRole) return settings.maxAddressCount;
const roleConfigs = await getJsonSetting<RoleAddressConfig>(c, CONSTANTS.ROLE_ADDRESS_CONFIG_KEY);
if (!roleConfigs) return settings.maxAddressCount;
const roleMaxCount = roleConfigs[userRole]?.maxAddressCount;
if (typeof roleMaxCount !== 'number') return settings.maxAddressCount;
if (roleMaxCount <= 0) return settings.maxAddressCount;
return roleMaxCount;
};
/**
* 检查用户是否已达到地址数量限制
* @param c - Hono Context
* @param user_id - 用户 ID
* @param userRole - 用户角色
* @returns true 表示已超限false 表示未超限
*/
export const isAddressCountLimitReached = async (
c: Context<HonoCustomType>,
user_id: number | string,
userRole: string | null | undefined
): Promise<boolean> => {
const value = await getJsonSetting(c, CONSTANTS.USER_SETTINGS_KEY);
const settings = new UserSettings(value);
const maxAddressCount = await getMaxAddressCount(c, userRole, settings);
if (maxAddressCount <= 0) return false;
const { count } = await c.env.DB.prepare(
`SELECT COUNT(*) as count FROM users_address where user_id = ?`
).bind(user_id).first<{ count: number }>() || { count: 0 };
return count >= maxAddressCount;
};
export default {
getJsonObjectValue,
getSetting,