@object-ui/permissions 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ObjectQL
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import type { PermissionAction, PermissionCheckResult, FieldLevelPermission } from '@object-ui/types';
9
+ export interface PermissionContextValue {
10
+ /** Check if action is allowed on object */
11
+ check: (object: string, action: PermissionAction, record?: Record<string, unknown>) => PermissionCheckResult;
12
+ /** Check field-level permissions */
13
+ checkField: (object: string, field: string, action: 'read' | 'write') => boolean;
14
+ /** Get field permissions for an object */
15
+ getFieldPermissions: (object: string) => FieldLevelPermission[];
16
+ /** Get row filter for an object */
17
+ getRowFilter: (object: string) => string | undefined;
18
+ /** Current user roles */
19
+ roles: string[];
20
+ /** Whether permissions are loaded */
21
+ isLoaded: boolean;
22
+ }
23
+ export declare const PermCtx: import("react").Context<PermissionContextValue | null>;
24
+ //# sourceMappingURL=PermissionContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PermissionContext.d.ts","sourceRoot":"","sources":["../src/PermissionContext.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAEtG,MAAM,WAAW,sBAAsB;IACrC,2CAA2C;IAC3C,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,qBAAqB,CAAC;IAC7G,oCAAoC;IACpC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,KAAK,OAAO,CAAC;IACjF,0CAA0C;IAC1C,mBAAmB,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,oBAAoB,EAAE,CAAC;IAChE,mCAAmC;IACnC,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IACrD,yBAAyB;IACzB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,qCAAqC;IACrC,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,eAAO,MAAM,OAAO,wDAAqD,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { createContext } from 'react';
9
+ export const PermCtx = createContext(null);
10
+ PermCtx.displayName = 'PermissionContext';
@@ -0,0 +1,27 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import React from 'react';
9
+ import type { PermissionAction } from '@object-ui/types';
10
+ export interface PermissionGuardProps {
11
+ /** Target object name */
12
+ object: string;
13
+ /** Required action */
14
+ action: PermissionAction;
15
+ /** Fallback behavior when denied */
16
+ fallback?: 'hide' | 'disable' | 'custom';
17
+ /** Custom fallback content */
18
+ fallbackContent?: React.ReactNode;
19
+ /** Children to render when permitted */
20
+ children: React.ReactNode;
21
+ }
22
+ /**
23
+ * Guard component that conditionally renders children based on permissions.
24
+ * Hides, disables, or shows custom content when access is denied.
25
+ */
26
+ export declare function PermissionGuard({ object, action, fallback, fallbackContent, children, }: PermissionGuardProps): import("react/jsx-runtime").JSX.Element | null;
27
+ //# sourceMappingURL=PermissionGuard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PermissionGuard.d.ts","sourceRoot":"","sources":["../src/PermissionGuard.tsx"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAGzD,MAAM,WAAW,oBAAoB;IACnC,yBAAyB;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,sBAAsB;IACtB,MAAM,EAAE,gBAAgB,CAAC;IACzB,oCAAoC;IACpC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;IACzC,8BAA8B;IAC9B,eAAe,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAClC,wCAAwC;IACxC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,EAC9B,MAAM,EACN,MAAM,EACN,QAAiB,EACjB,eAAe,EACf,QAAQ,GACT,EAAE,oBAAoB,kDAuBtB"}
@@ -0,0 +1,23 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
+ import { usePermissions } from './usePermissions';
3
+ /**
4
+ * Guard component that conditionally renders children based on permissions.
5
+ * Hides, disables, or shows custom content when access is denied.
6
+ */
7
+ export function PermissionGuard({ object, action, fallback = 'hide', fallbackContent, children, }) {
8
+ const { can } = usePermissions();
9
+ const isAllowed = can(object, action);
10
+ if (isAllowed) {
11
+ return _jsx(_Fragment, { children: children });
12
+ }
13
+ switch (fallback) {
14
+ case 'hide':
15
+ return null;
16
+ case 'disable':
17
+ return (_jsx("div", { style: { opacity: 0.5, pointerEvents: 'none' }, "aria-disabled": "true", children: children }));
18
+ case 'custom':
19
+ return _jsx(_Fragment, { children: fallbackContent ?? null });
20
+ default:
21
+ return null;
22
+ }
23
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import React from 'react';
9
+ import type { RoleDefinition, ObjectPermissionConfig } from '@object-ui/types';
10
+ export interface PermissionProviderProps {
11
+ /** Role definitions */
12
+ roles: RoleDefinition[];
13
+ /** Object permission configurations */
14
+ permissions: ObjectPermissionConfig[];
15
+ /** Current user's role names */
16
+ userRoles: string[];
17
+ /** Current user context */
18
+ user?: {
19
+ id: string;
20
+ [key: string]: unknown;
21
+ };
22
+ /** Children */
23
+ children: React.ReactNode;
24
+ }
25
+ export declare function PermissionProvider({ roles, permissions, userRoles, user, children, }: PermissionProviderProps): import("react/jsx-runtime").JSX.Element;
26
+ //# sourceMappingURL=PermissionProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PermissionProvider.d.ts","sourceRoot":"","sources":["../src/PermissionProvider.tsx"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAA+B,MAAM,OAAO,CAAC;AACpD,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EAIvB,MAAM,kBAAkB,CAAC;AAI1B,MAAM,WAAW,uBAAuB;IACtC,uBAAuB;IACvB,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,uCAAuC;IACvC,WAAW,EAAE,sBAAsB,EAAE,CAAC;IACtC,gCAAgC;IAChC,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,2BAA2B;IAC3B,IAAI,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC9C,eAAe;IACf,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED,wBAAgB,kBAAkB,CAAC,EACjC,KAAK,EACL,WAAW,EACX,SAAS,EACT,IAAI,EACJ,QAAQ,GACT,EAAE,uBAAuB,2CA0FzB"}
@@ -0,0 +1,80 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * ObjectUI
4
+ * Copyright (c) 2024-present ObjectStack Inc.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ import { useMemo, useCallback } from 'react';
10
+ import { PermCtx } from './PermissionContext';
11
+ import { evaluatePermission } from './evaluator';
12
+ export function PermissionProvider({ roles, permissions, userRoles, user, children, }) {
13
+ const check = useCallback((object, action, record) => {
14
+ return evaluatePermission({
15
+ roles,
16
+ permissions,
17
+ userRoles,
18
+ user: user ? { ...user, roles: userRoles } : { id: '', roles: userRoles },
19
+ object,
20
+ action,
21
+ record,
22
+ });
23
+ }, [roles, permissions, userRoles, user]);
24
+ const checkField = useCallback((object, field, action) => {
25
+ const objectConfig = permissions.find((p) => p.object === object);
26
+ if (!objectConfig)
27
+ return true; // No config means no restrictions
28
+ for (const role of userRoles) {
29
+ const roleConfig = objectConfig.roles[role];
30
+ if (roleConfig?.fieldPermissions) {
31
+ const fieldPerm = roleConfig.fieldPermissions.find((fp) => fp.field === field);
32
+ if (fieldPerm) {
33
+ return action === 'read' ? fieldPerm.read !== false : fieldPerm.write !== false;
34
+ }
35
+ }
36
+ }
37
+ // Check defaults
38
+ if (objectConfig.fieldDefaults) {
39
+ const defaultPerm = objectConfig.fieldDefaults.find((fp) => fp.field === field);
40
+ if (defaultPerm) {
41
+ return action === 'read' ? defaultPerm.read !== false : defaultPerm.write !== false;
42
+ }
43
+ }
44
+ return true; // Default allow
45
+ }, [permissions, userRoles]);
46
+ const getFieldPermissions = useCallback((object) => {
47
+ const objectConfig = permissions.find((p) => p.object === object);
48
+ if (!objectConfig)
49
+ return [];
50
+ const fieldPerms = [];
51
+ for (const role of userRoles) {
52
+ const roleConfig = objectConfig.roles[role];
53
+ if (roleConfig?.fieldPermissions) {
54
+ fieldPerms.push(...roleConfig.fieldPermissions);
55
+ }
56
+ }
57
+ return fieldPerms;
58
+ }, [permissions, userRoles]);
59
+ const getRowFilter = useCallback((object) => {
60
+ const objectConfig = permissions.find((p) => p.object === object);
61
+ if (!objectConfig)
62
+ return undefined;
63
+ for (const role of userRoles) {
64
+ const roleConfig = objectConfig.roles[role];
65
+ if (roleConfig?.rowPermissions?.length) {
66
+ return roleConfig.rowPermissions[0].filter;
67
+ }
68
+ }
69
+ return undefined;
70
+ }, [permissions, userRoles]);
71
+ const value = useMemo(() => ({
72
+ check,
73
+ checkField,
74
+ getFieldPermissions,
75
+ getRowFilter,
76
+ roles: userRoles,
77
+ isLoaded: true,
78
+ }), [check, checkField, getFieldPermissions, getRowFilter, userRoles]);
79
+ return _jsx(PermCtx.Provider, { value: value, children: children });
80
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import type { RoleDefinition, ObjectPermissionConfig, PermissionAction, PermissionCheckResult, PermissionCondition } from '@object-ui/types';
9
+ interface EvaluatePermissionParams {
10
+ roles: RoleDefinition[];
11
+ permissions: ObjectPermissionConfig[];
12
+ userRoles: string[];
13
+ user: {
14
+ id: string;
15
+ roles: string[];
16
+ [key: string]: unknown;
17
+ };
18
+ object: string;
19
+ action: PermissionAction;
20
+ record?: Record<string, unknown>;
21
+ field?: string;
22
+ }
23
+ /**
24
+ * Evaluates whether an action is permitted based on RBAC configuration.
25
+ * Supports role inheritance, field-level, and row-level permissions.
26
+ */
27
+ export declare function evaluatePermission({ roles, permissions, userRoles, object, action, record, }: EvaluatePermissionParams): PermissionCheckResult;
28
+ /**
29
+ * Evaluates a permission condition against a record.
30
+ */
31
+ export declare function evaluateCondition(condition: PermissionCondition, record: Record<string, unknown>): boolean;
32
+ export {};
33
+ //# sourceMappingURL=evaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluator.d.ts","sourceRoot":"","sources":["../src/evaluator.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAE1B,UAAU,wBAAwB;IAChC,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,WAAW,EAAE,sBAAsB,EAAE,CAAC;IACtC,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,IAAI,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC9D,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,gBAAgB,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,KAAK,EACL,WAAW,EACX,SAAS,EACT,MAAM,EACN,MAAM,EACN,MAAM,GACP,EAAE,wBAAwB,GAAG,qBAAqB,CA0ClD;AAyBD;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,mBAAmB,EAC9B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,OAAO,CAkCT"}
@@ -0,0 +1,103 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ /**
9
+ * Evaluates whether an action is permitted based on RBAC configuration.
10
+ * Supports role inheritance, field-level, and row-level permissions.
11
+ */
12
+ export function evaluatePermission({ roles, permissions, userRoles, object, action, record, }) {
13
+ const objectConfig = permissions.find((p) => p.object === object);
14
+ // If no config exists for the object, allow by default
15
+ if (!objectConfig) {
16
+ return { allowed: true };
17
+ }
18
+ // Check public access
19
+ if (objectConfig.publicAccess?.includes(action)) {
20
+ return { allowed: true };
21
+ }
22
+ // Resolve all effective roles (including inherited)
23
+ const effectiveRoles = resolveRoles(userRoles, roles);
24
+ // Check role-based permissions
25
+ for (const roleName of effectiveRoles) {
26
+ const roleConfig = objectConfig.roles[roleName];
27
+ if (!roleConfig)
28
+ continue;
29
+ if (roleConfig.actions.includes(action)) {
30
+ // Check row-level permissions if record is provided
31
+ if (record && roleConfig.rowPermissions?.length) {
32
+ const rowAllowed = roleConfig.rowPermissions.some((rp) => rp.actions.includes(action));
33
+ if (!rowAllowed)
34
+ continue;
35
+ }
36
+ return {
37
+ allowed: true,
38
+ fieldRestrictions: roleConfig.fieldPermissions,
39
+ rowFilter: roleConfig.rowPermissions?.[0]?.filter,
40
+ };
41
+ }
42
+ }
43
+ return {
44
+ allowed: false,
45
+ reason: `Action '${action}' on '${object}' is not permitted for roles: ${userRoles.join(', ')}`,
46
+ };
47
+ }
48
+ /**
49
+ * Resolves all effective roles including inherited roles.
50
+ */
51
+ function resolveRoles(userRoles, roleDefinitions) {
52
+ const resolved = new Set(userRoles);
53
+ const queue = [...userRoles];
54
+ while (queue.length > 0) {
55
+ const current = queue.shift();
56
+ const roleDef = roleDefinitions.find((r) => r.name === current);
57
+ if (roleDef?.inherits) {
58
+ for (const parent of roleDef.inherits) {
59
+ if (!resolved.has(parent)) {
60
+ resolved.add(parent);
61
+ queue.push(parent);
62
+ }
63
+ }
64
+ }
65
+ }
66
+ return Array.from(resolved);
67
+ }
68
+ /**
69
+ * Evaluates a permission condition against a record.
70
+ */
71
+ export function evaluateCondition(condition, record) {
72
+ // Prevent prototype pollution via dangerous property access
73
+ if (['__proto__', 'constructor', 'prototype'].includes(condition.field)) {
74
+ return false;
75
+ }
76
+ const value = Object.prototype.hasOwnProperty.call(record, condition.field) ? record[condition.field] : undefined;
77
+ switch (condition.operator) {
78
+ case 'eq':
79
+ return value === condition.value;
80
+ case 'neq':
81
+ return value !== condition.value;
82
+ case 'gt':
83
+ return typeof value === 'number' && typeof condition.value === 'number' && value > condition.value;
84
+ case 'gte':
85
+ return typeof value === 'number' && typeof condition.value === 'number' && value >= condition.value;
86
+ case 'lt':
87
+ return typeof value === 'number' && typeof condition.value === 'number' && value < condition.value;
88
+ case 'lte':
89
+ return typeof value === 'number' && typeof condition.value === 'number' && value <= condition.value;
90
+ case 'in':
91
+ return Array.isArray(condition.value) && condition.value.includes(value);
92
+ case 'not_in':
93
+ return Array.isArray(condition.value) && !condition.value.includes(value);
94
+ case 'contains':
95
+ return typeof value === 'string' && typeof condition.value === 'string' && value.includes(condition.value);
96
+ case 'is_null':
97
+ return value === null || value === undefined;
98
+ case 'is_not_null':
99
+ return value !== null && value !== undefined;
100
+ default:
101
+ return false;
102
+ }
103
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ /**
9
+ * @object-ui/permissions
10
+ *
11
+ * RBAC permission system for Object UI providing:
12
+ * - PermissionProvider context for React apps
13
+ * - usePermissions hook for checking access
14
+ * - PermissionGuard component for conditional rendering
15
+ * - Permission evaluation engine
16
+ * - Field-level and row-level permission support
17
+ *
18
+ * @packageDocumentation
19
+ */
20
+ export { PermissionProvider, type PermissionProviderProps } from './PermissionProvider';
21
+ export { usePermissions } from './usePermissions';
22
+ export { useFieldPermissions } from './useFieldPermissions';
23
+ export { PermissionGuard, type PermissionGuardProps } from './PermissionGuard';
24
+ export { evaluatePermission } from './evaluator';
25
+ export { createPermissionStore, type PermissionStore } from './store';
26
+ export type { PermissionAction, PermissionEffect, RoleDefinition, ObjectLevelPermission, FieldLevelPermission, RowLevelPermission, PermissionCondition, ObjectPermissionConfig, SharingRuleConfig, PermissionCheckResult, PermissionContext, PermissionGuardConfig, } from '@object-ui/types';
27
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,kBAAkB,EAAE,KAAK,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAC;AAGtE,YAAY,EACV,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ /**
9
+ * @object-ui/permissions
10
+ *
11
+ * RBAC permission system for Object UI providing:
12
+ * - PermissionProvider context for React apps
13
+ * - usePermissions hook for checking access
14
+ * - PermissionGuard component for conditional rendering
15
+ * - Permission evaluation engine
16
+ * - Field-level and row-level permission support
17
+ *
18
+ * @packageDocumentation
19
+ */
20
+ export { PermissionProvider } from './PermissionProvider';
21
+ export { usePermissions } from './usePermissions';
22
+ export { useFieldPermissions } from './useFieldPermissions';
23
+ export { PermissionGuard } from './PermissionGuard';
24
+ export { evaluatePermission } from './evaluator';
25
+ export { createPermissionStore } from './store';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import type { RoleDefinition, ObjectPermissionConfig, PermissionAction, PermissionCheckResult } from '@object-ui/types';
9
+ /**
10
+ * Permission store for non-React contexts (e.g., API handlers, middleware).
11
+ * Provides the same permission evaluation as the React hooks.
12
+ */
13
+ export interface PermissionStore {
14
+ /** Check if action is allowed */
15
+ check: (object: string, action: PermissionAction, record?: Record<string, unknown>) => PermissionCheckResult;
16
+ /** Update user roles */
17
+ setUserRoles: (roles: string[]) => void;
18
+ /** Update permissions configuration */
19
+ setPermissions: (permissions: ObjectPermissionConfig[]) => void;
20
+ }
21
+ /**
22
+ * Creates a permission store for non-React contexts.
23
+ */
24
+ export declare function createPermissionStore(config: {
25
+ roles: RoleDefinition[];
26
+ permissions: ObjectPermissionConfig[];
27
+ userRoles: string[];
28
+ user?: {
29
+ id: string;
30
+ [key: string]: unknown;
31
+ };
32
+ }): PermissionStore;
33
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,kBAAkB,CAAC;AAG1B;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,iCAAiC;IACjC,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,qBAAqB,CAAC;IAC7G,wBAAwB;IACxB,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IACxC,uCAAuC;IACvC,cAAc,EAAE,CAAC,WAAW,EAAE,sBAAsB,EAAE,KAAK,IAAI,CAAC;CACjE;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC5C,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,WAAW,EAAE,sBAAsB,EAAE,CAAC;IACtC,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,IAAI,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;CAC/C,GAAG,eAAe,CAsBlB"}
package/dist/store.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { evaluatePermission } from './evaluator';
9
+ /**
10
+ * Creates a permission store for non-React contexts.
11
+ */
12
+ export function createPermissionStore(config) {
13
+ const { roles, user } = config;
14
+ let { permissions, userRoles } = config;
15
+ return {
16
+ check: (object, action, record) => evaluatePermission({
17
+ roles,
18
+ permissions,
19
+ userRoles,
20
+ user: user ? { ...user, roles: userRoles } : { id: '', roles: userRoles },
21
+ object,
22
+ action,
23
+ record,
24
+ }),
25
+ setUserRoles: (newRoles) => {
26
+ userRoles = newRoles;
27
+ },
28
+ setPermissions: (newPermissions) => {
29
+ permissions = newPermissions;
30
+ },
31
+ };
32
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ /**
9
+ * Hook to get field-level permissions for a specific object.
10
+ * Returns utilities for checking individual field access.
11
+ */
12
+ export declare function useFieldPermissions(objectName: string): {
13
+ /** All field permissions for this object */
14
+ permissions: import("@object-ui/types").FieldLevelPermission[];
15
+ /** Check if a field is readable */
16
+ canRead: (field: string) => boolean;
17
+ /** Check if a field is writable */
18
+ canWrite: (field: string) => boolean;
19
+ /** Get fields that are readable */
20
+ readableFields: (fields: string[]) => string[];
21
+ /** Get fields that are writable */
22
+ writableFields: (fields: string[]) => string[];
23
+ };
24
+ //# sourceMappingURL=useFieldPermissions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFieldPermissions.d.ts","sourceRoot":"","sources":["../src/useFieldPermissions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM;IAUhD,4CAA4C;;IAE5C,mCAAmC;qBAClB,MAAM;IACvB,mCAAmC;sBACjB,MAAM;IACxB,mCAAmC;6BACV,MAAM,EAAE;IACjC,mCAAmC;6BACV,MAAM,EAAE;EAItC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { useMemo } from 'react';
9
+ import { usePermissions } from './usePermissions';
10
+ /**
11
+ * Hook to get field-level permissions for a specific object.
12
+ * Returns utilities for checking individual field access.
13
+ */
14
+ export function useFieldPermissions(objectName) {
15
+ const { checkField, getFieldPermissions } = usePermissions();
16
+ const fieldPermissions = useMemo(() => getFieldPermissions(objectName), [getFieldPermissions, objectName]);
17
+ return useMemo(() => ({
18
+ /** All field permissions for this object */
19
+ permissions: fieldPermissions,
20
+ /** Check if a field is readable */
21
+ canRead: (field) => checkField(objectName, field, 'read'),
22
+ /** Check if a field is writable */
23
+ canWrite: (field) => checkField(objectName, field, 'write'),
24
+ /** Get fields that are readable */
25
+ readableFields: (fields) => fields.filter((f) => checkField(objectName, f, 'read')),
26
+ /** Get fields that are writable */
27
+ writableFields: (fields) => fields.filter((f) => checkField(objectName, f, 'write')),
28
+ }), [fieldPermissions, checkField, objectName]);
29
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import type { PermissionAction } from '@object-ui/types';
9
+ import { type PermissionContextValue } from './PermissionContext';
10
+ /**
11
+ * Hook to access the permission system.
12
+ * Must be used within a PermissionProvider.
13
+ */
14
+ export declare function usePermissions(): PermissionContextValue & {
15
+ /** Convenience: check if action is allowed */
16
+ can: (object: string, action: PermissionAction) => boolean;
17
+ /** Convenience: check if action is denied */
18
+ cannot: (object: string, action: PermissionAction) => boolean;
19
+ };
20
+ //# sourceMappingURL=usePermissions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePermissions.d.ts","sourceRoot":"","sources":["../src/usePermissions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAyB,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAW,KAAK,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAE3E;;;GAGG;AACH,wBAAgB,cAAc,IAAI,sBAAsB,GAAG;IACzD,8CAA8C;IAC9C,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,KAAK,OAAO,CAAC;IAC3D,6CAA6C;IAC7C,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,KAAK,OAAO,CAAC;CAC/D,CAsBA"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { useContext } from 'react';
9
+ import { PermCtx } from './PermissionContext';
10
+ /**
11
+ * Hook to access the permission system.
12
+ * Must be used within a PermissionProvider.
13
+ */
14
+ export function usePermissions() {
15
+ const ctx = useContext(PermCtx);
16
+ if (!ctx) {
17
+ // Return a permissive default when no provider exists
18
+ return {
19
+ check: () => ({ allowed: true }),
20
+ checkField: () => true,
21
+ getFieldPermissions: () => [],
22
+ getRowFilter: () => undefined,
23
+ roles: [],
24
+ isLoaded: false,
25
+ can: () => true,
26
+ cannot: () => false,
27
+ };
28
+ }
29
+ return {
30
+ ...ctx,
31
+ can: (object, action) => ctx.check(object, action).allowed,
32
+ cannot: (object, action) => !ctx.check(object, action).allowed,
33
+ };
34
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@object-ui/permissions",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "description": "RBAC permission system for Object UI with object/field/row-level access control, permission guards, and hooks.",
7
+ "homepage": "https://www.objectui.org",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/objectstack-ai/objectui.git",
11
+ "directory": "packages/permissions"
12
+ },
13
+ "main": "./dist/index.js",
14
+ "module": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "peerDependencies": {
26
+ "react": "^18.0.0 || ^19.0.0"
27
+ },
28
+ "dependencies": {
29
+ "@object-ui/types": "0.5.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/react": "^19.2.13",
33
+ "react": "^19.1.0",
34
+ "typescript": "^5.9.3",
35
+ "vitest": "^4.0.18"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc",
39
+ "clean": "rm -rf dist",
40
+ "test": "vitest run",
41
+ "type-check": "tsc --noEmit",
42
+ "lint": "eslint ."
43
+ }
44
+ }