@elementor/editor-current-user 4.3.0-beta1 → 4.3.0-beta3

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/dist/index.d.mts CHANGED
@@ -12,18 +12,23 @@ declare const useCurrentUserCapabilities: () => {
12
12
  type User = {
13
13
  suppressedMessages: string[];
14
14
  capabilities: string[];
15
+ restrictions: string[];
15
16
  };
16
17
 
17
18
  type UserModel = {
18
19
  suppressedMessages: string[];
19
20
  capabilities: string[];
21
+ restrictions: string[];
20
22
  };
21
23
 
22
24
  declare const getCurrentUser: () => UserModel | undefined;
23
25
 
26
+ declare const isContentOnlyUser: () => boolean;
27
+
24
28
  declare function ensureUser(): Promise<{
25
29
  capabilities: string[];
26
30
  suppressedMessages: string[];
31
+ restrictions: string[];
27
32
  }>;
28
33
 
29
34
  declare function onSetUser(callback: (user: UserModel | null) => void): () => void;
@@ -31,8 +36,17 @@ declare function onSetUser(callback: (user: UserModel | null) => void): () => vo
31
36
  declare const useCurrentUser: () => _tanstack_react_query.UseQueryResult<{
32
37
  capabilities: string[];
33
38
  suppressedMessages: string[];
39
+ restrictions: string[];
34
40
  }, Error>;
35
41
 
36
42
  declare const useUpdateCurrentUser: () => _tanstack_react_query.UseMutationResult<axios.AxiosResponse<Partial<UserModel>, any, {}>, Error, Partial<User>, unknown>;
37
43
 
38
- export { ensureUser, getCurrentUser, onSetUser, useCurrentUser, useCurrentUserCapabilities, useSuppressedMessage, useUpdateCurrentUser };
44
+ declare const DESIGN_RESTRICTION = "design";
45
+ declare const useUserRestrictions: () => {
46
+ isRestricted: (restriction: string) => boolean;
47
+ restrictions: string[] | undefined;
48
+ };
49
+
50
+ declare const useHasContentOnlyAccess: () => boolean;
51
+
52
+ export { DESIGN_RESTRICTION, ensureUser, getCurrentUser, isContentOnlyUser, onSetUser, useCurrentUser, useCurrentUserCapabilities, useHasContentOnlyAccess, useSuppressedMessage, useUpdateCurrentUser, useUserRestrictions };
package/dist/index.d.ts CHANGED
@@ -12,18 +12,23 @@ declare const useCurrentUserCapabilities: () => {
12
12
  type User = {
13
13
  suppressedMessages: string[];
14
14
  capabilities: string[];
15
+ restrictions: string[];
15
16
  };
16
17
 
17
18
  type UserModel = {
18
19
  suppressedMessages: string[];
19
20
  capabilities: string[];
21
+ restrictions: string[];
20
22
  };
21
23
 
22
24
  declare const getCurrentUser: () => UserModel | undefined;
23
25
 
26
+ declare const isContentOnlyUser: () => boolean;
27
+
24
28
  declare function ensureUser(): Promise<{
25
29
  capabilities: string[];
26
30
  suppressedMessages: string[];
31
+ restrictions: string[];
27
32
  }>;
28
33
 
29
34
  declare function onSetUser(callback: (user: UserModel | null) => void): () => void;
@@ -31,8 +36,17 @@ declare function onSetUser(callback: (user: UserModel | null) => void): () => vo
31
36
  declare const useCurrentUser: () => _tanstack_react_query.UseQueryResult<{
32
37
  capabilities: string[];
33
38
  suppressedMessages: string[];
39
+ restrictions: string[];
34
40
  }, Error>;
35
41
 
36
42
  declare const useUpdateCurrentUser: () => _tanstack_react_query.UseMutationResult<axios.AxiosResponse<Partial<UserModel>, any, {}>, Error, Partial<User>, unknown>;
37
43
 
38
- export { ensureUser, getCurrentUser, onSetUser, useCurrentUser, useCurrentUserCapabilities, useSuppressedMessage, useUpdateCurrentUser };
44
+ declare const DESIGN_RESTRICTION = "design";
45
+ declare const useUserRestrictions: () => {
46
+ isRestricted: (restriction: string) => boolean;
47
+ restrictions: string[] | undefined;
48
+ };
49
+
50
+ declare const useHasContentOnlyAccess: () => boolean;
51
+
52
+ export { DESIGN_RESTRICTION, ensureUser, getCurrentUser, isContentOnlyUser, onSetUser, useCurrentUser, useCurrentUserCapabilities, useHasContentOnlyAccess, useSuppressedMessage, useUpdateCurrentUser, useUserRestrictions };
package/dist/index.js CHANGED
@@ -20,13 +20,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ DESIGN_RESTRICTION: () => DESIGN_RESTRICTION,
23
24
  ensureUser: () => ensureUser,
24
25
  getCurrentUser: () => getCurrentUser,
26
+ isContentOnlyUser: () => isContentOnlyUser,
25
27
  onSetUser: () => onSetUser,
26
28
  useCurrentUser: () => useCurrentUser,
27
29
  useCurrentUserCapabilities: () => useCurrentUserCapabilities,
30
+ useHasContentOnlyAccess: () => useHasContentOnlyAccess,
28
31
  useSuppressedMessage: () => useSuppressedMessage,
29
- useUpdateCurrentUser: () => useUpdateCurrentUser
32
+ useUpdateCurrentUser: () => useUpdateCurrentUser,
33
+ useUserRestrictions: () => useUserRestrictions
30
34
  });
