diff --git a/package.json b/package.json index b15ba2c..bb02251 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/permission", - "version": "0.0.1", + "version": "0.0.3", "description": "", "main": "dist/config-permission.js", "scripts": { diff --git a/src/config-permission.ts b/src/config-permission.ts index c305d51..d88bd6a 100644 --- a/src/config-permission.ts +++ b/src/config-permission.ts @@ -36,7 +36,7 @@ type ConfigPermissionOptions = { /** * 所有者 */ - owner: string; + owner?: string; }; /** @@ -49,6 +49,23 @@ export class ConfigPermission { this.permission = permission || ({} as Permission); 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; + } /** * 检查权限是否为公共权限 @@ -143,6 +160,9 @@ export class ConfigPermission { } return false; } + static create(opts: ConfigPermissionOptions) { + return new ConfigPermission(opts); + } } export type UserPermissionOptions = { @@ -162,7 +182,12 @@ export class UserPermission extends ConfigPermission { constructor({ permission, owner }: ConfigPermissionOptions) { super({ permission, owner }); } - + /** + * 检查权限 20100 以上全是错误 + * + * @param checkOptions + * @returns + */ checkPermission(checkOptions: UserPermissionOptions) { const { username, password } = checkOptions; @@ -210,15 +235,21 @@ export class UserPermission extends ConfigPermission { } // 第三步,检查用户名 const usernamesCheck = this.checkUsernames(username); - if (usernamesCheck) { + if (username && usernamesCheck) { return { code: 20003, message: '使用用户名访问', }; } + if (username) { + return { + code: 20004, + meassage: '登陆的用户都能够访问', + }; + } return { code: 20101, - message: '资源是私有的', + message: '资源是私有的,需要登陆', }; } checkPermissionSuccess(checkOptions: UserPermissionOptions) {