fix: fix permission auth

This commit is contained in:
xion 2025-03-30 13:38:48 +08:00
parent 87014f4d74
commit 912d3196cf
5 changed files with 100 additions and 72 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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);

View File

@ -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;
})

View File

@ -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();