Compare commits

..

2 Commits

Author SHA1 Message Date
c5ee6d20e6 fix 2026-02-01 19:03:16 +08:00
dc56548cdf perf: add get public permission filter 2025-05-20 10:47:44 +08:00
5 changed files with 153 additions and 24 deletions

24
bun.config.ts Normal file
View File

@@ -0,0 +1,24 @@
import { resolvePath } from '@kevisual/use-config';
import { execSync } from 'node:child_process';
const buildFn = async (opts: { entry?: string, naming?: string }) => {
const entry = opts.entry || 'agent/opencode.ts';
const naming = opts.naming || 'opencode';
const external: string[] = ["bun", "ws"];
await Bun.build({
target: 'node',
format: 'esm',
entrypoints: [resolvePath(entry, { meta: import.meta })],
outdir: resolvePath('./dist', { meta: import.meta }),
naming: {
entry: `${naming}.js`,
},
external,
});
const cmd = `dts -i ${entry} -o ${naming}.d.ts`;
execSync(cmd);
};
await buildFn({ naming: 'config-permission', entry: 'src/config-permission.ts' });

View File

@@ -1,15 +1,14 @@
{ {
"name": "@kevisual/permission", "name": "@kevisual/permission",
"version": "0.0.1", "version": "0.0.4",
"description": "", "description": "",
"main": "dist/config-permission.js", "main": "dist/config-permission.js",
"scripts": { "scripts": {
"build": "tsup", "build": "bun run bun.config.ts"
"dev": "tsup --watch",
"dev:lib": "tsup --watch"
}, },
"files": [ "files": [
"dist" "dist",
"src"
], ],
"keywords": [], "keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me>", "author": "abearxiong <xiongxiao@xiongxiao.me>",
@@ -22,6 +21,9 @@
"access": "public" "access": "public"
}, },
"devDependencies": { "devDependencies": {
"tsup": "^8.4.0" "@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.28",
"@types/bun": "^1.3.8",
"@types/node": "^25.1.0"
} }
} }

85
pnpm-lock.yaml generated Normal file
View File

@@ -0,0 +1,85 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.:
devDependencies:
'@kevisual/types':
specifier: ^0.0.12
version: 0.0.12
'@kevisual/use-config':
specifier: ^1.0.28
version: 1.0.28(dotenv@17.2.3)
'@types/bun':
specifier: ^1.3.8
version: 1.3.8
'@types/node':
specifier: ^25.1.0
version: 25.1.0
packages:
'@kevisual/load@0.0.6':
resolution: {integrity: sha512-+3YTFehRcZ1haGel5DKYMUwmi5i6f2psyaPZlfkKU/cOXgkpwoG9/BEqPCnPjicKqqnksEpixVRkyHJ+5bjLVA==}
'@kevisual/types@0.0.12':
resolution: {integrity: sha512-zJXH2dosir3jVrQ6QG4i0+iLQeT9gJ3H+cKXs8ReWboxBSYzUZO78XssVeVrFPsJ33iaAqo4q3DWbSS1dWGn7Q==}
'@kevisual/use-config@1.0.28':
resolution: {integrity: sha512-ngF+LDbjxpXWrZNmnShIKF/jPpAa+ezV+DcgoZIIzHlRnIjE+rr9sLkN/B7WJbiH9C/j1tQXOILY8ujBqILrow==}
peerDependencies:
dotenv: ^17
'@types/bun@1.3.8':
resolution: {integrity: sha512-3LvWJ2q5GerAXYxO2mffLTqOzEu5qnhEAlh48Vnu8WQfnmSwbgagjGZV6BoHKJztENYEDn6QmVd949W4uESRJA==}
'@types/node@25.1.0':
resolution: {integrity: sha512-t7frlewr6+cbx+9Ohpl0NOTKXZNV9xHRmNOvql47BFJKcEG1CxtxlPEEe+gR9uhVWM4DwhnvTF110mIL4yP9RA==}
bun-types@1.3.8:
resolution: {integrity: sha512-fL99nxdOWvV4LqjmC+8Q9kW3M4QTtTR1eePs94v5ctGqU8OeceWrSUaRw3JYb7tU3FkMIAjkueehrHPPPGKi5Q==}
dotenv@17.2.3:
resolution: {integrity: sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==}
engines: {node: '>=12'}
eventemitter3@5.0.4:
resolution: {integrity: sha512-mlsTRyGaPBjPedk6Bvw+aqbsXDtoAyAzm5MO7JgU+yVRyMQ5O8bD4Kcci7BS85f93veegeCPkL8R4GLClnjLFw==}
undici-types@7.16.0:
resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}
snapshots:
'@kevisual/load@0.0.6':
dependencies:
eventemitter3: 5.0.4
'@kevisual/types@0.0.12': {}
'@kevisual/use-config@1.0.28(dotenv@17.2.3)':
dependencies:
'@kevisual/load': 0.0.6
dotenv: 17.2.3
'@types/bun@1.3.8':
dependencies:
bun-types: 1.3.8
'@types/node@25.1.0':
dependencies:
undici-types: 7.16.0
bun-types@1.3.8:
dependencies:
'@types/node': 25.1.0
dotenv@17.2.3: {}
eventemitter3@5.0.4: {}
undici-types@7.16.0: {}

