@blocklet/discuss-kit-ux 1.6.182 → 1.6.183

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,10 +1,10 @@
1
1
  /// <reference types="react" />
2
- import type { User } from '../../types';
2
+ import type { SessionContextUser } from '../hooks/session';
3
3
  interface AccessControlProps {
4
4
  authenticated?: boolean;
5
5
  permissions?: string | string[];
6
6
  roles?: string | string[];
7
- check?: (user: User) => boolean;
7
+ check?: (user: SessionContextUser) => boolean;
8
8
  children: React.ReactElement;
9
9
  }
10
10
  export default function AccessControl({ authenticated, permissions, roles, check, children, }: AccessControlProps): import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | null;
@@ -1,14 +1,12 @@
1
1
  type HasPermission = (perms: string | string[]) => boolean;
2
- type HasRole = (roles: string | string[]) => boolean;
3
2
  interface AuthzProviderProps {
4
- fetchPermissions: () => Promise<string[]>;
5
3
  }
6
4
  interface AuthzContextValue {
7
5
  permissions: string[];
8
6
  hasPermission: HasPermission;
9
- hasRole: HasRole;
10
7
  isAdmin: boolean;
8
+ hasAnyPassport: (passports: string | string[]) => boolean;
11
9
  }
12
10
  export declare const useAuthzContext: () => AuthzContextValue;
13
- export declare const AuthzProvider: ({ fetchPermissions, ...rest }: AuthzProviderProps) => import("react/jsx-runtime").JSX.Element;
11
+ export declare const AuthzProvider: ({ ...rest }: AuthzProviderProps) => import("react/jsx-runtime").JSX.Element;
14
12
  export {};
@@ -1 +1,19 @@
1
- export declare const useSessionContext: () => any;
1
+ export interface SessionContextUser {
2
+ did: string;
3
+ fullName: string;
4
+ avatar: string;
5
+ passports: {
6
+ id: string;
7
+ name: string;
8
+ title: string;
9
+ }[];
10
+ permissions: string[];
11
+ role: string;
12
+ }
13
+ export declare const useSessionContext: () => {
14
+ isAdmin: boolean;
15
+ hasAnyPassport: (passports: string | string[]) => boolean;
16
+ session: {
17
+ user: SessionContextUser;
18
+ };
19
+ };
@@ -4,7 +4,7 @@ import { OnContentChangePlugin } from "@blocklet/editor/lib/ext/OnContentChangeP
4
4
  import { CtrlsShortcutPlugin } from "@blocklet/editor/lib/ext/ShortcutPlugin";
5
5
  import { SafeAreaPlugin } from "@blocklet/editor/lib/ext/SafeAreaPlugin";
6
6
  import { lazy } from "react";
7
- import { i as inferInitialEditorState, I as ImagePathFixerPlugin, V as VideoPathFixerPlugin, a as isEmptyContent, s as stringify, g as getExcerptSync } from "./index-QDqykalr.mjs";
7
+ import { i as inferInitialEditorState, I as ImagePathFixerPlugin, V as VideoPathFixerPlugin, a as isEmptyContent, s as stringify, g as getExcerptSync } from "./index-LsAi57M8.mjs";
8
8
  import "@blocklet/labels";
9
9
  import "@mui/material/styles";
10
10
  import "@mui/material/Box";
