From 912d3196cf3f80453688c8dcf9d3241378c34f1f Mon Sep 17 00:00:00 2001 From: xion Date: Sun, 30 Mar 2025 13:38:48 +0800 Subject: [PATCH] fix: fix permission auth --- src/module/get-user-app.ts | 11 ---- src/module/html/create-refresh-html.ts | 2 +- src/module/index.ts | 39 +++++++++++++- src/route/app/list.ts | 73 ++++++++++++++++++++++---- src/scripts/copy.ts | 47 ----------------- 5 files changed, 100 insertions(+), 72 deletions(-) delete mode 100644 src/scripts/copy.ts diff --git a/src/module/get-user-app.ts b/src/module/get-user-app.ts index a593c62..7bc4647 100644 --- a/src/module/get-user-app.ts +++ b/src/module/get-user-app.ts @@ -286,17 +286,6 @@ export class UserApp { data: 'loading', }; } - async getAllCacheData() { - const app = this.app; - const user = this.user; - const key = 'user:app:' + app + ':' + user; - const value = await redis.get(key); - console.log('getAllCacheData', JSON.parse(value)); - const exist = await redis.get('user:app:exist:' + app + ':' + user); - console.log('getAllCacheData:exist', exist); - const files = await redis.hgetall('user:app:set:' + app + ':' + user); - console.log('getAllCacheData:files', files); - } async clearCacheData() { const app = this.app; const user = this.user; diff --git a/src/module/html/create-refresh-html.ts b/src/module/html/create-refresh-html.ts index bd927c4..74e5220 100644 --- a/src/module/html/create-refresh-html.ts +++ b/src/module/html/create-refresh-html.ts @@ -176,7 +176,7 @@ export const createRefreshHtml = (user, app) => { count++; loadCount.innerHTML = count.toString(); - fetch(origin + '/api/proxy?user=${user}&app=${app}&path=app&key=status') + fetch(origin + '/api/router?user=${user}&app=${app}&path=page-proxy-app&key=status') .then((res) => { if (res.status === 200) { window.location.reload(); diff --git a/src/module/index.ts b/src/module/index.ts index 9b5139b..80a1844 100644 --- a/src/module/index.ts +++ b/src/module/index.ts @@ -213,8 +213,43 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR if (!isExist) { return createNotFoundPage(); } - const notAuthUser = ['root', 'admin', 'user', 'public']; - if (!notAuthUser.includes(user)) { + const notAuthPathList = [ + { + user: 'root', + paths: ['center'], + }, + { + user: 'admin', + paths: ['center'], + }, + { + user: 'user', + paths: ['login'], + }, + { + user: 'public', + paths: ['center'], + all: true, + }, + { + user: 'test', + paths: ['center'], + all: true, + }, + ]; + const checkNotAuthPath = (user, app) => { + const notAuthPath = notAuthPathList.find((item) => { + if (item.user === user) { + if (item.all) { + return true; + } + return item.paths?.includes?.(app); + } + return false; + }); + return notAuthPath; + }; + if (!checkNotAuthPath(user, app)) { const { permission } = isExist; const permissionInstance = new UserPermission({ permission, owner: user }); const loginUser = await getLoginUser(req); diff --git a/src/route/app/list.ts b/src/route/app/list.ts index 121b0ac..351a3af 100644 --- a/src/route/app/list.ts +++ b/src/route/app/list.ts @@ -1,12 +1,63 @@ -import { UserApp } from '@/module/get-user-app.ts'; +import { deleteUserAppFiles } from '@/module/get-user-app.ts'; import { app } from '../../app.ts'; import { redis } from '@/module/redis/redis.ts'; import fs from 'fs'; import { fileStore } from '../../module/config.ts'; +import { getAppLoadStatus } from '@/module/redis/get-app-status.ts'; +export class CenterUserApp { + user: string; + app: string; + constructor({ user, app }: { user: string; app: string }) { + this.user = user; + this.app = app; + } + async clearCache() { + const keys = await redis.keys('user:app:*'); + return keys; + } + async getCache() { + const app = this.app; + const user = this.user; + const key = 'user:app:' + app + ':' + user; + const value = await redis.get(key); + if (!value) { + return null; + } + return JSON.parse(value); + } + async getLoaded() { + const app = this.app; + const user = this.user; + const value = await getAppLoadStatus(user, app); + return value; + } + async clearCacheData() { + const app = this.app; + const user = this.user; + const key = 'user:app:' + app + ':' + user; + await redis.del(key); + await redis.del('user:app:exist:' + app + ':' + user); + await redis.del('user:app:set:' + app + ':' + user); + await redis.del('user:app:status:' + app + ':' + user); + await redis.del('user:app:permission:' + app + ':' + user); + const userDomainApp = 'user:domain:app:' + user + ':' + app; + const domainKeys = await redis.get(userDomainApp); + if (domainKeys) { + const domainKeysList = JSON.parse(domainKeys); + domainKeysList.forEach(async (domain: string) => { + await redis.del('domain:' + domain); + }); + } + await redis.del(userDomainApp); + + // 删除所有文件 + deleteUserAppFiles(user, app); + } +} app .route({ - path: 'app', + path: 'page-proxy-app', key: 'auth-admin', id: 'auth-admin', }) @@ -20,7 +71,7 @@ app app .route({ - path: 'app', + path: 'page-proxy-app', key: 'list', middleware: ['auth-admin'], description: '获取应用列表', @@ -41,14 +92,14 @@ app app .route({ - path: 'app', + path: 'page-proxy-app', key: 'delete', middleware: ['auth-admin'], }) .define(async (ctx) => { const { user, app } = ctx.query; try { - const userApp = new UserApp({ user, app }); + const userApp = new CenterUserApp({ user, app }); await userApp.clearCacheData(); } catch (error) { console.error(error); @@ -60,7 +111,7 @@ app app .route({ - path: 'app', + path: 'page-proxy-app', key: 'deleteAll', }) .define(async (ctx) => { @@ -76,7 +127,7 @@ app app .route({ - path: 'app', + path: 'page-proxy-app', key: 'clear', }) .define(async (ctx) => { @@ -94,7 +145,7 @@ app app .route({ - path: 'app', + path: 'page-proxy-app', key: 'get', middleware: ['auth-admin'], }) @@ -108,7 +159,7 @@ app ctx.throw('app is required'); } } - const userApp = new UserApp({ user, app }); + const userApp = new CenterUserApp({ user, app }); const cache = await userApp.getCache(); if (!cache) { ctx.throw('Not Found App'); @@ -119,13 +170,13 @@ app app .route({ - path: 'app', + path: 'page-proxy-app', key: 'status', middleware: [], }) .define(async (ctx) => { const { user, app } = ctx.query; - const userApp = new UserApp({ user, app }); + const userApp = new CenterUserApp({ user, app }); const status = await userApp.getLoaded(); ctx.body = status; }) diff --git a/src/scripts/copy.ts b/src/scripts/copy.ts deleted file mode 100644 index a34ad92..0000000 --- a/src/scripts/copy.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { UserApp, clearAllUserApp } from '../module/get-user-app.ts'; -import { redis } from '../module/redis/redis.ts'; -import path from 'path'; -import { config, fileStore } from '../module/config.ts'; - -const main = async () => { - const userApp = new UserApp({ user: 'root', app: 'codeflow' }); - const res = await userApp.setCacheData(); - console.log(res); - // userApp.close(); - process.exit(0); -}; - -// main(); - -const getAll = async () => { - const userApp = new UserApp({ user: 'root', app: 'codeflow' }); - const res = await userApp.getAllCacheData(); - userApp.close(); -}; - -// getAll(); - -// console.log('path', path.join(filePath, '/module/get-user-app.ts')); - -const clearData = async () => { - const userApp = new UserApp({ user: 'root', app: 'codeflow' }); - const res = await userApp.clearCacheData(); - process.exit(0); -}; - -// clearData(); -// clearAllUserApp(); - -const expireData = async () => { - await redis.set('user:app:exist:' + 'codeflow:root', 'value', 'EX', 2); - process.exit(0); -}; - -// expireData(); - -const keysData = async () => { - const keys = await redis.keys('user:app:exist:*'); - console.log('keys', keys); - process.exit(0); -}; -keysData();