diff --git a/src/api/controllers/configuration.ts b/src/api/controllers/configuration.ts index 18123968..4afab272 100644 --- a/src/api/controllers/configuration.ts +++ b/src/api/controllers/configuration.ts @@ -3,18 +3,30 @@ import { html } from 'hono/html'; import { CommonKVManager } from '@/kv/admin'; import { DingKVManager } from '@/kv/ding'; import { GitHubKVManager } from '@/kv/github'; -import { settingsTypes, SettingType } from '@/kv/types'; +import { + EValidLevel, + LevelSettingsMap, + SettingsNameMap, + SettingType, +} from '@/kv/types'; import UnauthorizedHTML from '@/public/configuration/401.html'; import ConfigurationHTML from '@/public/configuration/configuration.html'; +declare module 'hono' { + interface Context { + validLevel: EValidLevel; + } +} + export function route(hono: THono) { hono.use('/configuration/:id/*', async (c, next) => { const token = c.req.query('token'); const id = c.req.param('id'); const kv = new CommonKVManager(); - const valid = await kv.isTokenValidFor(token, id); - if (valid) { + const validLevel = await kv.isTokenValidFor(token, id); + if (validLevel > EValidLevel.None) { + c.validLevel = validLevel; await next(); return; } @@ -29,14 +41,16 @@ export function route(hono: THono) { const params = new URLSearchParams(); if (token) params.append('token', token); + const settingsTypes = LevelSettingsMap[c.validLevel]; + return c.html( html` -

Hello!

+

Please select an item to continue:

${settingsTypes.map( (v) => html`${v}${v}: ${SettingsNameMap[v]}
`, )} `, ); @@ -49,6 +63,9 @@ export function route(hono: THono) { return c.send.error(404, 'Not Found: id in query'); } const type = c.req.param('type') as SettingType; + + const settingsTypes = LevelSettingsMap[c.validLevel]; + if (!settingsTypes.includes(type as SettingType)) { return c.send.error(404, 'Not Found: type is not valid'); } @@ -77,6 +94,9 @@ export function route(hono: THono) { if (!id) { return c.text('Not Found: id in query', 404); } + + const settingsTypes = LevelSettingsMap[c.validLevel]; + const type = c.req.param('type') as SettingType; if (!settingsTypes.includes(type as SettingType)) { return c.text('Not Found:' + type, 404); diff --git a/src/kv/admin.ts b/src/kv/admin.ts index 5cdd9f37..4e2ad8ac 100644 --- a/src/kv/admin.ts +++ b/src/kv/admin.ts @@ -1,6 +1,6 @@ import { KVManager, Common } from '@/kv'; -import { IAdminInfo } from './types'; +import { EValidLevel, IAdminInfo } from './types'; export class CommonKVManager { kv: KVManager; @@ -9,19 +9,19 @@ export class CommonKVManager { this.kv = KVManager.for(Common.ADMIN_PREFIX); } - async isTokenValidFor(token?: string, scope?: string) { - if (!token) return false; + async isTokenValidFor(token?: string, scope?: string): Promise { + if (!token) return EValidLevel.None; if (scope) { const tokenByScope = await this.getTokenByScope(scope); - if (tokenByScope && tokenByScope === token) return true; + if (tokenByScope && tokenByScope === token) return EValidLevel.Normal; } const adminToken = await this.getAdminToken(); - if (adminToken && adminToken === token) return true; - return false; + if (adminToken && adminToken === token) return EValidLevel.Admin; + return EValidLevel.None; } private async getAdminToken() { - const data = (await this.kv.getJSON('')) ?? { token: '1234' }; + const data = await this.kv.getJSON(''); return data?.token; } private async getTokenByScope(scope: string) { diff --git a/src/kv/types.ts b/src/kv/types.ts index 940c6134..34ee7b6b 100644 --- a/src/kv/types.ts +++ b/src/kv/types.ts @@ -99,12 +99,24 @@ export type SettingType = | 'ding-setting' | 'setting'; -export const settingsTypes = [ - 'app-settings', - 'ding-info', - 'ding-setting', - 'setting', -] as const; +export const SettingsNameMap = { + 'app-settings': 'GitHub App 配置', + 'ding-info': '钉钉群信息配置', + 'ding-setting': '钉钉机器人配置', + setting: 'GitHub Webhooks 配置', +}; + +export enum EValidLevel { + None = 1, + Normal = 1 << 1, + Admin = 1 << 2, +} + +export const LevelSettingsMap = { + [EValidLevel.None]: [], + [EValidLevel.Normal]: ['setting', 'app-settings'], + [EValidLevel.Admin]: ['app-settings', 'ding-info', 'ding-setting', 'setting'], +} as Record; export interface IAdminInfo { token: string; diff --git a/src/public/configuration/configuration.html b/src/public/configuration/configuration.html index 19d73aed..f9d21092 100644 --- a/src/public/configuration/configuration.html +++ b/src/public/configuration/configuration.html @@ -3,8 +3,8 @@ Preference Editor - - + +