View File

@@ -36,7 +36,7 @@ type ConfigPermissionOptions = {
/** /**
* 所有者 * 所有者
*/ */
owner: string; owner?: string;
}; };
/** /**
@@ -49,6 +49,23 @@ export class ConfigPermission {
this.permission = permission || ({} as Permission); this.permission = permission || ({} as Permission);
this.owner = owner; this.owner = owner;
} }
/**
* 暴露的公共的permission的值
*/
getPublicPermission() {
if (!this.permission?.share) return {};
return {
share: this.permission.share,
};
}
static getDataPublicPermission(value: { data: { permission?: Permission }; [key: string]: any }) {
const { data } = value;
if (data.permission) {
data.permission = new ConfigPermission({ permission: data.permission }).getPublicPermission();
value.data = data;
}
return value;
}
/** /**
* 检查权限是否为公共权限 * 检查权限是否为公共权限
@@ -117,7 +134,7 @@ export class ConfigPermission {
*/ */
checkPassword(password: string) { checkPassword(password: string) {
if (this.permission.password) { if (this.permission.password) {
const ok = this.permission.password !== password; const ok = this.permission.password === password;
return ok ? 'success' : 'error'; return ok ? 'success' : 'error';
} }
return 'none'; return 'none';
@@ -143,6 +160,9 @@ export class ConfigPermission {
} }
return false; return false;
} }
static create(opts: ConfigPermissionOptions) {
return new ConfigPermission(opts);
}
} }
export type UserPermissionOptions = { export type UserPermissionOptions = {
@@ -162,7 +182,12 @@ export class UserPermission extends ConfigPermission {
constructor({ permission, owner }: ConfigPermissionOptions) { constructor({ permission, owner }: ConfigPermissionOptions) {
super({ permission, owner }); super({ permission, owner });
} }
/**
* 检查权限 20100 以上全是错误
*
* @param checkOptions
* @returns
*/
checkPermission(checkOptions: UserPermissionOptions) { checkPermission(checkOptions: UserPermissionOptions) {
const { username, password } = checkOptions; const { username, password } = checkOptions;
@@ -210,15 +235,21 @@ export class UserPermission extends ConfigPermission {
} }
// 第三步,检查用户名 // 第三步,检查用户名
const usernamesCheck = this.checkUsernames(username); const usernamesCheck = this.checkUsernames(username);
if (usernamesCheck) { if (username && usernamesCheck) {
return { return {
code: 20003, code: 20003,
message: '使用用户名访问', message: '使用用户名访问',
}; };
} }
if (username) {
return {
code: 20004,
meassage: '登陆的用户都能够访问',
};
}
return { return {
code: 20101, code: 20101,
message: '资源是私有的', message: '资源是私有的,需要登陆',
}; };
} }
checkPermissionSuccess(checkOptions: UserPermissionOptions) { checkPermissionSuccess(checkOptions: UserPermissionOptions) {

View File

@@ -1,13 +0,0 @@
import { defineConfig } from 'tsup';
export default defineConfig({
entry: ['src/config-permission.ts'],
splitting: false,
sourcemap: false,
clean: true,
format: 'esm',
dts: true,
outDir: 'dist',
tsconfig: 'tsconfig.json',
});