role-permission-engine 1.0.0
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.
- package/CHANGELOG.md +139 -0
- package/LICENSE +21 -0
- package/README.md +682 -0
- package/dist/index.cjs.js +2558 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +2548 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/utils.cjs.js +389 -0
- package/dist/utils.cjs.js.map +1 -0
- package/dist/utils.esm.js +385 -0
- package/dist/utils.esm.js.map +1 -0
- package/package.json +106 -0
- package/types/index.d.ts +229 -0
- package/types/utils.d.ts +40 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type declarations for `role-permission-engine`.
|
|
3
|
+
*
|
|
4
|
+
* These types mirror the JSDoc annotations in the source files and provide
|
|
5
|
+
* full IntelliSense support for TypeScript consumers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as React from 'react';
|
|
9
|
+
|
|
10
|
+
// ─── Primitives ───────────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
/** A user role string, e.g. `"admin"`, `"editor"`. */
|
|
13
|
+
export type Role = string;
|
|
14
|
+
|
|
15
|
+
/** A permission string, e.g. `"read:users"`, `"write:posts"`, `"*"`. */
|
|
16
|
+
export type Permission = string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Logical operator for multi-value checks.
|
|
20
|
+
* - `"any"` — OR logic (at least one match required).
|
|
21
|
+
* - `"all"` — AND logic (every item must match).
|
|
22
|
+
*/
|
|
23
|
+
export type LogicOperator = 'any' | 'all';
|
|
24
|
+
|
|
25
|
+
/** Result returned by permission utility functions. */
|
|
26
|
+
export interface PermissionResult {
|
|
27
|
+
/** Whether the user is allowed. */
|
|
28
|
+
allowed: boolean;
|
|
29
|
+
/** Human-readable explanation. */
|
|
30
|
+
reason: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ─── Utility Functions ────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Checks whether the user's roles satisfy the required roles.
|
|
37
|
+
*
|
|
38
|
+
* @param userRoles - The roles currently held by the user.
|
|
39
|
+
* @param requiredRoles - The roles required to pass the check.
|
|
40
|
+
* @param logic - `"any"` (OR) or `"all"` (AND). Defaults to `"any"`.
|
|
41
|
+
*/
|
|
42
|
+
export function hasRole(
|
|
43
|
+
userRoles: Role[],
|
|
44
|
+
requiredRoles: Role[],
|
|
45
|
+
logic?: LogicOperator
|
|
46
|
+
): PermissionResult;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks whether the user's permissions satisfy the required permissions.
|
|
50
|
+
* Supports wildcard `"*"` and namespace wildcards like `"read:*"`.
|
|
51
|
+
*
|
|
52
|
+
* @param userPermissions - The permissions currently held by the user.
|
|
53
|
+
* @param requiredPermissions - The permissions required to pass the check.
|
|
54
|
+
* @param logic - `"any"` (OR) or `"all"` (AND). Defaults to `"any"`.
|
|
55
|
+
*/
|
|
56
|
+
export function hasPermission(
|
|
57
|
+
userPermissions: Permission[],
|
|
58
|
+
requiredPermissions: Permission[],
|
|
59
|
+
logic?: LogicOperator
|
|
60
|
+
): PermissionResult;
|
|
61
|
+
|
|
62
|
+
/** Options for `checkAccess`. */
|
|
63
|
+
export interface CheckAccessOptions {
|
|
64
|
+
userRoles?: Role[];
|
|
65
|
+
userPermissions?: Permission[];
|
|
66
|
+
requiredRoles?: Role[];
|
|
67
|
+
requiredPermissions?: Permission[];
|
|
68
|
+
roleLogic?: LogicOperator;
|
|
69
|
+
permissionLogic?: LogicOperator;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Combined role + permission access check.
|
|
74
|
+
* Both role and permission constraints must pass for `allowed` to be `true`.
|
|
75
|
+
*/
|
|
76
|
+
export function checkAccess(options: CheckAccessOptions): PermissionResult;
|
|
77
|
+
|
|
78
|
+
// ─── Context ──────────────────────────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
/** The shape of the value stored in the permission context. */
|
|
81
|
+
export interface PermissionContextValue {
|
|
82
|
+
roles: Role[];
|
|
83
|
+
permissions: Permission[];
|
|
84
|
+
user: Record<string, unknown> | null;
|
|
85
|
+
isAuthenticated: boolean;
|
|
86
|
+
isLoading: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Props for `PermissionProvider`. */
|
|
90
|
+
export interface PermissionProviderProps {
|
|
91
|
+
roles?: Role[];
|
|
92
|
+
permissions?: Permission[];
|
|
93
|
+
user?: Record<string, unknown> | null;
|
|
94
|
+
isAuthenticated?: boolean;
|
|
95
|
+
isLoading?: boolean;
|
|
96
|
+
children: React.ReactNode;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Provides permission state to all child components.
|
|
101
|
+
* Wrap your app root (or a sub-tree) with this provider.
|
|
102
|
+
*/
|
|
103
|
+
export const PermissionProvider: React.FC<PermissionProviderProps>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns the current permission context value.
|
|
107
|
+
* Must be used inside a `<PermissionProvider>`.
|
|
108
|
+
*/
|
|
109
|
+
export function usePermissionContext(): PermissionContextValue;
|
|
110
|
+
|
|
111
|
+
// ─── Hooks ────────────────────────────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
/** Options for `usePermission`. */
|
|
114
|
+
export interface UsePermissionOptions {
|
|
115
|
+
roles?: Role[];
|
|
116
|
+
permissions?: Permission[];
|
|
117
|
+
roleLogic?: LogicOperator;
|
|
118
|
+
permissionLogic?: LogicOperator;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Value returned by `usePermission`. */
|
|
122
|
+
export interface UsePermissionResult {
|
|
123
|
+
/** Whether the user is allowed access. */
|
|
124
|
+
allowed: boolean;
|
|
125
|
+
/** Convenience inverse of `allowed`. */
|
|
126
|
+
denied: boolean;
|
|
127
|
+
/** Whether the auth state is still loading. */
|
|
128
|
+
isLoading: boolean;
|
|
129
|
+
/** Whether the user is authenticated. */
|
|
130
|
+
isAuthenticated: boolean;
|
|
131
|
+
/** Human-readable explanation of the result. */
|
|
132
|
+
reason: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Hook for programmatic permission checking.
|
|
137
|
+
* Reads from the nearest `PermissionProvider`.
|
|
138
|
+
*/
|
|
139
|
+
export function usePermission(options?: UsePermissionOptions): UsePermissionResult;
|
|
140
|
+
|
|
141
|
+
// ─── Components ───────────────────────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
/** Props for `BlockRoute`. */
|
|
144
|
+
export interface BlockRouteProps {
|
|
145
|
+
children: React.ReactNode;
|
|
146
|
+
roles?: Role[];
|
|
147
|
+
permissions?: Permission[];
|
|
148
|
+
roleLogic?: LogicOperator;
|
|
149
|
+
permissionLogic?: LogicOperator;
|
|
150
|
+
/** Path to redirect to when access is denied. Defaults to `"/unauthorized"`. */
|
|
151
|
+
redirectTo?: string;
|
|
152
|
+
/** Component to render while auth state is loading. */
|
|
153
|
+
loadingComponent?: React.ReactNode;
|
|
154
|
+
/** Replace the history entry on redirect. Defaults to `true`. */
|
|
155
|
+
replace?: boolean;
|
|
156
|
+
/** State to pass to the redirect location. */
|
|
157
|
+
state?: unknown;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Route guard component. Redirects unauthorized users to `redirectTo`.
|
|
162
|
+
* Compatible with React Router v5 and v6.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* // React Router v6
|
|
166
|
+
* <Route path="/admin" element={<BlockRoute roles={['admin']}><AdminPage /></BlockRoute>} />
|
|
167
|
+
*/
|
|
168
|
+
export const BlockRoute: React.FC<BlockRouteProps>;
|
|
169
|
+
|
|
170
|
+
/** Props for `PermissionGate`. */
|
|
171
|
+
export interface PermissionGateProps {
|
|
172
|
+
children: React.ReactNode;
|
|
173
|
+
roles?: Role[];
|
|
174
|
+
permissions?: Permission[];
|
|
175
|
+
roleLogic?: LogicOperator;
|
|
176
|
+
permissionLogic?: LogicOperator;
|
|
177
|
+
/** Content to render when the user is NOT authorized. */
|
|
178
|
+
fallback?: React.ReactNode;
|
|
179
|
+
/** Content to render while auth state is loading. */
|
|
180
|
+
loadingComponent?: React.ReactNode;
|
|
181
|
+
/**
|
|
182
|
+
* When `true`, inverts the check — renders `children` when the user
|
|
183
|
+
* does NOT have the roles/permissions.
|
|
184
|
+
*/
|
|
185
|
+
negate?: boolean;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Inline conditional rendering based on roles/permissions.
|
|
190
|
+
* Unlike `BlockRoute`, this never redirects — it shows or hides content.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* <PermissionGate roles={['admin']} fallback={<p>No access</p>}>
|
|
194
|
+
* <DeleteButton />
|
|
195
|
+
* </PermissionGate>
|
|
196
|
+
*/
|
|
197
|
+
export const PermissionGate: React.FC<PermissionGateProps>;
|
|
198
|
+
|
|
199
|
+
/** Options accepted by `withPermission`. */
|
|
200
|
+
export interface WithPermissionOptions {
|
|
201
|
+
roles?: Role[];
|
|
202
|
+
permissions?: Permission[];
|
|
203
|
+
roleLogic?: LogicOperator;
|
|
204
|
+
permissionLogic?: LogicOperator;
|
|
205
|
+
/** Content to render when access is denied. Defaults to `null`. */
|
|
206
|
+
fallback?: React.ReactNode;
|
|
207
|
+
/** Content to render while auth state is loading. Defaults to `null`. */
|
|
208
|
+
loadingComponent?: React.ReactNode;
|
|
209
|
+
/**
|
|
210
|
+
* When `true`, inverts the check — renders the component when the user
|
|
211
|
+
* does NOT have the roles/permissions. Defaults to `false`.
|
|
212
|
+
*/
|
|
213
|
+
negate?: boolean;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Higher Order Component that wraps a component with a permission check.
|
|
218
|
+
* The wrapped component is only rendered when the current user passes the
|
|
219
|
+
* role and permission requirements.
|
|
220
|
+
*
|
|
221
|
+
* @param WrappedComponent - The component to protect.
|
|
222
|
+
* @param options - Access control options.
|
|
223
|
+
* @returns A new component wrapped with the permission gate.
|
|
224
|
+
*/
|
|
225
|
+
export function withPermission<P extends object>(
|
|
226
|
+
WrappedComponent: React.ComponentType<P>,
|
|
227
|
+
options?: WithPermissionOptions
|
|
228
|
+
): React.FC<P>;
|
|
229
|
+
|
package/types/utils.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type declarations for the backend utility subpath of `role-permission-engine`.
|
|
3
|
+
*
|
|
4
|
+
* Provides type support for consumers importing from `role-permission-engine/utils`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Role, Permission, LogicOperator, PermissionResult, CheckAccessOptions } from './index';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Checks whether the user's roles satisfy the required roles.
|
|
11
|
+
*
|
|
12
|
+
* @param userRoles - The roles currently held by the user.
|
|
13
|
+
* @param requiredRoles - The roles required to pass the check.
|
|
14
|
+
* @param logic - `"any"` (OR) or `"all"` (AND). Defaults to `"any"`.
|
|
15
|
+
*/
|
|
16
|
+
export function hasRole(
|
|
17
|
+
userRoles: Role[],
|
|
18
|
+
requiredRoles: Role[],
|
|
19
|
+
logic?: LogicOperator
|
|
20
|
+
): PermissionResult;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Checks whether the user's permissions satisfy the required permissions.
|
|
24
|
+
* Supports wildcard `"*"` and namespace wildcards like `"read:*"`.
|
|
25
|
+
*
|
|
26
|
+
* @param userPermissions - The permissions currently held by the user.
|
|
27
|
+
* @param requiredPermissions - The permissions required to pass the check.
|
|
28
|
+
* @param logic - `"any"` (OR) or `"all"` (AND). Defaults to `"any"`.
|
|
29
|
+
*/
|
|
30
|
+
export function hasPermission(
|
|
31
|
+
userPermissions: Permission[],
|
|
32
|
+
requiredPermissions: Permission[],
|
|
33
|
+
logic?: LogicOperator
|
|
34
|
+
): PermissionResult;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Combined role + permission access check.
|
|
38
|
+
* Both role and permission constraints must pass for `allowed` to be `true`.
|
|
39
|
+
*/
|
|
40
|
+
export function checkAccess(options: CheckAccessOptions): PermissionResult;
|