@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.
@@ -1,8 +1,8 @@
1
1
  import { OAuth2Tokens } from '@docyrus/api-client';
2
- import { D as DocyrusAuthConfig } from '../types-CFnKr6T_.js';
3
- export { A as AclOperation, a as AdaptiveCard, b as AuthMode, c as AuthSessionResult, d as AuthStatus, e as DocyrusAclRule, f as DocyrusAuthContextValue, g as DocyrusCalendarEventPayload, h as DocyrusEmailPayload, i as DocyrusRole, j as DocyrusShareAdaptiveCardPayload, k as DocyrusSharePayload, l as DocyrusShareTextPayload, m as DocyrusUser, M as MsGraphAccount, n as MsGraphClient, o as MsGraphRequest, p as MsGraphRequestOptions, q as MsGraphResponse, r as MsGraphStatus, O as OpenAuthSessionFn, P as PermissionConfig } from '../types-CFnKr6T_.js';
4
- export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from '../use-docyrus-client-BYQdgvz1.js';
5
- export { A as AuthManager, D as DEFAULT_API_URL, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from '../constants-CB5mfHCf.js';
2
+ import { D as DocyrusAuthConfig } from '../types-BcLDyMdw.js';
3
+ export { A as AclOperation, a as AclTargetType, b as AdaptiveCard, c as AuthMode, d as AuthSessionResult, e as AuthStatus, f as DocyrusAclRule, g as DocyrusAuthContextValue, h as DocyrusCalendarEventPayload, i as DocyrusEmailPayload, j as DocyrusPermission, k as DocyrusRole, l as DocyrusShareAdaptiveCardPayload, m as DocyrusSharePayload, n as DocyrusShareTextPayload, o as DocyrusUser, M as MsGraphAccount, p as MsGraphClient, q as MsGraphRequest, r as MsGraphRequestOptions, s as MsGraphResponse, t as MsGraphStatus, O as OpenAuthSessionFn, P as PermissionConfig, u as PermissionScope } from '../types-BcLDyMdw.js';
4
+ export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from '../use-docyrus-client-C9uiHma9.js';
5
+ export { A as AuthManager, D as DEFAULT_API_URL, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, b as getAllowedOperations, h as hasPermission, c as hasRole } from '../constants-C9ezX9zG.js';
6
6
  import 'react';
7
7
 
8
8
  /**
@@ -1748,24 +1748,72 @@ function hasRole(user, role) {
1748
1748
  }
1749
1749
  return allRoles.some((r) => r.slug === role || r.uid === role);
1750
1750
  }
1751
- function hasPermission(user, operation, dataSourceId, config) {
1752
- if (!user) return false;
1753
- if (hasRole(user, "super_admin")) return true;
1754
- if (dataSourceId && hasRole(user, "global_editor")) {
1755
- if (GLOBAL_EDITOR_OPERATIONS.has(operation)) return true;
1751
+ var SCOPE_FIELD_BY_TARGET_TYPE = {
1752
+ data_source: "dataSourceId",
1753
+ field: "dataSourceId",
1754
+ app: "appId",
1755
+ ai_tool: "aiToolId"
1756
+ };
1757
+ var GLOBAL_TARGET_TYPES = [
1758
+ "ai",
1759
+ "settings",
1760
+ "devtools"
1761
+ ];
1762
+ function toScope(scope) {
1763
+ if (scope == null) return {};
1764
+ if (typeof scope === "string") return { dataSourceId: scope };
1765
+ return scope;
1766
+ }
1767
+ function resolveTargetTypes(scope) {
1768
+ if (scope.targetType) return [scope.targetType];
1769
+ if (scope.dataSourceId != null) return ["data_source"];
1770
+ if (scope.appId != null) return ["app"];
1771
+ if (scope.aiToolId != null) return ["ai_tool"];
1772
+ return GLOBAL_TARGET_TYPES;
1773
+ }
1774
+ function matchesScope(permission, scope) {
1775
+ const field = SCOPE_FIELD_BY_TARGET_TYPE[permission.targetType];
1776
+ if (!field) return true;
1777
+ return (permission[field] ?? null) === (scope[field] ?? null);
1778
+ }
1779
+ function getAllowedOperations(user, scope) {
1780
+ if (!user) return [];
1781
+ const resolved = toScope(scope);
1782
+ const targetTypes = resolveTargetTypes(resolved);
1783
+ const operations = /* @__PURE__ */ new Set();
1784
+ if (user.permissions) {
1785
+ for (const permission of user.permissions) {
1786
+ if (!targetTypes.includes(permission.targetType)) continue;
1787
+ if (!matchesScope(permission, resolved)) continue;
1788
+ for (const op of permission.allowedOperations ?? []) {
1789
+ operations.add(op);
1790
+ }
1791
+ }
1792
+ return Array.from(operations);
1756
1793
  }
1757
- if (dataSourceId && hasRole(user, "global_viewer")) {
1758
- if (operation === "view") return true;
1794
+ if (resolved.appId != null || resolved.aiToolId != null) return [];
1795
+ const dataSourceId = resolved.dataSourceId ?? null;
1796
+ for (const rule of user.aclRules ?? []) {
1797
+ if ((rule.dataSourceId ?? null) !== dataSourceId) continue;
1798
+ for (const op of rule.allowedOperations ?? []) {
1799
+ operations.add(op);
1800
+ }
1759
1801
  }
1802
+ return Array.from(operations);
1803
+ }
1804
+ function hasPermission(user, operation, scope, config) {
1805
+ if (!user) return false;
1806
+ if (hasRole(user, "super_admin")) return true;
1807
+ const resolved = toScope(scope);
1808
+ const { dataSourceId } = resolved;
1760
1809
  if (dataSourceId) {
1810
+ if (hasRole(user, "global_editor") && GLOBAL_EDITOR_OPERATIONS.has(operation)) return true;
1811
+ if (hasRole(user, "global_viewer") && operation === "view") return true;
1761
1812
  const alwaysPermitted = config?.alwaysPermittedDataSources ?? DEFAULT_ALWAYS_PERMITTED;
1762
1813
  const permittedOps = alwaysPermitted[dataSourceId];
1763
1814
  if (permittedOps?.includes(operation)) return true;
1764
1815
  }
1765
- const rules = user.aclRules ?? [];
1766
- return rules.some(
1767
- (rule) => rule.dataSourceId === dataSourceId && rule.allowedOperations.includes(operation)
1768
- );
1816
+ return getAllowedOperations(user, resolved).includes(operation);
1769
1817
  }
1770
1818
  var DocyrusAuthContext = createContext(null);
1771
1819
  function DocyrusAuthProvider({
@@ -1823,7 +1871,7 @@ function DocyrusAuthProvider({
1823
1871
  return (role) => hasRole(user, role);
1824
1872
  }, [user]);
1825
1873
  const hasPermission2 = useMemo(() => {
1826
- return (operation, dataSourceId) => hasPermission(user, operation, dataSourceId);
1874
+ return (operation, scope) => hasPermission(user, operation, scope);
1827
1875
  }, [user]);
1828
1876
  const refreshUser = useMemo(() => {
1829
1877
  return async () => {
@@ -1954,6 +2002,6 @@ function useDocyrusClient() {
1954
2002
  return client;
1955
2003
  }
1956
2004
 
1957
- export { AuthManager, DEFAULT_API_URL, DEFAULT_OAUTH_SCOPES, DocyrusAuthContext, DocyrusAuthProvider, ReactNativeOAuth2Auth, getAllRoles, hasPermission, hasRole, useDocyrusAuth, useDocyrusClient };
2005
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_OAUTH_SCOPES, DocyrusAuthContext, DocyrusAuthProvider, ReactNativeOAuth2Auth, getAllRoles, getAllowedOperations, hasPermission, hasRole, useDocyrusAuth, useDocyrusClient };
1958
2006
  //# sourceMappingURL=index.js.map
1959
2007
  //# sourceMappingURL=index.js.map