You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
41 lines
1.2 KiB
import {
|
|
ensureKnownConfigKey,
|
|
setGlobalConfigValue,
|
|
} from "#server/service/config";
|
|
import { toPublicConfigError } from "#server/service/config/errors";
|
|
import type { H3Event } from "h3";
|
|
|
|
type UpdateGlobalConfigBody = {
|
|
key: string;
|
|
value: unknown;
|
|
};
|
|
|
|
async function assertCanManageGlobalConfig(event: H3Event) {
|
|
const user = await event.context.auth.requireUser();
|
|
// 当前版本先采用最小权限策略:仅首个系统用户可写全局配置。
|
|
if (user.id !== 1) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
statusMessage: "无权限修改全局配置",
|
|
data: {
|
|
code: "CONFIG_FORBIDDEN",
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
try {
|
|
await assertCanManageGlobalConfig(event);
|
|
const body = await readBody<UpdateGlobalConfigBody>(event);
|
|
const key = ensureKnownConfigKey(body.key);
|
|
await setGlobalConfigValue(key, body.value);
|
|
const value = await event.context.config.getGlobal(key);
|
|
return R.success({
|
|
key,
|
|
value,
|
|
});
|
|
} catch (err) {
|
|
throw toPublicConfigError(err);
|
|
}
|
|
});
|
|
|