1 import { getCallerFilePath, isFileDisabled, MEDUSA_SKIP_FILE } from "../common"
2 import { toSnakeCase } from "../common/to-snake-case"
3
4 export const MedusaPolicySymbol = Symbol.for("MedusaPolicy")
5
6 /**
7 * The single character used as the RBAC wildcard across both resource and
8 * operation slots. All RBAC code that needs to read, write, or compare the
9 * wildcard should import this constant.
10 */
11 export const WILDCARD = "*"
12
13 export interface PolicyDefinition {
14 name: string
15 resource: string
16 operation: string
17 description?: string
18 }
19
20 export interface definePoliciesExport {
21 [MedusaPolicySymbol]: boolean
22 policies: PolicyDefinition[]
23 }
24
25 // This will be overridden by the actual interface when medusa types are loaded
26 type DefaultPolicyResources = Record<string, string>
27
28 /**
29 * Global registry for all unique resources.
30 */
31 const PolicyResource: DefaultPolicyResources & Record<string, string> =
32 global.PolicyResource ?? {}
33
34 global.PolicyResource ??= PolicyResource
35
36 /**
37 * Global registry for all unique operations.
38 */
39 const defaultOperations = ["read", "create", "update", "delete", WILDCARD]
40
41 const PolicyOperation: Record<string, string> & {
42 readonly read: "read"
43 readonly create: "create"
44 readonly update: "update"
45 readonly delete: "delete"
46 readonly "*": "*"
47 readonly ALL: "*"
48 } = global.PolicyOperation ?? { ALL: WILDCARD }
49
50 const normalizeKey = (element: string) => {
51 return element === WILDCARD ? WILDCARD : toSnakeCase(element)
52 }
53
54 for (const operation of defaultOperations) {
55 const operationKey = normalizeKey(operation)
56 PolicyOperation[operationKey] = operation
57 }
58
59 global.PolicyOperation ??= PolicyOperation
60
61 const Policy: Record<
62 string,
63 { resource: string; operation: string; description?: string }
64 > = global.Policy ?? {}
65
66 global.Policy ??= Policy
67
68 /**
69 * Define RBAC policies that will be automatically synced to the database
70 * when the application starts.
71 *
72 * @param policies - Single policy or array of policy definitions
73 *
74 * @example
75 * ```ts
76 * definePolicies({
77 * name: "ReadBrands",
78 * resource: "brand",
79 * operation: "read"
80 * description: "Read brands"
81 * })
82 *
83 * definePolicies([
84 * {
85 * name: "ReadBrands",
86 * resource: "brand",
87 * operation: "read"
88 * },
89 * {
90 * name: "CreateBrands",
91 * resource: "brand",
92 * operation: "create"
93 * }
94 * ])
95 * ```
96 */
97 export function definePolicies(
98 policies: PolicyDefinition | PolicyDefinition[]
99 ): definePoliciesExport {
100 const callerFilePath = getCallerFilePath()
101 if (isFileDisabled(callerFilePath ?? "")) {
102 return { [MEDUSA_SKIP_FILE]: true } as any
103 }
104
105 const policiesArray = Array.isArray(policies) ? policies : [policies]
106
107 for (const policy of policiesArray) {
108 if (!policy.name || !policy.resource || !policy.operation) {
109 throw new Error(
110 `Policy definition must include name, resource, and operation. Received: ${JSON.stringify(
111 policy,
112 null,
113 2
114 )}`
115 )
116 }
117 }
118
119 for (const policy of policiesArray) {
120 const resourceKey = normalizeKey(policy.resource)
121 const operationKey = normalizeKey(policy.operation)
122
123 policy.resource = resourceKey
124 policy.operation = operationKey
125
126 PolicyResource[resourceKey] = policy.resource
127
128 PolicyOperation[operationKey] = policy.operation
129
130 // Register in Policy object with name as key
131 Policy[policy.name] = { ...policy }
132 }
133
134 const output: definePoliciesExport = {
135 [MedusaPolicySymbol]: true,
136 policies: policiesArray,
137 }
138
139 return output
140 }
141
142 export { Policy, PolicyOperation, PolicyResource }