@@ -917,10 +917,18 @@ function Badge({
917
917
  }
918
918
  const useSessionContext = () => {
919
919
  var _a2, _b2;
920
- const ctx = useContext(SessionContext);
920
+ const ctx = useContext(SessionContext) || {};
921
+ const loginRole = (_b2 = (_a2 = ctx == null ? void 0 : ctx.session) == null ? void 0 : _a2.user) == null ? void 0 : _b2.role;
921
922
  return {
922
- ...ctx || {},
923
- isAdmin: ["admin", "owner"].includes((_b2 = (_a2 = ctx == null ? void 0 : ctx.session) == null ? void 0 : _a2.user) == null ? void 0 : _b2.role)
923
+ ...ctx,
924
+ isAdmin: ["admin", "owner"].includes(loginRole),
925
+ hasAnyPassport: (passports) => {
926
+ if (!passports || passports.length === 0) {
927
+ return true;
928
+ }
929
+ const passportsArr = Array.isArray(passports) ? passports : [passports];
930
+ return passportsArr.includes(loginRole);
931
+ }
924
932
  };
925
933
  };
926
934
  const isEqual = (a, b) => {
@@ -3340,27 +3348,18 @@ function BlogPermaLink({ slug, onChange, ...rest }) {
3340
3348
  const AuthzContext = createContext({
3341
3349
  permissions: [],
3342
3350
  hasPermission: () => false,
3343
- hasRole: () => false,
3344
- isAdmin: false
3351
+ isAdmin: false,
3352
+ hasAnyPassport: () => false
3345
3353
  });
3346
3354
  const useAuthzContext = () => useContext(AuthzContext);
3347
- const AuthzProvider = ({ fetchPermissions, ...rest }) => {
3348
- var _a2, _b2, _c;
3349
- const { session } = useSessionContext();
3350
- const [permissions, setPermissions] = useState([]);
3351
- const permissionSet = useMemo(() => new Set(permissions), [permissions]);
3352
- useEffect(() => {
3355
+ const AuthzProvider = ({ ...rest }) => {
3356
+ var _a2;
3357
+ const { session, isAdmin, hasAnyPassport } = useSessionContext();
3358
+ const permissions = useMemo(() => {
3353
3359
  var _a3;
3354
- const initPermissions = async () => {
3355
- const perms = await fetchPermissions();
3356
- if (perms == null ? void 0 : perms.length) {
3357
- setPermissions(perms);
3358
- }
3359
- };
3360
- if ((_a3 = session == null ? void 0 : session.user) == null ? void 0 : _a3.role) {
3361
- initPermissions();
3362
- }
3363
- }, [(_a2 = session == null ? void 0 : session.user) == null ? void 0 : _a2.role]);
3360
+ return ((_a3 = session == null ? void 0 : session.user) == null ? void 0 : _a3.permissions) || [];
3361
+ }, [(_a2 = session == null ? void 0 : session.user) == null ? void 0 : _a2.permissions]);
3362
+ const permissionSet = useMemo(() => new Set(permissions), [permissions]);
3364
3363
  const hasPermission = useCallback(
3365
3364
  (perms) => {
3366
3365
  if (!perms || perms.length === 0) {
@@ -3371,21 +3370,9 @@ const AuthzProvider = ({ fetchPermissions, ...rest }) => {
3371
3370
  },
3372
3371
  [permissionSet]
3373
3372
  );
3374
- const hasRole = useCallback(
3375
- (roles) => {
3376
- var _a3;
3377
- if (!roles || roles.length === 0) {
3378
- return true;
3379
- }
3380
- const rolesArr = Array.isArray(roles) ? roles : [roles];
3381
- return rolesArr.includes((_a3 = session == null ? void 0 : session.user) == null ? void 0 : _a3.role);
3382
- },
3383
- [(_b2 = session == null ? void 0 : session.user) == null ? void 0 : _b2.role]
3384
- );
3385
- const isAdmin = ["admin", "owner"].includes((_c = session == null ? void 0 : session.user) == null ? void 0 : _c.role);
3386
3373
  const value = useMemo(
3387
- () => ({ permissions, hasPermission, hasRole, isAdmin }),
3388
- [hasPermission, hasRole, permissions, isAdmin]
3374
+ () => ({ permissions, hasPermission, isAdmin, hasAnyPassport }),
3375
+ [hasPermission, permissions, isAdmin, hasAnyPassport]
3389
3376
  );
3390
3377
  return /* @__PURE__ */ jsx(AuthzContext.Provider, { ...rest, value });
3391
3378
  };
@@ -3397,11 +3384,11 @@ function AccessControl({
3397
3384
  children
3398
3385
  }) {
3399
3386
  const { session } = useSessionContext();
3400
- const { hasPermission, hasRole } = useAuthzContext();
3387
+ const { hasPermission, hasAnyPassport } = useAuthzContext();
3401
3388
  if (authenticated && !(session == null ? void 0 : session.user)) {
3402
3389
  return null;
3403
3390
  }
3404
- const permitted = hasPermission(permissions || "") && hasRole(roles || "") && (!check || check(session.user));
3391
+ const permitted = hasPermission(permissions || "") && hasAnyPassport(roles || "") && (!check || check(session.user));
3405
3392
  if (permitted) {
3406
3393
  return children;
3407
3394
  }
@@ -4810,7 +4797,7 @@ function Pagination({
4810
4797
  }
4811
4798
  );
4812
4799
  }
4813
- const Editor = lazy(() => import("./editor-fQ1zjzhm.mjs"));
4800
+ const Editor = lazy(() => import("./editor-LUBMEeS4.mjs"));
4814
4801
  function LazyEditor(props) {
4815
4802
  const fallback = /* @__PURE__ */ jsxs(Fragment, { children: [
4816
4803
  /* @__PURE__ */ jsx(Skeleton, {}),
package/dist/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "@blocklet/labels";
2
- import { O, j, T, A, h, a9, B, z, H, F, J, al, m, W, U, X, Z, q, C, v, w, y, a1, a2, ad, a4, L, N, D, ac, ab, E, G, b, f, aa, M, P, ak, o, n, a8, R, S, a5, am, k, _, a0, ae, ah, ag, ai, an, K, ao, l, p, r, e, t, a6, Q, c, Y, x, a3, a7, u, aj, ap, $, af, d } from "./index-QDqykalr.mjs";
2
+ import { O, j, T, A, h, a9, B, z, H, F, J, al, m, W, U, X, Z, q, C, v, w, y, a1, a2, ad, a4, L, N, D, ac, ab, E, G, b, f, aa, M, P, ak, o, n, a8, R, S, a5, am, k, _, a0, ae, ah, ag, ai, an, K, ao, l, p, r, e, t, a6, Q, c, Y, x, a3, a7, u, aj, ap, $, af, d } from "./index-LsAi57M8.mjs";
3
3
  import "react/jsx-runtime";
4
4
  import "react";
5
5
  import "@mui/material/Box";
package/dist/index.umd.js CHANGED
@@ -850,10 +850,18 @@ var __publicField = (obj, key, value) => {
850
850
  }
851
851
  const useSessionContext = () => {
852
852
  var _a2, _b2;
853
- const ctx = react.useContext(Session.SessionContext);
853
+ const ctx = react.useContext(Session.SessionContext) || {};
854
+ const loginRole = (_b2 = (_a2 = ctx == null ? void 0 : ctx.session) == null ? void 0 : _a2.user) == null ? void 0 : _b2.role;
854
855
  return {
855
- ...ctx || {},
856
- isAdmin: ["admin", "owner"].includes((_b2 = (_a2 = ctx == null ? void 0 : ctx.session) == null ? void 0 : _a2.user) == null ? void 0 : _b2.role)
856
+ ...ctx,
857
+ isAdmin: ["admin", "owner"].includes(loginRole),
858
+ hasAnyPassport: (passports) => {
859
+ if (!passports || passports.length === 0) {
860
+ return true;
861
+ }
862
+ const passportsArr = Array.isArray(passports) ? passports : [passports];
863
+ return passportsArr.includes(loginRole);
864
+ }
857
865
  };
858
866
  };
859
867
  const isEqual = (a, b) => {
@@ -3273,27 +3281,18 @@ var __publicField = (obj, key, value) => {
3273
3281
  const AuthzContext = react.createContext({
3274
3282
  permissions: [],
3275
3283
  hasPermission: () => false,
3276
- hasRole: () => false,
3277
- isAdmin: false
3284
+ isAdmin: false,
3285
+ hasAnyPassport: () => false
3278
3286
  });
3279
3287
  const useAuthzContext = () => react.useContext(AuthzContext);
3280
- const AuthzProvider = ({ fetchPermissions, ...rest }) => {
3281
- var _a2, _b2, _c;
3282
- const { session } = useSessionContext();
3283
- const [permissions, setPermissions] = react.useState([]);
3284
- const permissionSet = react.useMemo(() => new Set(permissions), [permissions]);
3285
- react.useEffect(() => {
3288
+ const AuthzProvider = ({ ...rest }) => {
3289
+ var _a2;
3290
+ const { session, isAdmin, hasAnyPassport } = useSessionContext();
3291
+ const permissions = react.useMemo(() => {
3286
3292
  var _a3;
3287
- const initPermissions = async () => {
3288
- const perms = await fetchPermissions();
3289
- if (perms == null ? void 0 : perms.length) {
3290
- setPermissions(perms);
3291
- }
3292
- };
3293
- if ((_a3 = session == null ? void 0 : session.user) == null ? void 0 : _a3.role) {
3294
- initPermissions();
3295
- }
3296
- }, [(_a2 = session == null ? void 0 : session.user) == null ? void 0 : _a2.role]);
3293
+ return ((_a3 = session == null ? void 0 : session.user) == null ? void 0 : _a3.permissions) || [];
3294
+ }, [(_a2 = session == null ? void 0 : session.user) == null ? void 0 : _a2.permissions]);
3295
+ const permissionSet = react.useMemo(() => new Set(permissions), [permissions]);
3297
3296
  const hasPermission = react.useCallback(
3298
3297
  (perms) => {
3299
3298
  if (!perms || perms.length === 0) {
@@ -3304,21 +3303,9 @@ var __publicField = (obj, key, value) => {
3304
3303
  },
3305
3304
  [permissionSet]
3306
3305
  );
3307
- const hasRole = react.useCallback(
3308
- (roles) => {
3309
- var _a3;
3310
- if (!roles || roles.length === 0) {
3311
- return true;
3312
- }
3313
- const rolesArr = Array.isArray(roles) ? roles : [roles];
3314
- return rolesArr.includes((_a3 = session == null ? void 0 : session.user) == null ? void 0 : _a3.role);
3315
- },
3316
- [(_b2 = session == null ? void 0 : session.user) == null ? void 0 : _b2.role]
3317
- );
3318
- const isAdmin = ["admin", "owner"].includes((_c = session == null ? void 0 : session.user) == null ? void 0 : _c.role);
3319
3306
  const value = react.useMemo(
3320
- () => ({ permissions, hasPermission, hasRole, isAdmin }),
3321
- [hasPermission, hasRole, permissions, isAdmin]
3307
+ () => ({ permissions, hasPermission, isAdmin, hasAnyPassport }),
3308
+ [hasPermission, permissions, isAdmin, hasAnyPassport]
3322
3309
  );
3323
3310
  return /* @__PURE__ */ jsxRuntime.jsx(AuthzContext.Provider, { ...rest, value });
3324
3311
  };
@@ -3330,11 +3317,11 @@ var __publicField = (obj, key, value) => {
3330
3317
  children
3331
3318
  }) {
3332
3319
  const { session } = useSessionContext();
3333
- const { hasPermission, hasRole } = useAuthzContext();
3320
+ const { hasPermission, hasAnyPassport } = useAuthzContext();
3334
3321
  if (authenticated && !(session == null ? void 0 : session.user)) {
3335
3322
  return null;
3336
3323
  }
3337
- const permitted = hasPermission(permissions || "") && hasRole(roles || "") && (!check || check(session.user));
3324
+ const permitted = hasPermission(permissions || "") && hasAnyPassport(roles || "") && (!check || check(session.user));
3338
3325
  if (permitted) {
3339
3326
  return children;
3340
3327
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/discuss-kit-ux",
3
- "version": "1.6.182",
3
+ "version": "1.6.183",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
@@ -29,8 +29,8 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@arcblock/ws": "^1.18.113",
32
- "@blocklet/editor": "1.6.182",
33
- "@blocklet/labels": "1.6.182",
32
+ "@blocklet/editor": "1.6.183",
33
+ "@blocklet/labels": "1.6.183",
34
34
  "@blocklet/uploader": "^0.0.75",
35
35
  "@emotion/css": "^11.10.5",
36
36
  "@emotion/react": "^11.10.5",
@@ -94,5 +94,5 @@
94
94
  "resolutions": {
95
95
  "react": "^18.2.0"
96
96
  },
97
- "gitHead": "77622b088302b4a4251b9f3479c1d1b8c1fee9ce"
97
+ "gitHead": "59597449f4d3d4373b598fcf9e80bdc7eb9c098c"
98
98
  }