Skip to content

Commit

Permalink
refactor: configuration add level
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 27, 2023
1 parent fee3c5a commit 9afcb1a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
30 changes: 25 additions & 5 deletions src/api/controllers/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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`<!DOCTYPE html>
<h1>Hello!</h1>
<h1>Please select an item to continue:</h1>
${settingsTypes.map(
(v) =>
html`<a href="/configuration/${id}/${v}?${params.toString()}"
>${v}</a
>${v}: ${SettingsNameMap[v]}</a
><br />`,
)} `,
);
Expand All @@ -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');
}
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/kv/admin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { KVManager, Common } from '@/kv';

import { IAdminInfo } from './types';
import { EValidLevel, IAdminInfo } from './types';

export class CommonKVManager {
kv: KVManager<IAdminInfo>;
Expand All @@ -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<EValidLevel> {
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) {
Expand Down
24 changes: 18 additions & 6 deletions src/kv/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EValidLevel, SettingType[]>;

export interface IAdminInfo {
token: string;
Expand Down
4 changes: 2 additions & 2 deletions src/public/configuration/configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<meta charset="utf-8" />
<title>Preference Editor</title>
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.14.0/src-noconflict/ace.js"></script>
<script src="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/json-editor/2.10.1/jsoneditor.min.js"></script>
<script src="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/ace/1.14.0/ace.js"></script>
<link rel="stylesheet" id="theme-link" />
<link rel="stylesheet" id="iconlib-link" />
</head>
Expand Down

0 comments on commit 9afcb1a

Please sign in to comment.