@goplusvn/core 0.1.44 → 0.1.46

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.
@@ -1,134 +0,0 @@
1
- // @goerp/core/rbac/permissions
2
- // Server-only permission utilities - no client/UI imports
3
- // Use this in Server Components and API routes to avoid bundling client code
4
-
5
- import type { Permission, Session } from "../types";
6
-
7
- // ============================================================================
8
- // Action Mapping
9
- // ============================================================================
10
-
11
- export const CRUD_ACTIONS = {
12
- create: "create",
13
- view: "view",
14
- update: "update",
15
- delete: "delete",
16
- export: "export",
17
- import: "import",
18
- approve: "approve",
19
- reject: "reject",
20
- } as const;
21
-
22
- export type CrudAction = keyof typeof CRUD_ACTIONS;
23
-
24
- export function getActionCode(operation: CrudAction): string {
25
- return CRUD_ACTIONS[operation];
26
- }
27
-
28
- // ============================================================================
29
- // Permission Helpers
30
- // ============================================================================
31
-
32
- interface ExtendedUser {
33
- id: string;
34
- name?: string | null;
35
- email?: string | null;
36
- image?: string | null;
37
- roles?: string[];
38
- permissions?: Permission[];
39
- }
40
-
41
- const BYPASS_AUTH =
42
- process.env.BYPASS_AUTH === "true" || process.env.BYPASS_AUTH === "1";
43
-
44
- const ADMIN_ROLE_CODES = ["admin", "SUPER_ADMIN"];
45
-
46
- export function getCrudPermissionsFromSession(
47
- session: Session | null,
48
- entity: string,
49
- ): {
50
- create: boolean;
51
- view: boolean;
52
- update: boolean;
53
- delete: boolean;
54
- export: boolean;
55
- import: boolean;
56
- approve: boolean;
57
- reject: boolean;
58
- } {
59
- if (BYPASS_AUTH) {
60
- return {
61
- create: true,
62
- view: true,
63
- update: true,
64
- delete: true,
65
- export: true,
66
- import: true,
67
- approve: true,
68
- reject: true,
69
- };
70
- }
71
-
72
- if (!session?.user) {
73
- return {
74
- create: false,
75
- view: false,
76
- update: false,
77
- delete: false,
78
- export: false,
79
- import: false,
80
- approve: false,
81
- reject: false,
82
- };
83
- }
84
-
85
- const user = session.user as ExtendedUser;
86
-
87
- if (!user.id) {
88
- return {
89
- create: false,
90
- view: false,
91
- update: false,
92
- delete: false,
93
- export: false,
94
- import: false,
95
- approve: false,
96
- reject: false,
97
- };
98
- }
99
-
100
- const isAdmin = user.roles?.some((role) => ADMIN_ROLE_CODES.includes(role));
101
- if (isAdmin) {
102
- return {
103
- create: true,
104
- view: true,
105
- update: true,
106
- delete: true,
107
- export: true,
108
- import: true,
109
- approve: true,
110
- reject: true,
111
- };
112
- }
113
-
114
- const permissions = user.permissions || [];
115
- const permissionKeys = new Set(
116
- permissions.map((p) => `${p.resourceCode}:${p.actionCode}`),
117
- );
118
-
119
- const hasPermission = (action: string) => {
120
- const key = `${entity}:${action}`;
121
- return permissionKeys.has(key);
122
- };
123
-
124
- return {
125
- create: hasPermission(getActionCode("create")),
126
- view: hasPermission(getActionCode("view")),
127
- update: hasPermission(getActionCode("update")),
128
- delete: hasPermission(getActionCode("delete")),
129
- export: hasPermission(getActionCode("export")),
130
- import: hasPermission(getActionCode("import")),
131
- approve: hasPermission(getActionCode("approve")),
132
- reject: hasPermission(getActionCode("reject")),
133
- };
134
- }