@dofe/sso-hooks 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ interface Scope {
2
+ module: string;
3
+ resource: string;
4
+ action: string;
5
+ }
6
+ /**
7
+ * 权限检查 Hook
8
+ *
9
+ * 根据当前用户拥有的 scope 列表,检查是否具有指定的模块权限。
10
+ * scope 列表通常来自认证响应(JWT claims)或 GET /user/permissions 接口。
11
+ *
12
+ * 权限映射规则:
13
+ * hasPermission(module, resource, action)
14
+ * → 查找 scope: { module, resource, action }
15
+ * → 对应 Scope.name = `${action}:${resource}`, Resource.indicator = module
16
+ */
17
+ export declare function usePermissions(scopes?: Scope[]): {
18
+ hasPermission: (module: string, resource: string, action: string) => boolean;
19
+ hasAnyPermission: (permissions: Array<{
20
+ module: string;
21
+ resource: string;
22
+ action: string;
23
+ }>) => boolean;
24
+ hasAllPermissions: (permissions: Array<{
25
+ module: string;
26
+ resource: string;
27
+ action: string;
28
+ }>) => boolean;
29
+ };
30
+ export {};
31
+ //# sourceMappingURL=usePermissions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePermissions.d.ts","sourceRoot":"","sources":["../src/usePermissions.ts"],"names":[],"mappings":"AAIA,UAAU,KAAK;IACb,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,MAAM,GAAE,KAAK,EAAO;4BAUvC,MAAM,YACJ,MAAM,UACR,MAAM,KACb,OAAO;oCAMK,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,KACvE,OAAO;qCAKK,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,KACvE,OAAO;EAMX"}
@@ -0,0 +1,37 @@
1
+ 'use client';
2
+ import { useMemo } from 'react';
3
+ /**
4
+ * 权限检查 Hook
5
+ *
6
+ * 根据当前用户拥有的 scope 列表,检查是否具有指定的模块权限。
7
+ * scope 列表通常来自认证响应(JWT claims)或 GET /user/permissions 接口。
8
+ *
9
+ * 权限映射规则:
10
+ * hasPermission(module, resource, action)
11
+ * → 查找 scope: { module, resource, action }
12
+ * → 对应 Scope.name = `${action}:${resource}`, Resource.indicator = module
13
+ */
14
+ export function usePermissions(scopes = []) {
15
+ const scopeSet = useMemo(() => {
16
+ const set = new Set();
17
+ for (const s of scopes) {
18
+ set.add(`${s.module}:${s.resource}:${s.action}`);
19
+ }
20
+ return set;
21
+ }, [scopes]);
22
+ const hasPermission = (module, resource, action) => {
23
+ if (scopes.length === 0)
24
+ return false;
25
+ return scopeSet.has(`${module}:${resource}:${action}`);
26
+ };
27
+ const hasAnyPermission = (permissions) => {
28
+ return permissions.some((p) => hasPermission(p.module, p.resource, p.action));
29
+ };
30
+ const hasAllPermissions = (permissions) => {
31
+ if (permissions.length === 0)
32
+ return false;
33
+ return permissions.every((p) => hasPermission(p.module, p.resource, p.action));
34
+ };
35
+ return { hasPermission, hasAnyPermission, hasAllPermissions };
36
+ }
37
+ //# sourceMappingURL=usePermissions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePermissions.js","sourceRoot":"","sources":["../src/usePermissions.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAQhC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,cAAc,CAAC,SAAkB,EAAE;IACjD,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE;QAC5B,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;QAC9B,KAAK,MAAM,CAAC,IAAI,MAAM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 ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./*": {
13
+ "types": "./dist/*.d.ts",
14
+ "default": "./dist/*.js"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "build": "tsc -p tsconfig.build.json",
19
+ "clean": "rm -rf dist",
20
+ "typecheck": "tsc --noEmit",
21
+ "prepack": "node scripts/prepack.mjs",
22
+ "postpack": "node scripts/postpack.mjs",
23
+ "publish:npm": "npm publish --access public",
24
+ "publish:npm:otp": "npm publish --access public --otp",
25
+ "release:patch": "npm version patch && pnpm run prepack && pnpm run publish:npm && pnpm run postpack",
26
+ "release:minor": "npm version minor && pnpm run prepack && pnpm run publish:npm && pnpm run postpack",
27
+ "release:major": "npm version major && pnpm run prepack && pnpm run publish:npm && pnpm run postpack"
28
+ },
29
+ "dependencies": {},
30
+ "peerDependencies": {
31
+ "next-intl": "^4.0.0",
32
+ "react": "^19.0.0",
33
+ "sonner": "^2.0.0",
34
+ "zod": "^3.24.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^25.8.0",
38
+ "@types/react": "^19.2.14",
39
+ "next-intl": "^4.0.0",
40
+ "react": "^19.2.6",
41
+ "sonner": "^2.0.7",
42
+ "typescript": "^6.0.3",
43
+ "zod": "^3.24.0"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "files": [
49
+ "dist"
50
+ ],
51
+ "license": "MIT",
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "https://github.com/dofe-ai/sso.dofe.ai.git"
55
+ },
56
+ "author": "Techwu <wumin.itea@gmail.com>"
57
+ }