31
35
  module.exports = __toCommonJS(index_exports);
32
36
 
@@ -39,8 +43,8 @@ var RESOURCE_URL = "elementor/v1/user-data/current-user";
39
43
  var getUserPayload = { params: { context: "edit" } };
40
44
  var apiClient = {
41
45
  get: () => (0, import_http_client.httpService)().get(RESOURCE_URL, getUserPayload).then((res) => {
42
- const { capabilities = [], suppressedMessages = [] } = res.data;
43
- return { capabilities, suppressedMessages };
46
+ const { capabilities = [], suppressedMessages = [], restrictions = [] } = res.data;
47
+ return { capabilities, suppressedMessages, restrictions };
44
48
  }),
45
49
  update: (data) => (0, import_http_client.httpService)().patch(RESOURCE_URL, {
46
50
  suppressedMessages: data.suppressedMessages
@@ -97,6 +101,22 @@ var getCurrentUser = () => {
97
101
  return queryClient.getQueryData([EDITOR_CURRENT_USER_QUERY_KEY]);
98
102
  };
99
103
 
104
+ // src/use-user-restrictions.ts
105
+ var DESIGN_RESTRICTION = "design";
106
+ var useUserRestrictions = () => {
107
+ const { data } = useCurrentUser();
108
+ const isRestricted = (restriction) => Boolean(data?.restrictions?.includes(restriction));
109
+ return {
110
+ isRestricted,
111
+ restrictions: data?.restrictions
112
+ };
113
+ };
114
+
115
+ // src/is-content-only-user.ts
116
+ var isContentOnlyUser = () => {
117
+ return Boolean(getCurrentUser()?.restrictions?.includes(DESIGN_RESTRICTION));
118
+ };
119
+
100
120
  // src/ensure-current-user.ts
101
121
  var import_query4 = require("@elementor/query");
102
122
  async function ensureUser() {
@@ -126,14 +146,21 @@ function onSetUser(callback) {
126
146
  unsubscribeListener();
127
147
  };
128
148
  }
149
+
150
+ // src/use-has-content-only-access.ts
151
+ var useHasContentOnlyAccess = () => useUserRestrictions().isRestricted(DESIGN_RESTRICTION);
129
152
  // Annotate the CommonJS export names for ESM import in node:
130
153
  0 && (module.exports = {
154
+ DESIGN_RESTRICTION,
131
155
  ensureUser,
132
156
  getCurrentUser,
157
+ isContentOnlyUser,
133
158
  onSetUser,
134
159
  useCurrentUser,
135
160
  useCurrentUserCapabilities,
161
+ useHasContentOnlyAccess,
136
162
  useSuppressedMessage,
137
- useUpdateCurrentUser
163
+ useUpdateCurrentUser,
164
+ useUserRestrictions
138
165
  });
139
166
  //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -7,8 +7,8 @@ var RESOURCE_URL = "elementor/v1/user-data/current-user";
7
7
  var getUserPayload = { params: { context: "edit" } };
8
8
  var apiClient = {
9
9
  get: () => httpService().get(RESOURCE_URL, getUserPayload).then((res) => {
10
- const { capabilities = [], suppressedMessages = [] } = res.data;
11
- return { capabilities, suppressedMessages };
10
+ const { capabilities = [], suppressedMessages = [], restrictions = [] } = res.data;
11
+ return { capabilities, suppressedMessages, restrictions };
12
12
  }),
13
13
  update: (data) => httpService().patch(RESOURCE_URL, {
14
14
  suppressedMessages: data.suppressedMessages
@@ -65,6 +65,22 @@ var getCurrentUser = () => {
65
65
  return queryClient.getQueryData([EDITOR_CURRENT_USER_QUERY_KEY]);
66
66
  };
67
67
 
68
+ // src/use-user-restrictions.ts
69
+ var DESIGN_RESTRICTION = "design";
70
+ var useUserRestrictions = () => {
71
+ const { data } = useCurrentUser();
72
+ const isRestricted = (restriction) => Boolean(data?.restrictions?.includes(restriction));
73
+ return {
74
+ isRestricted,
75
+ restrictions: data?.restrictions
76
+ };
77
+ };
78
+
79
+ // src/is-content-only-user.ts
80
+ var isContentOnlyUser = () => {
81
+ return Boolean(getCurrentUser()?.restrictions?.includes(DESIGN_RESTRICTION));
82
+ };
83
+
68
84
  // src/ensure-current-user.ts
69
85
  import { getQueryClient as getQueryClient2 } from "@elementor/query";
70
86
  async function ensureUser() {
@@ -94,13 +110,20 @@ function onSetUser(callback) {
94
110
  unsubscribeListener();
95
111
  };
96
112
  }
113
+
114
+ // src/use-has-content-only-access.ts
115
+ var useHasContentOnlyAccess = () => useUserRestrictions().isRestricted(DESIGN_RESTRICTION);
97
116
  export {
117
+ DESIGN_RESTRICTION,
98
118
  ensureUser,
99
119
  getCurrentUser,
120
+ isContentOnlyUser,
100
121
  onSetUser,
101
122
  useCurrentUser,
102
123
  useCurrentUserCapabilities,
124
+ useHasContentOnlyAccess,
103
125
  useSuppressedMessage,
104
- useUpdateCurrentUser
126
+ useUpdateCurrentUser,
127
+ useUserRestrictions
105
128
  };
106
129
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elementor/editor-current-user",
3
3
  "description": "Elementor Editor Current User",
4
- "version": "4.3.0-beta1",
4
+ "version": "4.3.0-beta3",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -36,9 +36,9 @@
36
36
  "react": "^18.3.1"
37
37
  },
38
38
  "dependencies": {
39
- "@elementor/editor-v1-adapters": "4.3.0-beta1",
40
- "@elementor/http-client": "4.3.0-beta1",
41
- "@elementor/query": "4.3.0-beta1"
39
+ "@elementor/editor-v1-adapters": "4.3.0-beta3",
40
+ "@elementor/http-client": "4.3.0-beta3",
41
+ "@elementor/query": "4.3.0-beta3"
42
42
  },
43
43
  "devDependencies": {
44
44
  "tsup": "^8.3.5"
@@ -0,0 +1,45 @@
1
+ import { type UserModel } from '../api';
2
+ import { getCurrentUser } from '../get-current-user';
3
+ import { isContentOnlyUser } from '../is-content-only-user';
4
+ import { DESIGN_RESTRICTION } from '../use-user-restrictions';
5
+
6
+ jest.mock( '../get-current-user' );
7
+
8
+ describe( 'isContentOnlyUser', () => {
9
+ it( 'should return true when the design restriction is present', () => {
10
+ // Arrange
11
+ jest.mocked( getCurrentUser ).mockReturnValue( {
12
+ restrictions: [ DESIGN_RESTRICTION ],
13
+ } as UserModel );
14
+
15
+ // Act
16
+ const result = isContentOnlyUser();
17
+
18
+ // Assert
19
+ expect( result ).toBe( true );
20
+ } );
21
+
22
+ it( 'should return false when the design restriction is absent', () => {
23
+ // Arrange
24
+ jest.mocked( getCurrentUser ).mockReturnValue( {
25
+ restrictions: [ 'json-upload' ],
26
+ } as UserModel );
27
+
28
+ // Act
29
+ const result = isContentOnlyUser();
30
+
31
+ // Assert
32
+ expect( result ).toBe( false );
33
+ } );
34
+
35
+ it( 'should return false when there is no cached user', () => {
36
+ // Arrange
37
+ jest.mocked( getCurrentUser ).mockReturnValue( undefined );
38
+
39
+ // Act
40
+ const result = isContentOnlyUser();
41
+
42
+ // Assert
43
+ expect( result ).toBe( false );
44
+ } );
45
+ } );
@@ -0,0 +1,77 @@
1
+ import { type UseQueryResult } from '@elementor/query';
2
+ import { renderHook } from '@testing-library/react';
3
+
4
+ import { type User } from '../types';
5
+ import { useCurrentUser } from '../use-current-user';
6
+ import { useHasContentOnlyAccess } from '../use-has-content-only-access';
7
+ import { DESIGN_RESTRICTION, useUserRestrictions } from '../use-user-restrictions';
8
+
9
+ jest.mock( '../use-current-user' );
10
+
11
+ const mockRestrictions = ( restrictions: string[] ) => {
12
+ jest.mocked( useCurrentUser ).mockReturnValue( {
13
+ data: { restrictions },
14
+ } as UseQueryResult< User, Error > );
15
+ };
16
+
17
+ describe( 'useUserRestrictions', () => {
18
+ it( 'should report the restrictions it was given', () => {
19
+ // Arrange
20
+ mockRestrictions( [ 'json-upload' ] );
21
+
22
+ // Act
23
+ const { result } = renderHook( () => useUserRestrictions() );
24
+
25
+ // Assert
26
+ expect( result.current.restrictions ).toEqual( [ 'json-upload' ] );
27
+ expect( result.current.isRestricted( 'json-upload' ) ).toBe( true );
28
+ expect( result.current.isRestricted( DESIGN_RESTRICTION ) ).toBe( false );
29
+ } );
30
+
31
+ it( 'should report nothing as restricted while the user data is unavailable', () => {
32
+ // Arrange
33
+ jest.mocked( useCurrentUser ).mockReturnValue( {} as UseQueryResult< User, Error > );
34
+
35
+ // Act
36
+ const { result } = renderHook( () => useUserRestrictions() );
37
+
38
+ // Assert
39
+ expect( result.current.restrictions ).toBeUndefined();
40
+ expect( result.current.isRestricted( DESIGN_RESTRICTION ) ).toBe( false );
41
+ } );
42
+ } );
43
+
44
+ describe( 'useHasContentOnlyAccess', () => {
45
+ it( 'should report content-only access when the design restriction is present', () => {
46
+ // Arrange
47
+ mockRestrictions( [ DESIGN_RESTRICTION ] );
48
+
49
+ // Act
50
+ const { result } = renderHook( () => useHasContentOnlyAccess() );
51
+
52
+ // Assert
53
+ expect( result.current ).toBe( true );
54
+ } );
55
+
56
+ it( 'should not report content-only access for unrelated restrictions', () => {
57
+ // Arrange
58
+ mockRestrictions( [ 'json-upload' ] );
59
+
60
+ // Act
61
+ const { result } = renderHook( () => useHasContentOnlyAccess() );
62
+
63
+ // Assert
64
+ expect( result.current ).toBe( false );
65
+ } );
66
+
67
+ it( 'should not report content-only access while the user data is unavailable', () => {
68
+ // Arrange
69
+ jest.mocked( useCurrentUser ).mockReturnValue( {} as UseQueryResult< User, Error > );
70
+
71
+ // Act
72
+ const { result } = renderHook( () => useHasContentOnlyAccess() );
73
+
74
+ // Assert
75
+ expect( result.current ).toBe( false );
76
+ } );
77
+ } );
package/src/api.ts CHANGED
@@ -11,6 +11,7 @@ type GetUserPayload = {
11
11
  export type UserModel = {
12
12
  suppressedMessages: string[];
13
13
  capabilities: string[];
14
+ restrictions: string[];
14
15
  };
15
16
 
16
17
  const getUserPayload: GetUserPayload = { params: { context: 'edit' } };
@@ -20,9 +21,9 @@ export const apiClient = {
20
21
  httpService()
21
22
  .get< UserModel >( RESOURCE_URL, getUserPayload )
22
23
  .then( ( res ) => {
23
- const { capabilities = [], suppressedMessages = [] } = res.data;
24
+ const { capabilities = [], suppressedMessages = [], restrictions = [] } = res.data;
24
25
 
25
- return { capabilities, suppressedMessages };
26
+ return { capabilities, suppressedMessages, restrictions };
26
27
  } ),
27
28
  update: ( data: Partial< User > ) =>
28
29
  httpService().patch< Partial< UserModel > >( RESOURCE_URL, {
package/src/index.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export { useSuppressedMessage } from './use-suppressed-message';
2
2
  export { useCurrentUserCapabilities } from './use-current-user-capabilities';
3
3
  export { getCurrentUser } from './get-current-user';
4
+ export { isContentOnlyUser } from './is-content-only-user';
4
5
  export { ensureUser } from './ensure-current-user';
5
6
  export { onSetUser } from './on-set-user';
6
7
  export { useCurrentUser } from './use-current-user';
7
8
  export { useUpdateCurrentUser } from './use-update-current-user';
9
+ export { DESIGN_RESTRICTION, useUserRestrictions } from './use-user-restrictions';
10
+ export { useHasContentOnlyAccess } from './use-has-content-only-access';
@@ -0,0 +1,6 @@
1
+ import { getCurrentUser } from './get-current-user';
2
+ import { DESIGN_RESTRICTION } from './use-user-restrictions';
3
+
4
+ export const isContentOnlyUser = () => {
5
+ return Boolean( getCurrentUser()?.restrictions?.includes( DESIGN_RESTRICTION ) );
6
+ };
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export type User = {
2
2
  suppressedMessages: string[];
3
3
  capabilities: string[];
4
+ restrictions: string[];
4
5
  };
@@ -0,0 +1,3 @@
1
+ import { DESIGN_RESTRICTION, useUserRestrictions } from './use-user-restrictions';
2
+
3
+ export const useHasContentOnlyAccess = () => useUserRestrictions().isRestricted( DESIGN_RESTRICTION );
@@ -0,0 +1,14 @@
1
+ import { useCurrentUser } from './use-current-user';
2
+
3
+ export const DESIGN_RESTRICTION = 'design';
4
+
5
+ export const useUserRestrictions = () => {
6
+ const { data } = useCurrentUser();
7
+
8
+ const isRestricted = ( restriction: string ) => Boolean( data?.restrictions?.includes( restriction ) );
9
+
10
+ return {
11
+ isRestricted,
12
+ restrictions: data?.restrictions,
13
+ };
14
+ };