@docyrus/signin 0.17.0 → 0.18.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/README.md CHANGED
@@ -523,7 +523,7 @@ const {
523
523
  getAuthorizationUrl, // () => Promise<string | null> (returns URL without navigating — for Electron)
524
524
  signOut, // () => Promise<void>
525
525
  hasRole, // (role) => boolean — check if user has a role by slug or uid
526
- hasPermission, // (operation, dataSourceId?) => boolean — check ACL permission
526
+ hasPermission, // (operation, scope?) => boolean — check ACL permission (scope: data source id, {appId}/{aiToolId}, or omitted for tenant-wide)
527
527
  refreshUser, // () => Promise<void> — re-fetch user from API
528
528
  onHostNavigation, // (handler) => () => void — subscribe to host navigation messages (iframe mode)
529
529
  onHostNotification, // (handler) => () => void — subscribe to host notification messages (iframe mode)
@@ -1010,6 +1010,12 @@ function DataSourceView({ dataSourceId }: { dataSourceId: string }) {
1010
1010
  const canEdit = hasPermission('edit', dataSourceId);
1011
1011
  const canDelete = hasPermission('delete', dataSourceId);
1012
1012
 
1013
+ // Check a tenant-wide operation — no entity to scope it to
1014
+ const canUseAi = hasPermission('ai-access');
1015
+
1016
+ // Check an app / AI tool operation
1017
+ const canOpenApp = hasPermission('app_access', { appId });
1018
+
1013
1019
  // Check multiple roles
1014
1020
  if (hasRole(['editor', 'reviewer'])) {
1015
1021
  return <EditorView canEdit={canEdit} canDelete={canDelete} />;
@@ -1022,7 +1028,7 @@ function DataSourceView({ dataSourceId }: { dataSourceId: string }) {
1022
1028
  ### Via Pure Functions (Framework-Agnostic)
1023
1029
 
1024
1030
  ```tsx
1025
- import { hasRole, hasPermission } from '@docyrus/signin/core';
1031
+ import { hasRole, hasPermission, getAllowedOperations } from '@docyrus/signin/core';
1026
1032
  import type { DocyrusUser } from '@docyrus/signin/core';
1027
1033
 
1028
1034
  function checkAccess(user: DocyrusUser) {
@@ -1031,19 +1037,56 @@ function checkAccess(user: DocyrusUser) {
1031
1037
  hasRole(user, 'super_admin'); // checks slug or uid
1032
1038
  hasRole(user, ['editor', 'viewer']); // any match
1033
1039
 
1034
- // hasPermission checks: super_admin → global_editor → global_viewer → always-permitted → aclRules
1040
+ // hasPermission checks: super_admin → global_editor → global_viewer → always-permitted → rules
1035
1041
  hasPermission(user, 'view', 'some-ds-id');
1036
1042
  hasPermission(user, 'edit', 'some-ds-id');
1043
+
1044
+ // Everything the user may do within a scope
1045
+ getAllowedOperations(user, 'some-ds-id'); // ['view', 'edit', ...]
1046
+ getAllowedOperations(user); // tenant-wide: ['ai-access', 'manage_users', ...]
1037
1047
  }
1038
1048
  ```
1039
1049
 
1050
+ ### Permission Scopes
1051
+
1052
+ `hasPermission` and `getAllowedOperations` take an optional scope as their last
1053
+ positional argument. What you pass decides which ACL target type answers the
1054
+ check:
1055
+
1056
+ | Scope argument | Target type | Example operations |
1057
+ | --- | --- | --- |
1058
+ | `'ds-id'` or `{ dataSourceId }` | `data_source` | `view`, `create`, `edit`, `delete`, `export`, `print` |
1059
+ | `{ appId }` | `app` | `app_access` |
1060
+ | `{ aiToolId }` | `ai_tool` | `allow` |
1061
+ | omitted / `null` | `ai`, `settings`, `devtools` | `ai-access`, `manage_users`, `studio`, `automations` |
1062
+
1063
+ Pass `{ targetType }` alongside an id to pin the check to a single target type
1064
+ (for example `{ dataSourceId, targetType: 'field' }` to read field
1065
+ restrictions, which are never treated as grants otherwise).
1066
+
1067
+ ```tsx
1068
+ // Tenant-wide capabilities carry no entity id
1069
+ hasPermission(user, 'ai-access');
1070
+ hasPermission(user, 'manage_users');
1071
+
1072
+ // Entity-scoped, but not a data source
1073
+ hasPermission(user, 'app_access', { appId });
1074
+ hasPermission(user, 'allow', { aiToolId });
1075
+ ```
1076
+
1040
1077
  ### Permission Resolution Order
1041
1078
 
1042
1079
  1. `super_admin` role → always granted
1043
- 2. `global_editor` role → granted for: view, create, edit, delete, create_bulk, export, import, print
1044
- 3. `global_viewer` role → granted only for: view
1080
+ 2. `global_editor` role + data source scope → granted for: view, create, edit, delete, create_bulk, export, import, print
1081
+ 3. `global_viewer` role + data source scope → granted only for: view
1045
1082
  4. Always-permitted system data sources (reports, todos, notes, etc.)
1046
- 5. User's `aclRules` array (merged from all roles by the server)
1083
+ 5. The user's rules for the scope — `permissions` when the API returns it, otherwise the legacy `aclRules` array (both merged from all roles by the server)
1084
+
1085
+ > `permissions` is the target-type-aware shape (`{ targetType, dataSourceId?,
1086
+ > appId?, aiToolId?, allowedOperations }`). The older `aclRules` shape is keyed
1087
+ > by data source alone, so it collapses every entity-less rule into a single
1088
+ > null-keyed entry and cannot answer app or AI tool scopes; it is used only when
1089
+ > `permissions` is absent.
1047
1090
 
1048
1091
  ### Refreshing User Data
1049
1092
 
@@ -1,5 +1,5 @@
1
1
  import { OAuth2Tokens, RestApiClient } from '@docyrus/api-client';
2
- import { D as DocyrusAuthConfig, b as AuthMode, d as AuthStatus, m as DocyrusUser, G as GuidyRoute, R as RouteChangePayload, t as HostNavigationRequestOptions, k as DocyrusSharePayload, h as DocyrusEmailPayload, g as DocyrusCalendarEventPayload, n as MsGraphClient, H as HostNavigationHandler, s as HostNotificationHandler, i as DocyrusRole, P as PermissionConfig } from './types-CFnKr6T_.js';
2
+ import { D as DocyrusAuthConfig, c as AuthMode, e as AuthStatus, o as DocyrusUser, G as GuidyRoute, R as RouteChangePayload, w as HostNavigationRequestOptions, m as DocyrusSharePayload, i as DocyrusEmailPayload, h as DocyrusCalendarEventPayload, p as MsGraphClient, H as HostNavigationHandler, v as HostNotificationHandler, k as DocyrusRole, u as PermissionScope, P as PermissionConfig } from './types-BcLDyMdw.js';
3
3
 
4
4
  type AuthStateListener = (state: {
5
5
  status: AuthStatus;
@@ -212,16 +212,30 @@ declare function getAllRoles(user: DocyrusUser | null | undefined): DocyrusRole[
212
212
  */
213
213
  declare function hasRole(user: DocyrusUser | null | undefined, role: string | string[] | null | undefined): boolean;
214
214
  /**
215
- * Check if the user has permission for an operation on a data source.
215
+ * Every operation the user is granted within a scope, merged across roles.
216
+ *
217
+ * Omit `scope` (or pass one with no ids) for tenant-wide operations; pass a
218
+ * data source id, or a {@link PermissionScope} for app / AI tool targets.
219
+ *
220
+ * Role shortcuts are deliberately excluded — this reports what the user's rules
221
+ * actually carry. Use {@link hasPermission} for an access decision.
222
+ */
223
+ declare function getAllowedOperations(user: DocyrusUser | null | undefined, scope?: string | null | PermissionScope): string[];
224
+ /**
225
+ * Check if the user has permission for an operation.
226
+ *
227
+ * `scope` accepts a data source id, a {@link PermissionScope} for app / AI tool
228
+ * targets, or nothing at all for tenant-wide operations such as `ai-access`,
229
+ * `manage_users` or `studio`.
216
230
  *
217
231
  * Permission resolution order:
218
232
  * 1. super_admin role → always true
219
233
  * 2. global_editor role + dataSourceId → true if operation is in the global_editor set
220
234
  * 3. global_viewer role + dataSourceId → true only for 'view'
221
235
  * 4. Always-permitted data sources (configurable)
222
- * 5. User's aclRules array
236
+ * 5. The user's permissions for the scope (`permissions`, falling back to `aclRules`)
223
237
  */
224
- declare function hasPermission(user: DocyrusUser | null | undefined, operation: string, dataSourceId?: string | null, config?: PermissionConfig): boolean;
238
+ declare function hasPermission(user: DocyrusUser | null | undefined, operation: string, scope?: string | null | PermissionScope, config?: PermissionConfig): boolean;
225
239
 
226
240
  declare const DEFAULT_API_URL = "https://alpha-api.docyrus.com";
227
241
  declare const DEFAULT_OAUTH_SCOPES: string[];
@@ -229,4 +243,4 @@ declare const DEFAULT_CALLBACK_PATH = "/auth/callback";
229
243
  /** Default cookie name for SSR token sync */
230
244
  declare const DEFAULT_SSR_COOKIE_KEY = "docyrus-token";
231
245
 
232
- export { AuthManager as A, DEFAULT_API_URL as D, DEFAULT_OAUTH_SCOPES as a, hasRole as b, DEFAULT_CALLBACK_PATH as c, DEFAULT_SSR_COOKIE_KEY as d, type AuthStateListener as e, getAllRoles as g, hasPermission as h };
246
+ export { AuthManager as A, DEFAULT_API_URL as D, DEFAULT_OAUTH_SCOPES as a, getAllowedOperations as b, hasRole as c, DEFAULT_CALLBACK_PATH as d, DEFAULT_SSR_COOKIE_KEY as e, type AuthStateListener as f, getAllRoles as g, hasPermission as h };
@@ -1,7 +1,7 @@
1
- export { A as AuthManager, e as AuthStateListener, D as DEFAULT_API_URL, c as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from './constants-CB5mfHCf.js';
1
+ export { A as AuthManager, f as AuthStateListener, D as DEFAULT_API_URL, d as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, b as getAllowedOperations, h as hasPermission, c as hasRole } from './constants-C9ezX9zG.js';
2
2
  import { OAuth2Tokens } from '@docyrus/api-client';
3
- import { D as DocyrusAuthConfig, N as GuidyElementsMessage, T as GuidyRoutesMessage, Q as GuidyPointAckMessage, G as GuidyRoute, K as GuidyCommandMessage, b as AuthMode } from './types-CFnKr6T_.js';
4
- export { A as AclOperation, a as AdaptiveCard, u as AppNavigationRequestMessage, v as AppRouteChangeMessage, d as AuthStatus, C as CalendarSendAckMessage, w as CalendarSendMessage, x as ChatSendAckMessage, y as ChatSendMessage, z as DocySendAckMessage, B as DocySendMessage, e as DocyrusAclRule, f as DocyrusAuthContextValue, g as DocyrusCalendarEventPayload, h as DocyrusEmailPayload, E as DocyrusNotification, i as DocyrusRole, j as DocyrusShareAdaptiveCardPayload, k as DocyrusSharePayload, l as DocyrusShareTextPayload, m as DocyrusUser, F as EmailSendAckMessage, I as EmailSendMessage, J as GuidyCommandHandler, L as GuidyElement, S as GuidyPointMessage, U as GuidyScanMessage, H as HostNavigationHandler, V as HostNavigationMessage, t as HostNavigationRequestOptions, s as HostNotificationHandler, W as HostNotificationMessage, X as HostSignInMessage, Y as IframeAuth, M as MsGraphAccount, n as MsGraphClient, o as MsGraphRequest, Z as MsGraphRequestMessage, p as MsGraphRequestOptions, q as MsGraphResponse, _ as MsGraphResponseMessage, r as MsGraphStatus, $ as MsGraphStatusRequestMessage, a0 as MsGraphStatusResponseMessage, P as PermissionConfig, R as RouteChangePayload, a1 as TokenRefreshRequestMessage } from './types-CFnKr6T_.js';
3
+ import { D as DocyrusAuthConfig, T as GuidyElementsMessage, W as GuidyRoutesMessage, U as GuidyPointAckMessage, G as GuidyRoute, Q as GuidyCommandMessage, c as AuthMode } from './types-BcLDyMdw.js';
4
+ export { A as AclOperation, a as AclTargetType, b as AdaptiveCard, x as AppNavigationRequestMessage, y as AppRouteChangeMessage, e as AuthStatus, C as CalendarSendAckMessage, z as CalendarSendMessage, B as ChatSendAckMessage, E as ChatSendMessage, F as DocySendAckMessage, I as DocySendMessage, f as DocyrusAclRule, g as DocyrusAuthContextValue, h as DocyrusCalendarEventPayload, i as DocyrusEmailPayload, J as DocyrusNotification, j as DocyrusPermission, k as DocyrusRole, l as DocyrusShareAdaptiveCardPayload, m as DocyrusSharePayload, n as DocyrusShareTextPayload, o as DocyrusUser, K as EmailSendAckMessage, L as EmailSendMessage, N as GuidyCommandHandler, S as GuidyElement, V as GuidyPointMessage, X as GuidyScanMessage, H as HostNavigationHandler, Y as HostNavigationMessage, w as HostNavigationRequestOptions, v as HostNotificationHandler, Z as HostNotificationMessage, _ as HostSignInMessage, $ as IframeAuth, M as MsGraphAccount, p as MsGraphClient, q as MsGraphRequest, a0 as MsGraphRequestMessage, r as MsGraphRequestOptions, s as MsGraphResponse, a1 as MsGraphResponseMessage, t as MsGraphStatus, a2 as MsGraphStatusRequestMessage, a3 as MsGraphStatusResponseMessage, P as PermissionConfig, u as PermissionScope, R as RouteChangePayload, a4 as TokenRefreshRequestMessage } from './types-BcLDyMdw.js';
5
5
 
6
6
  /**
7
7
  * Standalone OAuth2 authorization code flow with PKCE via page redirect.
@@ -1744,26 +1744,74 @@ function hasRole(user, role) {
1744
1744
  }
1745
1745
  return allRoles.some((r) => r.slug === role || r.uid === role);
1746
1746
  }
1747
- function hasPermission(user, operation, dataSourceId, config) {
1748
- if (!user) return false;
1749
- if (hasRole(user, "super_admin")) return true;
1750
- if (dataSourceId && hasRole(user, "global_editor")) {
1751
- if (GLOBAL_EDITOR_OPERATIONS.has(operation)) return true;
1747
+ var SCOPE_FIELD_BY_TARGET_TYPE = {
1748
+ data_source: "dataSourceId",
1749
+ field: "dataSourceId",
1750
+ app: "appId",
1751
+ ai_tool: "aiToolId"
1752
+ };
1753
+ var GLOBAL_TARGET_TYPES = [
1754
+ "ai",
1755
+ "settings",
1756
+ "devtools"
1757
+ ];
1758
+ function toScope(scope) {
1759
+ if (scope == null) return {};
1760
+ if (typeof scope === "string") return { dataSourceId: scope };
1761
+ return scope;
1762
+ }
1763
+ function resolveTargetTypes(scope) {
1764
+ if (scope.targetType) return [scope.targetType];
1765
+ if (scope.dataSourceId != null) return ["data_source"];
1766
+ if (scope.appId != null) return ["app"];
1767
+ if (scope.aiToolId != null) return ["ai_tool"];
1768
+ return GLOBAL_TARGET_TYPES;
1769
+ }
1770
+ function matchesScope(permission, scope) {
1771
+ const field = SCOPE_FIELD_BY_TARGET_TYPE[permission.targetType];
1772
+ if (!field) return true;
1773
+ return (permission[field] ?? null) === (scope[field] ?? null);
1774
+ }
1775
+ function getAllowedOperations(user, scope) {
1776
+ if (!user) return [];
1777
+ const resolved = toScope(scope);
1778
+ const targetTypes = resolveTargetTypes(resolved);
1779
+ const operations = /* @__PURE__ */ new Set();
1780
+ if (user.permissions) {
1781
+ for (const permission of user.permissions) {
1782
+ if (!targetTypes.includes(permission.targetType)) continue;
1783
+ if (!matchesScope(permission, resolved)) continue;
1784
+ for (const op of permission.allowedOperations ?? []) {
1785
+ operations.add(op);
1786
+ }
1787
+ }
1788
+ return Array.from(operations);
1752
1789
  }
1753
- if (dataSourceId && hasRole(user, "global_viewer")) {
1754
- if (operation === "view") return true;
1790
+ if (resolved.appId != null || resolved.aiToolId != null) return [];
1791
+ const dataSourceId = resolved.dataSourceId ?? null;
1792
+ for (const rule of user.aclRules ?? []) {
1793
+ if ((rule.dataSourceId ?? null) !== dataSourceId) continue;
1794
+ for (const op of rule.allowedOperations ?? []) {
1795
+ operations.add(op);
1796
+ }
1755
1797
  }
1798
+ return Array.from(operations);
1799
+ }
1800
+ function hasPermission(user, operation, scope, config) {
1801
+ if (!user) return false;
1802
+ if (hasRole(user, "super_admin")) return true;
1803
+ const resolved = toScope(scope);
1804
+ const { dataSourceId } = resolved;
1756
1805
  if (dataSourceId) {
1806
+ if (hasRole(user, "global_editor") && GLOBAL_EDITOR_OPERATIONS.has(operation)) return true;
1807
+ if (hasRole(user, "global_viewer") && operation === "view") return true;
1757
1808
  const alwaysPermitted = config?.alwaysPermittedDataSources ?? DEFAULT_ALWAYS_PERMITTED;
1758
1809
  const permittedOps = alwaysPermitted[dataSourceId];
1759
1810
  if (permittedOps?.includes(operation)) return true;
1760
1811
  }
1761
- const rules = user.aclRules ?? [];
1762
- return rules.some(
1763
- (rule) => rule.dataSourceId === dataSourceId && rule.allowedOperations.includes(operation)
1764
- );
1812
+ return getAllowedOperations(user, resolved).includes(operation);
1765
1813
  }
1766
1814
 
1767
- export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, GuidyBridge, IframeAuth, MsGraphClient, StandaloneOAuth2Auth, detectAuthMode, getAllRoles, hasPermission, hasRole };
1815
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, GuidyBridge, IframeAuth, MsGraphClient, StandaloneOAuth2Auth, detectAuthMode, getAllRoles, getAllowedOperations, hasPermission, hasRole };
1768
1816
  //# sourceMappingURL=core-index.js.map
1769
1817
  //# sourceMappingURL=core-index.js.map