@strapi/plugin-users-permissions 5.0.0-beta.4 → 5.0.0-beta.6

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.
Files changed (135) hide show
  1. package/admin/src/components/FormModal/index.jsx +1 -1
  2. package/admin/src/pages/EmailTemplates/components/EmailForm.jsx +1 -1
  3. package/admin/src/pages/EmailTemplates/components/EmailTable.jsx +13 -18
  4. package/admin/src/pages/Roles/pages/ListPage/components/TableBody.jsx +4 -4
  5. package/admin/src/pages/Roles/pages/ListPage/index.jsx +1 -1
  6. package/package.json +6 -6
  7. package/server/services/user.js +21 -4
  8. package/dist/_chunks/ar-BguGUqwK.js +0 -44
  9. package/dist/_chunks/ar-BguGUqwK.js.map +0 -1
  10. package/dist/_chunks/ar-CK8BRRXB.mjs +0 -44
  11. package/dist/_chunks/ar-CK8BRRXB.mjs.map +0 -1
  12. package/dist/_chunks/cs-BVigMk0l.mjs +0 -50
  13. package/dist/_chunks/cs-BVigMk0l.mjs.map +0 -1
  14. package/dist/_chunks/cs-BW8-K_GY.js +0 -50
  15. package/dist/_chunks/cs-BW8-K_GY.js.map +0 -1
  16. package/dist/_chunks/de-BKUdRFI4.mjs +0 -62
  17. package/dist/_chunks/de-BKUdRFI4.mjs.map +0 -1
  18. package/dist/_chunks/de-owXpVluI.js +0 -62
  19. package/dist/_chunks/de-owXpVluI.js.map +0 -1
  20. package/dist/_chunks/dk-BQiTK50l.mjs +0 -86
  21. package/dist/_chunks/dk-BQiTK50l.mjs.map +0 -1
  22. package/dist/_chunks/dk-LXAnbuBk.js +0 -86
  23. package/dist/_chunks/dk-LXAnbuBk.js.map +0 -1
  24. package/dist/_chunks/en-DOHtPf-2.mjs +0 -86
  25. package/dist/_chunks/en-DOHtPf-2.mjs.map +0 -1
  26. package/dist/_chunks/en-MHo5mcsU.js +0 -86
  27. package/dist/_chunks/en-MHo5mcsU.js.map +0 -1
  28. package/dist/_chunks/es-BwLCLXAQ.js +0 -86
  29. package/dist/_chunks/es-BwLCLXAQ.js.map +0 -1
  30. package/dist/_chunks/es-DNgOVMjD.mjs +0 -86
  31. package/dist/_chunks/es-DNgOVMjD.mjs.map +0 -1
  32. package/dist/_chunks/fr-DkgRugiU.mjs +0 -50
  33. package/dist/_chunks/fr-DkgRugiU.mjs.map +0 -1
  34. package/dist/_chunks/fr-DkhpSjjm.js +0 -50
  35. package/dist/_chunks/fr-DkhpSjjm.js.map +0 -1
  36. package/dist/_chunks/id-BTemOeTZ.js +0 -62
  37. package/dist/_chunks/id-BTemOeTZ.js.map +0 -1
  38. package/dist/_chunks/id-BdEsvnaF.mjs +0 -62
  39. package/dist/_chunks/id-BdEsvnaF.mjs.map +0 -1
  40. package/dist/_chunks/index-7wfWos1T.js +0 -248
  41. package/dist/_chunks/index-7wfWos1T.js.map +0 -1
  42. package/dist/_chunks/index-BPGl32Qx.mjs +0 -253
  43. package/dist/_chunks/index-BPGl32Qx.mjs.map +0 -1
  44. package/dist/_chunks/index-BQQx9fm3.js +0 -1174
  45. package/dist/_chunks/index-BQQx9fm3.js.map +0 -1
  46. package/dist/_chunks/index-BzscZzdb.js +0 -358
  47. package/dist/_chunks/index-BzscZzdb.js.map +0 -1
  48. package/dist/_chunks/index-Cgtj8fbz.js +0 -634
  49. package/dist/_chunks/index-Cgtj8fbz.js.map +0 -1
  50. package/dist/_chunks/index-DR9uuqpY.mjs +0 -1143
  51. package/dist/_chunks/index-DR9uuqpY.mjs.map +0 -1
  52. package/dist/_chunks/index-Doewayrm.mjs +0 -249
  53. package/dist/_chunks/index-Doewayrm.mjs.map +0 -1
  54. package/dist/_chunks/index-N9_Qd-tg.mjs +0 -611
  55. package/dist/_chunks/index-N9_Qd-tg.mjs.map +0 -1
  56. package/dist/_chunks/index-i37IN_fH-BF_n1AVy.mjs +0 -10310
  57. package/dist/_chunks/index-i37IN_fH-BF_n1AVy.mjs.map +0 -1
  58. package/dist/_chunks/index-i37IN_fH-_tlNeVkL.js +0 -10334
  59. package/dist/_chunks/index-i37IN_fH-_tlNeVkL.js.map +0 -1
  60. package/dist/_chunks/index-l2QT_IDT.js +0 -272
  61. package/dist/_chunks/index-l2QT_IDT.js.map +0 -1
  62. package/dist/_chunks/index-vs42H81V.mjs +0 -336
  63. package/dist/_chunks/index-vs42H81V.mjs.map +0 -1
  64. package/dist/_chunks/it-B-rv0E24.mjs +0 -62
  65. package/dist/_chunks/it-B-rv0E24.mjs.map +0 -1
  66. package/dist/_chunks/it-D1rH6V6_.js +0 -62
  67. package/dist/_chunks/it-D1rH6V6_.js.map +0 -1
  68. package/dist/_chunks/ja-C8K-VBPD.mjs +0 -48
  69. package/dist/_chunks/ja-C8K-VBPD.mjs.map +0 -1
  70. package/dist/_chunks/ja-DqShgTMf.js +0 -48
  71. package/dist/_chunks/ja-DqShgTMf.js.map +0 -1
  72. package/dist/_chunks/ko-B9DGEPWH.js +0 -86
  73. package/dist/_chunks/ko-B9DGEPWH.js.map +0 -1
  74. package/dist/_chunks/ko-Busb0wIY.mjs +0 -86
  75. package/dist/_chunks/ko-Busb0wIY.mjs.map +0 -1
  76. package/dist/_chunks/ms-ByvsQjRt.mjs +0 -49
  77. package/dist/_chunks/ms-ByvsQjRt.mjs.map +0 -1
  78. package/dist/_chunks/ms-CPBU3LWf.js +0 -49
  79. package/dist/_chunks/ms-CPBU3LWf.js.map +0 -1
  80. package/dist/_chunks/nl-5qO8Rpcy.mjs +0 -48
  81. package/dist/_chunks/nl-5qO8Rpcy.mjs.map +0 -1
  82. package/dist/_chunks/nl-CwNB6YoO.js +0 -48
  83. package/dist/_chunks/nl-CwNB6YoO.js.map +0 -1
  84. package/dist/_chunks/pl-BdIzifBE.mjs +0 -86
  85. package/dist/_chunks/pl-BdIzifBE.mjs.map +0 -1
  86. package/dist/_chunks/pl-Do9UD69f.js +0 -86
  87. package/dist/_chunks/pl-Do9UD69f.js.map +0 -1
  88. package/dist/_chunks/pt-BIO24ioG.mjs +0 -48
  89. package/dist/_chunks/pt-BIO24ioG.mjs.map +0 -1
  90. package/dist/_chunks/pt-BR-D7dZhxuP.js +0 -44
  91. package/dist/_chunks/pt-BR-D7dZhxuP.js.map +0 -1
  92. package/dist/_chunks/pt-BR-f0p23AQZ.mjs +0 -44
  93. package/dist/_chunks/pt-BR-f0p23AQZ.mjs.map +0 -1
  94. package/dist/_chunks/pt-fdvyOnUp.js +0 -48
  95. package/dist/_chunks/pt-fdvyOnUp.js.map +0 -1
  96. package/dist/_chunks/ru-C94rjPGA.js +0 -86
  97. package/dist/_chunks/ru-C94rjPGA.js.map +0 -1
  98. package/dist/_chunks/ru-VWy-IB7K.mjs +0 -86
  99. package/dist/_chunks/ru-VWy-IB7K.mjs.map +0 -1
  100. package/dist/_chunks/sk-BABEhykl.js +0 -50
  101. package/dist/_chunks/sk-BABEhykl.js.map +0 -1
  102. package/dist/_chunks/sk-B_LIcepm.mjs +0 -50
  103. package/dist/_chunks/sk-B_LIcepm.mjs.map +0 -1
  104. package/dist/_chunks/sv-ABLKOokl.mjs +0 -86
  105. package/dist/_chunks/sv-ABLKOokl.mjs.map +0 -1
  106. package/dist/_chunks/sv-Be43LhA9.js +0 -86
  107. package/dist/_chunks/sv-Be43LhA9.js.map +0 -1
  108. package/dist/_chunks/th-DKyP7ueR.mjs +0 -60
  109. package/dist/_chunks/th-DKyP7ueR.mjs.map +0 -1
  110. package/dist/_chunks/th-DgVhVLhL.js +0 -60
  111. package/dist/_chunks/th-DgVhVLhL.js.map +0 -1
  112. package/dist/_chunks/tr-B_idhkEs.js +0 -85
  113. package/dist/_chunks/tr-B_idhkEs.js.map +0 -1
  114. package/dist/_chunks/tr-qa1Q5UjC.mjs +0 -85
  115. package/dist/_chunks/tr-qa1Q5UjC.mjs.map +0 -1
  116. package/dist/_chunks/uk-BmRqbeQc.mjs +0 -49
  117. package/dist/_chunks/uk-BmRqbeQc.mjs.map +0 -1
  118. package/dist/_chunks/uk-LHOivnhP.js +0 -49
  119. package/dist/_chunks/uk-LHOivnhP.js.map +0 -1
  120. package/dist/_chunks/vi-CdVRdKDw.js +0 -50
  121. package/dist/_chunks/vi-CdVRdKDw.js.map +0 -1
  122. package/dist/_chunks/vi-HW-EdMea.mjs +0 -50
  123. package/dist/_chunks/vi-HW-EdMea.mjs.map +0 -1
  124. package/dist/_chunks/zh-5hKkVPA4.mjs +0 -86
  125. package/dist/_chunks/zh-5hKkVPA4.mjs.map +0 -1
  126. package/dist/_chunks/zh-Cuq8gMnF.js +0 -86
  127. package/dist/_chunks/zh-Cuq8gMnF.js.map +0 -1
  128. package/dist/_chunks/zh-Hans-BHilK-yc.mjs +0 -86
  129. package/dist/_chunks/zh-Hans-BHilK-yc.mjs.map +0 -1
  130. package/dist/_chunks/zh-Hans-GQDMKtY4.js +0 -86
  131. package/dist/_chunks/zh-Hans-GQDMKtY4.js.map +0 -1
  132. package/dist/admin/index.js +0 -4
  133. package/dist/admin/index.js.map +0 -1
  134. package/dist/admin/index.mjs +0 -5
  135. package/dist/admin/index.mjs.map +0 -1
@@ -1,1174 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const jsxRuntime = require("react/jsx-runtime");
4
- const React = require("react");
5
- const admin = require("@strapi/strapi/admin");
6
- const reactRouterDom = require("react-router-dom");
7
- const index = require("./index-7wfWos1T.js");
8
- const designSystem = require("@strapi/design-system");
9
- const icons = require("@strapi/icons");
10
- const formik = require("formik");
11
- const reactIntl = require("react-intl");
12
- const reactQuery = require("react-query");
13
- const PropTypes = require("prop-types");
14
- const upperFirst = require("lodash/upperFirst");
15
- const sortBy = require("lodash/sortBy");
16
- const get = require("lodash/get");
17
- const styled = require("styled-components");
18
- const createNextState2 = require("immer");
19
- const isEmpty = require("lodash/isEmpty");
20
- const without = require("lodash/without");
21
- const map = require("lodash/map");
22
- const tail = require("lodash/tail");
23
- const set = require("lodash/set");
24
- const take = require("lodash/take");
25
- const yup = require("yup");
26
- const v2 = require("@strapi/design-system/v2");
27
- const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
28
- function _interopNamespace(e) {
29
- if (e && e.__esModule)
30
- return e;
31
- const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
32
- if (e) {
33
- for (const k in e) {
34
- if (k !== "default") {
35
- const d = Object.getOwnPropertyDescriptor(e, k);
36
- Object.defineProperty(n, k, d.get ? d : {
37
- enumerable: true,
38
- get: () => e[k]
39
- });
40
- }
41
- }
42
- }
43
- n.default = e;
44
- return Object.freeze(n);
45
- }
46
- const React__namespace = /* @__PURE__ */ _interopNamespace(React);
47
- const PropTypes__default = /* @__PURE__ */ _interopDefault(PropTypes);
48
- const upperFirst__default = /* @__PURE__ */ _interopDefault(upperFirst);
49
- const sortBy__default = /* @__PURE__ */ _interopDefault(sortBy);
50
- const get__default = /* @__PURE__ */ _interopDefault(get);
51
- const styled__default = /* @__PURE__ */ _interopDefault(styled);
52
- const isEmpty__default = /* @__PURE__ */ _interopDefault(isEmpty);
53
- const without__default = /* @__PURE__ */ _interopDefault(without);
54
- const map__default = /* @__PURE__ */ _interopDefault(map);
55
- const tail__default = /* @__PURE__ */ _interopDefault(tail);
56
- const set__default = /* @__PURE__ */ _interopDefault(set);
57
- const take__default = /* @__PURE__ */ _interopDefault(take);
58
- const yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
59
- const UsersPermissions$2 = React.createContext({});
60
- const UsersPermissionsProvider = ({ children, value }) => {
61
- return /* @__PURE__ */ jsxRuntime.jsx(UsersPermissions$2.Provider, { value, children });
62
- };
63
- const useUsersPermissions = () => React.useContext(UsersPermissions$2);
64
- UsersPermissionsProvider.propTypes = {
65
- children: PropTypes__default.default.node.isRequired,
66
- value: PropTypes__default.default.object.isRequired
67
- };
68
- function formatPluginName(pluginSlug) {
69
- switch (pluginSlug) {
70
- case "application":
71
- return "Application";
72
- case "plugin::content-manager":
73
- return "Content manager";
74
- case "plugin::content-type-builder":
75
- return "Content types builder";
76
- case "plugin::documentation":
77
- return "Documentation";
78
- case "plugin::email":
79
- return "Email";
80
- case "plugin::i18n":
81
- return "i18n";
82
- case "plugin::upload":
83
- return "Upload";
84
- case "plugin::users-permissions":
85
- return "Users-permissions";
86
- default:
87
- return upperFirst__default.default(pluginSlug.replace("api::", "").replace("plugin::", ""));
88
- }
89
- }
90
- const init$1 = (initialState2, permissions) => {
91
- const collapses = Object.keys(permissions).sort().map((name) => ({ name, isOpen: false }));
92
- return { ...initialState2, collapses };
93
- };
94
- const activeCheckboxWrapperStyles = styled.css`
95
- background: ${(props) => props.theme.colors.primary100};
96
- svg {
97
- opacity: 1;
98
- }
99
- `;
100
- const CheckboxWrapper = styled__default.default(designSystem.Box)`
101
- display: flex;
102
- justify-content: space-between;
103
- align-items: center;
104
-
105
- svg {
106
- opacity: 0;
107
- path {
108
- fill: ${(props) => props.theme.colors.primary600};
109
- }
110
- }
111
-
112
- /* Show active style both on hover and when the action is selected */
113
- ${(props) => props.isActive && activeCheckboxWrapperStyles}
114
- &:hover {
115
- ${activeCheckboxWrapperStyles}
116
- }
117
- `;
118
- const Border = styled__default.default.div`
119
- flex: 1;
120
- align-self: center;
121
- border-top: 1px solid ${({ theme }) => theme.colors.neutral150};
122
- `;
123
- const SubCategory = ({ subCategory }) => {
124
- const { formatMessage } = reactIntl.useIntl();
125
- const { onChange, onChangeSelectAll, onSelectedAction, selectedAction, modifiedData } = useUsersPermissions();
126
- const currentScopedModifiedData = React.useMemo(() => {
127
- return get__default.default(modifiedData, subCategory.name, {});
128
- }, [modifiedData, subCategory]);
129
- const hasAllActionsSelected = React.useMemo(() => {
130
- return Object.values(currentScopedModifiedData).every((action) => action.enabled === true);
131
- }, [currentScopedModifiedData]);
132
- const hasSomeActionsSelected = React.useMemo(() => {
133
- return Object.values(currentScopedModifiedData).some((action) => action.enabled === true) && !hasAllActionsSelected;
134
- }, [currentScopedModifiedData, hasAllActionsSelected]);
135
- const handleChangeSelectAll = React.useCallback(
136
- ({ target: { name } }) => {
137
- onChangeSelectAll({ target: { name, value: !hasAllActionsSelected } });
138
- },
139
- [hasAllActionsSelected, onChangeSelectAll]
140
- );
141
- const isActionSelected = React.useCallback(
142
- (actionName) => {
143
- return selectedAction === actionName;
144
- },
145
- [selectedAction]
146
- );
147
- return /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Box, { children: [
148
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { justifyContent: "space-between", alignItems: "center", children: [
149
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { paddingRight: 4, children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "sigma", textColor: "neutral600", children: subCategory.label }) }),
150
- /* @__PURE__ */ jsxRuntime.jsx(Border, {}),
151
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { paddingLeft: 4, children: /* @__PURE__ */ jsxRuntime.jsx(
152
- designSystem.Checkbox,
153
- {
154
- name: subCategory.name,
155
- value: hasAllActionsSelected,
156
- onValueChange: (value) => handleChangeSelectAll({ target: { name: subCategory.name, value } }),
157
- indeterminate: hasSomeActionsSelected,
158
- children: formatMessage({ id: "app.utils.select-all", defaultMessage: "Select all" })
159
- }
160
- ) })
161
- ] }),
162
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Flex, { paddingTop: 6, paddingBottom: 6, children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Grid, { gap: 2, style: { flex: 1 }, children: subCategory.actions.map((action) => {
163
- const name = `${action.name}.enabled`;
164
- return /* @__PURE__ */ jsxRuntime.jsx(designSystem.GridItem, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsxs(CheckboxWrapper, { isActive: isActionSelected(action.name), padding: 2, hasRadius: true, children: [
165
- /* @__PURE__ */ jsxRuntime.jsx(
166
- designSystem.Checkbox,
167
- {
168
- value: get__default.default(modifiedData, name, false),
169
- name,
170
- onValueChange: (value) => onChange({ target: { name, value } }),
171
- children: action.label
172
- }
173
- ),
174
- /* @__PURE__ */ jsxRuntime.jsxs(
175
- "button",
176
- {
177
- type: "button",
178
- onClick: () => onSelectedAction(action.name),
179
- style: { display: "inline-flex", alignItems: "center" },
180
- children: [
181
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.VisuallyHidden, { as: "span", children: formatMessage(
182
- {
183
- id: "app.utils.show-bound-route",
184
- defaultMessage: "Show bound route for {route}"
185
- },
186
- {
187
- route: action.name
188
- }
189
- ) }),
190
- /* @__PURE__ */ jsxRuntime.jsx(icons.Cog, {})
191
- ]
192
- }
193
- )
194
- ] }) }, action.name);
195
- }) }) })
196
- ] });
197
- };
198
- SubCategory.propTypes = {
199
- subCategory: PropTypes__default.default.object.isRequired
200
- };
201
- const PermissionRow = ({ name, permissions }) => {
202
- const subCategories = React.useMemo(() => {
203
- return sortBy__default.default(
204
- Object.values(permissions.controllers).reduce((acc, curr, index2) => {
205
- const currentName = `${name}.controllers.${Object.keys(permissions.controllers)[index2]}`;
206
- const actions = sortBy__default.default(
207
- Object.keys(curr).reduce((acc2, current) => {
208
- return [
209
- ...acc2,
210
- {
211
- ...curr[current],
212
- label: current,
213
- name: `${currentName}.${current}`
214
- }
215
- ];
216
- }, []),
217
- "label"
218
- );
219
- return [
220
- ...acc,
221
- {
222
- actions,
223
- label: Object.keys(permissions.controllers)[index2],
224
- name: currentName
225
- }
226
- ];
227
- }, []),
228
- "label"
229
- );
230
- }, [name, permissions]);
231
- return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { padding: 6, children: subCategories.map((subCategory) => /* @__PURE__ */ jsxRuntime.jsx(SubCategory, { subCategory }, subCategory.name)) });
232
- };
233
- PermissionRow.propTypes = {
234
- name: PropTypes__default.default.string.isRequired,
235
- permissions: PropTypes__default.default.object.isRequired
236
- };
237
- const initialState$1 = {
238
- collapses: []
239
- };
240
- const reducer$1 = (state, action) => (
241
- // eslint-disable-next-line consistent-return
242
- createNextState2.produce(state, (draftState) => {
243
- switch (action.type) {
244
- case "TOGGLE_COLLAPSE": {
245
- draftState.collapses = state.collapses.map((collapse, index2) => {
246
- if (index2 === action.index) {
247
- return { ...collapse, isOpen: !collapse.isOpen };
248
- }
249
- return { ...collapse, isOpen: false };
250
- });
251
- break;
252
- }
253
- default:
254
- return draftState;
255
- }
256
- })
257
- );
258
- const Permissions = () => {
259
- const { modifiedData } = useUsersPermissions();
260
- const { formatMessage } = reactIntl.useIntl();
261
- const [{ collapses }, dispatch] = React.useReducer(
262
- reducer$1,
263
- initialState$1,
264
- (state) => init$1(state, modifiedData)
265
- );
266
- const handleToggle = (index2) => dispatch({
267
- type: "TOGGLE_COLLAPSE",
268
- index: index2
269
- });
270
- return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 1, children: collapses.map((collapse, index2) => /* @__PURE__ */ jsxRuntime.jsxs(
271
- designSystem.Accordion,
272
- {
273
- expanded: collapse.isOpen,
274
- onToggle: () => handleToggle(index2),
275
- variant: index2 % 2 === 0 ? "secondary" : void 0,
276
- children: [
277
- /* @__PURE__ */ jsxRuntime.jsx(
278
- designSystem.AccordionToggle,
279
- {
280
- title: formatPluginName(collapse.name),
281
- description: formatMessage(
282
- {
283
- id: "users-permissions.Plugin.permissions.plugins.description",
284
- defaultMessage: "Define all allowed actions for the {name} plugin."
285
- },
286
- { name: collapse.name }
287
- ),
288
- variant: index2 % 2 ? "primary" : "secondary"
289
- }
290
- ),
291
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.AccordionContent, { children: /* @__PURE__ */ jsxRuntime.jsx(PermissionRow, { permissions: modifiedData[collapse.name], name: collapse.name }) })
292
- ]
293
- },
294
- collapse.name
295
- )) });
296
- };
297
- const getMethodColor = (verb) => {
298
- switch (verb) {
299
- case "POST": {
300
- return {
301
- text: "success600",
302
- border: "success200",
303
- background: "success100"
304
- };
305
- }
306
- case "GET": {
307
- return {
308
- text: "secondary600",
309
- border: "secondary200",
310
- background: "secondary100"
311
- };
312
- }
313
- case "PUT": {
314
- return {
315
- text: "warning600",
316
- border: "warning200",
317
- background: "warning100"
318
- };
319
- }
320
- case "DELETE": {
321
- return {
322
- text: "danger600",
323
- border: "danger200",
324
- background: "danger100"
325
- };
326
- }
327
- default: {
328
- return {
329
- text: "neutral600",
330
- border: "neutral200",
331
- background: "neutral100"
332
- };
333
- }
334
- }
335
- };
336
- const MethodBox = styled__default.default(designSystem.Box)`
337
- margin: -1px;
338
- border-radius: ${({ theme }) => theme.spaces[1]} 0 0 ${({ theme }) => theme.spaces[1]};
339
- `;
340
- function BoundRoute({ route }) {
341
- const { formatMessage } = reactIntl.useIntl();
342
- const { method, handler: title, path } = route;
343
- const formattedRoute = path ? tail__default.default(path.split("/")) : [];
344
- const [controller = "", action = ""] = title ? title.split(".") : [];
345
- const colors = getMethodColor(route.method);
346
- return /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 2, children: [
347
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Typography, { variant: "delta", as: "h3", children: [
348
- formatMessage({
349
- id: "users-permissions.BoundRoute.title",
350
- defaultMessage: "Bound route to"
351
- }),
352
- " ",
353
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: controller }),
354
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Typography, { variant: "delta", textColor: "primary600", children: [
355
- ".",
356
- action
357
- ] })
358
- ] }),
359
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { hasRadius: true, background: "neutral0", borderColor: "neutral200", gap: 0, children: [
360
- /* @__PURE__ */ jsxRuntime.jsx(MethodBox, { background: colors.background, borderColor: colors.border, padding: 2, children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { fontWeight: "bold", textColor: colors.text, children: method }) }),
361
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { paddingLeft: 2, paddingRight: 2, children: map__default.default(formattedRoute, (value) => /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Typography, { textColor: value.includes(":") ? "neutral600" : "neutral900", children: [
362
- "/",
363
- value
364
- ] }, value)) })
365
- ] })
366
- ] });
367
- }
368
- BoundRoute.defaultProps = {
369
- route: {
370
- handler: "Nocontroller.error",
371
- method: "GET",
372
- path: "/there-is-no-path"
373
- }
374
- };
375
- BoundRoute.propTypes = {
376
- route: PropTypes__default.default.shape({
377
- handler: PropTypes__default.default.string,
378
- method: PropTypes__default.default.string,
379
- path: PropTypes__default.default.string
380
- })
381
- };
382
- const Policies = () => {
383
- const { formatMessage } = reactIntl.useIntl();
384
- const { selectedAction, routes } = useUsersPermissions();
385
- const path = without__default.default(selectedAction.split("."), "controllers");
386
- const controllerRoutes = get__default.default(routes, path[0]);
387
- const pathResolved = path.slice(1).join(".");
388
- const displayedRoutes = isEmpty__default.default(controllerRoutes) ? [] : controllerRoutes.filter((o) => o.handler.endsWith(pathResolved));
389
- return /* @__PURE__ */ jsxRuntime.jsx(
390
- designSystem.GridItem,
391
- {
392
- col: 5,
393
- background: "neutral150",
394
- paddingTop: 6,
395
- paddingBottom: 6,
396
- paddingLeft: 7,
397
- paddingRight: 7,
398
- style: { minHeight: "100%" },
399
- children: selectedAction ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 2, children: displayedRoutes.map((route, key) => (
400
- // eslint-disable-next-line react/no-array-index-key
401
- /* @__PURE__ */ jsxRuntime.jsx(BoundRoute, { route }, key)
402
- )) }) : /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 2, children: [
403
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "delta", as: "h3", children: formatMessage({
404
- id: "users-permissions.Policies.header.title",
405
- defaultMessage: "Advanced settings"
406
- }) }),
407
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { as: "p", textColor: "neutral600", children: formatMessage({
408
- id: "users-permissions.Policies.header.hint",
409
- defaultMessage: "Select the application's actions or the plugin's actions and click on the cog icon to display the bound route"
410
- }) })
411
- ] })
412
- }
413
- );
414
- };
415
- const init = (state, permissions, routes) => {
416
- return {
417
- ...state,
418
- initialData: permissions,
419
- modifiedData: permissions,
420
- routes
421
- };
422
- };
423
- const initialState = {
424
- initialData: {},
425
- modifiedData: {},
426
- routes: {},
427
- selectedAction: "",
428
- policies: []
429
- };
430
- const reducer = (state, action) => createNextState2.produce(state, (draftState) => {
431
- switch (action.type) {
432
- case "ON_CHANGE": {
433
- const keysLength = action.keys.length;
434
- const isChangingCheckbox = action.keys[keysLength - 1] === "enabled";
435
- if (action.value && isChangingCheckbox) {
436
- const selectedAction = take__default.default(action.keys, keysLength - 1).join(".");
437
- draftState.selectedAction = selectedAction;
438
- }
439
- set__default.default(draftState, ["modifiedData", ...action.keys], action.value);
440
- break;
441
- }
442
- case "ON_CHANGE_SELECT_ALL": {
443
- const pathToValue = ["modifiedData", ...action.keys];
444
- const oldValues = get__default.default(state, pathToValue, {});
445
- const updatedValues = Object.keys(oldValues).reduce((acc, current) => {
446
- acc[current] = { ...oldValues[current], enabled: action.value };
447
- return acc;
448
- }, {});
449
- set__default.default(draftState, pathToValue, updatedValues);
450
- break;
451
- }
452
- case "ON_RESET": {
453
- draftState.modifiedData = state.initialData;
454
- break;
455
- }
456
- case "ON_SUBMIT_SUCCEEDED": {
457
- draftState.initialData = state.modifiedData;
458
- break;
459
- }
460
- case "SELECT_ACTION": {
461
- const { actionToSelect } = action;
462
- draftState.selectedAction = actionToSelect === state.selectedAction ? "" : actionToSelect;
463
- break;
464
- }
465
- default:
466
- return draftState;
467
- }
468
- });
469
- const UsersPermissions = React.forwardRef(({ permissions, routes }, ref) => {
470
- const { formatMessage } = reactIntl.useIntl();
471
- const [state, dispatch] = React.useReducer(
472
- reducer,
473
- initialState,
474
- (state2) => init(state2, permissions, routes)
475
- );
476
- React.useImperativeHandle(ref, () => ({
477
- getPermissions() {
478
- return {
479
- permissions: state.modifiedData
480
- };
481
- },
482
- resetForm() {
483
- dispatch({ type: "ON_RESET" });
484
- },
485
- setFormAfterSubmit() {
486
- dispatch({ type: "ON_SUBMIT_SUCCEEDED" });
487
- }
488
- }));
489
- const handleChange = ({ target: { name, value } }) => dispatch({
490
- type: "ON_CHANGE",
491
- keys: name.split("."),
492
- value: value === "empty__string_value" ? "" : value
493
- });
494
- const handleChangeSelectAll = ({ target: { name, value } }) => dispatch({
495
- type: "ON_CHANGE_SELECT_ALL",
496
- keys: name.split("."),
497
- value
498
- });
499
- const handleSelectedAction = (actionToSelect) => dispatch({
500
- type: "SELECT_ACTION",
501
- actionToSelect
502
- });
503
- const providerValue = {
504
- ...state,
505
- onChange: handleChange,
506
- onChangeSelectAll: handleChangeSelectAll,
507
- onSelectedAction: handleSelectedAction
508
- };
509
- return /* @__PURE__ */ jsxRuntime.jsx(UsersPermissionsProvider, { value: providerValue, children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Grid, { gap: 0, shadow: "filterShadow", hasRadius: true, background: "neutral0", children: [
510
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.GridItem, { col: 7, paddingTop: 6, paddingBottom: 6, paddingLeft: 7, paddingRight: 7, children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 6, children: [
511
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 2, children: [
512
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "delta", as: "h2", children: formatMessage({
513
- id: index.getTrad("Plugins.header.title"),
514
- defaultMessage: "Permissions"
515
- }) }),
516
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { as: "p", textColor: "neutral600", children: formatMessage({
517
- id: index.getTrad("Plugins.header.description"),
518
- defaultMessage: "Only actions bound by a route are listed below."
519
- }) })
520
- ] }),
521
- /* @__PURE__ */ jsxRuntime.jsx(Permissions, {})
522
- ] }) }),
523
- /* @__PURE__ */ jsxRuntime.jsx(Policies, {})
524
- ] }) });
525
- });
526
- UsersPermissions.propTypes = {
527
- permissions: PropTypes__default.default.object.isRequired,
528
- routes: PropTypes__default.default.object.isRequired
529
- };
530
- const UsersPermissions$1 = React.memo(UsersPermissions);
531
- const createRoleSchema = yup__namespace.object().shape({
532
- name: yup__namespace.string().required(admin.translatedErrors.required.id),
533
- description: yup__namespace.string().required(admin.translatedErrors.required.id)
534
- });
535
- const cleanPermissions = (permissions) => Object.keys(permissions).reduce((acc, current) => {
536
- const currentPermission = permissions[current].controllers;
537
- const cleanedControllers = Object.keys(currentPermission).reduce((acc2, curr) => {
538
- if (isEmpty__default.default(currentPermission[curr])) {
539
- return acc2;
540
- }
541
- acc2[curr] = currentPermission[curr];
542
- return acc2;
543
- }, {});
544
- if (isEmpty__default.default(cleanedControllers)) {
545
- return acc;
546
- }
547
- acc[current] = { controllers: cleanedControllers };
548
- return acc;
549
- }, {});
550
- const usePlugins = () => {
551
- const { toggleNotification } = admin.useNotification();
552
- const { get: get2 } = admin.useFetchClient();
553
- const { formatAPIError } = admin.useAPIErrorHandler(index.getTrad);
554
- const [
555
- {
556
- data: permissions,
557
- isLoading: isLoadingPermissions,
558
- error: permissionsError,
559
- refetch: refetchPermissions
560
- },
561
- { data: routes, isLoading: isLoadingRoutes, error: routesError, refetch: refetchRoutes }
562
- ] = reactQuery.useQueries([
563
- {
564
- queryKey: ["users-permissions", "permissions"],
565
- async queryFn() {
566
- const {
567
- data: { permissions: permissions2 }
568
- } = await get2(`/users-permissions/permissions`);
569
- return permissions2;
570
- }
571
- },
572
- {
573
- queryKey: ["users-permissions", "routes"],
574
- async queryFn() {
575
- const {
576
- data: { routes: routes2 }
577
- } = await get2(`/users-permissions/routes`);
578
- return routes2;
579
- }
580
- }
581
- ]);
582
- const refetchQueries = async () => {
583
- await Promise.all([refetchPermissions(), refetchRoutes()]);
584
- };
585
- React.useEffect(() => {
586
- if (permissionsError) {
587
- toggleNotification({
588
- type: "danger",
589
- message: formatAPIError(permissionsError)
590
- });
591
- }
592
- }, [toggleNotification, permissionsError, formatAPIError]);
593
- React.useEffect(() => {
594
- if (routesError) {
595
- toggleNotification({
596
- type: "danger",
597
- message: formatAPIError(routesError)
598
- });
599
- }
600
- }, [toggleNotification, routesError, formatAPIError]);
601
- const isLoading = isLoadingPermissions || isLoadingRoutes;
602
- return {
603
- // TODO: these return values need to be memoized, otherwise
604
- // they will create infinite rendering loops when used as
605
- // effect dependencies
606
- permissions: permissions ? cleanPermissions(permissions) : {},
607
- routes: routes ?? {},
608
- getData: refetchQueries,
609
- isLoading
610
- };
611
- };
612
- const CreatePage = () => {
613
- const { formatMessage } = reactIntl.useIntl();
614
- const { toggleNotification } = admin.useNotification();
615
- const navigate = reactRouterDom.useNavigate();
616
- const { isLoading: isLoadingPlugins, permissions, routes } = usePlugins();
617
- const { trackUsage } = admin.useTracking();
618
- const permissionsRef = React__namespace.useRef();
619
- const { post } = admin.useFetchClient();
620
- const mutation = reactQuery.useMutation((body) => post(`/users-permissions/roles`, body), {
621
- onError() {
622
- toggleNotification({
623
- type: "danger",
624
- message: formatMessage({
625
- id: "notification.error",
626
- defaultMessage: "An error occurred"
627
- })
628
- });
629
- },
630
- onSuccess() {
631
- trackUsage("didCreateRole");
632
- toggleNotification({
633
- type: "success",
634
- message: formatMessage({
635
- id: index.getTrad("Settings.roles.created"),
636
- defaultMessage: "Role created"
637
- })
638
- });
639
- navigate(-1);
640
- }
641
- });
642
- const handleCreateRoleSubmit = async (data) => {
643
- const permissions2 = permissionsRef.current.getPermissions();
644
- await mutation.mutate({ ...data, ...permissions2, users: [] });
645
- };
646
- return /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Main, { children: [
647
- /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Title, { children: formatMessage(
648
- { id: "Settings.PageTitle", defaultMessage: "Settings - {name}" },
649
- { name: "Roles" }
650
- ) }),
651
- /* @__PURE__ */ jsxRuntime.jsx(
652
- formik.Formik,
653
- {
654
- enableReinitialize: true,
655
- initialValues: { name: "", description: "" },
656
- onSubmit: handleCreateRoleSubmit,
657
- validationSchema: createRoleSchema,
658
- children: ({ handleSubmit, values, handleChange, errors }) => /* @__PURE__ */ jsxRuntime.jsxs(formik.Form, { noValidate: true, onSubmit: handleSubmit, children: [
659
- /* @__PURE__ */ jsxRuntime.jsx(
660
- designSystem.HeaderLayout,
661
- {
662
- primaryAction: !isLoadingPlugins && /* @__PURE__ */ jsxRuntime.jsx(designSystem.Button, { type: "submit", loading: mutation.isLoading, startIcon: /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}), children: formatMessage({
663
- id: "global.save",
664
- defaultMessage: "Save"
665
- }) }),
666
- title: formatMessage({
667
- id: "Settings.roles.create.title",
668
- defaultMessage: "Create a role"
669
- }),
670
- subtitle: formatMessage({
671
- id: "Settings.roles.create.description",
672
- defaultMessage: "Define the rights given to the role"
673
- })
674
- }
675
- ),
676
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.ContentLayout, { children: /* @__PURE__ */ jsxRuntime.jsxs(
677
- designSystem.Flex,
678
- {
679
- background: "neutral0",
680
- direction: "column",
681
- alignItems: "stretch",
682
- gap: 7,
683
- hasRadius: true,
684
- paddingTop: 6,
685
- paddingBottom: 6,
686
- paddingLeft: 7,
687
- paddingRight: 7,
688
- shadow: "filterShadow",
689
- children: [
690
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", children: [
691
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "delta", as: "h2", children: formatMessage({
692
- id: index.getTrad("EditPage.form.roles"),
693
- defaultMessage: "Role details"
694
- }) }),
695
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Grid, { gap: 4, children: [
696
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.GridItem, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsx(
697
- designSystem.TextInput,
698
- {
699
- name: "name",
700
- value: values.name || "",
701
- onChange: handleChange,
702
- label: formatMessage({
703
- id: "global.name",
704
- defaultMessage: "Name"
705
- }),
706
- error: errors?.name ? formatMessage({ id: errors.name, defaultMessage: "Name is required" }) : false,
707
- required: true
708
- }
709
- ) }),
710
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.GridItem, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsx(
711
- designSystem.Textarea,
712
- {
713
- id: "description",
714
- value: values.description || "",
715
- onChange: handleChange,
716
- label: formatMessage({
717
- id: "global.description",
718
- defaultMessage: "Description"
719
- }),
720
- error: errors?.description ? formatMessage({
721
- id: errors.description,
722
- defaultMessage: "Description is required"
723
- }) : false,
724
- required: true
725
- }
726
- ) })
727
- ] })
728
- ] }),
729
- !isLoadingPlugins && /* @__PURE__ */ jsxRuntime.jsx(
730
- UsersPermissions$1,
731
- {
732
- ref: permissionsRef,
733
- permissions,
734
- routes
735
- }
736
- )
737
- ]
738
- }
739
- ) })
740
- ] })
741
- }
742
- )
743
- ] });
744
- };
745
- const ProtectedRolesCreatePage = () => /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Protect, { permissions: index.PERMISSIONS.createRole, children: /* @__PURE__ */ jsxRuntime.jsx(CreatePage, {}) });
746
- const EditPage = () => {
747
- const { formatMessage } = reactIntl.useIntl();
748
- const { toggleNotification } = admin.useNotification();
749
- const {
750
- params: { id }
751
- } = reactRouterDom.useMatch(`/settings/users-permissions/roles/:id`);
752
- const { get: get2 } = admin.useFetchClient();
753
- const { isLoading: isLoadingPlugins, routes } = usePlugins();
754
- const {
755
- data: role,
756
- isLoading: isLoadingRole,
757
- refetch: refetchRole
758
- } = reactQuery.useQuery(["users-permissions", "role", id], async () => {
759
- const {
760
- data: { role: role2 }
761
- } = await get2(`/users-permissions/roles/${id}`);
762
- return role2;
763
- });
764
- const permissionsRef = React__namespace.useRef();
765
- const { put } = admin.useFetchClient();
766
- const { formatAPIError } = admin.useAPIErrorHandler();
767
- const mutation = reactQuery.useMutation((body) => put(`/users-permissions/roles/${id}`, body), {
768
- onError(error) {
769
- toggleNotification({
770
- type: "danger",
771
- message: formatAPIError(error)
772
- });
773
- },
774
- async onSuccess() {
775
- toggleNotification({
776
- type: "success",
777
- message: formatMessage({
778
- id: index.getTrad("Settings.roles.created"),
779
- defaultMessage: "Role edited"
780
- })
781
- });
782
- await refetchRole();
783
- }
784
- });
785
- const handleEditRoleSubmit = async (data) => {
786
- const permissions = permissionsRef.current.getPermissions();
787
- await mutation.mutate({ ...data, ...permissions, users: [] });
788
- };
789
- if (isLoadingRole) {
790
- return /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Loading, {});
791
- }
792
- return /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Main, { children: [
793
- /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Title, { children: formatMessage(
794
- { id: "Settings.PageTitle", defaultMessage: "Settings - {name}" },
795
- { name: "Roles" }
796
- ) }),
797
- /* @__PURE__ */ jsxRuntime.jsx(
798
- formik.Formik,
799
- {
800
- enableReinitialize: true,
801
- initialValues: { name: role.name, description: role.description },
802
- onSubmit: handleEditRoleSubmit,
803
- validationSchema: createRoleSchema,
804
- children: ({ handleSubmit, values, handleChange, errors }) => /* @__PURE__ */ jsxRuntime.jsxs(formik.Form, { noValidate: true, onSubmit: handleSubmit, children: [
805
- /* @__PURE__ */ jsxRuntime.jsx(
806
- designSystem.HeaderLayout,
807
- {
808
- primaryAction: !isLoadingPlugins ? /* @__PURE__ */ jsxRuntime.jsx(
809
- designSystem.Button,
810
- {
811
- disabled: role.code === "strapi-super-admin",
812
- type: "submit",
813
- loading: mutation.isLoading,
814
- startIcon: /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}),
815
- children: formatMessage({
816
- id: "global.save",
817
- defaultMessage: "Save"
818
- })
819
- }
820
- ) : null,
821
- title: role.name,
822
- subtitle: role.description,
823
- navigationAction: /* @__PURE__ */ jsxRuntime.jsx(admin.BackButton, {})
824
- }
825
- ),
826
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.ContentLayout, { children: /* @__PURE__ */ jsxRuntime.jsxs(
827
- designSystem.Flex,
828
- {
829
- background: "neutral0",
830
- direction: "column",
831
- alignItems: "stretch",
832
- gap: 7,
833
- hasRadius: true,
834
- paddingTop: 6,
835
- paddingBottom: 6,
836
- paddingLeft: 7,
837
- paddingRight: 7,
838
- shadow: "filterShadow",
839
- children: [
840
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 4, children: [
841
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "delta", as: "h2", children: formatMessage({
842
- id: index.getTrad("EditPage.form.roles"),
843
- defaultMessage: "Role details"
844
- }) }),
845
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Grid, { gap: 4, children: [
846
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.GridItem, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsx(
847
- designSystem.TextInput,
848
- {
849
- name: "name",
850
- value: values.name || "",
851
- onChange: handleChange,
852
- label: formatMessage({
853
- id: "global.name",
854
- defaultMessage: "Name"
855
- }),
856
- error: errors?.name ? formatMessage({ id: errors.name, defaultMessage: "Name is required" }) : false,
857
- required: true
858
- }
859
- ) }),
860
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.GridItem, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsx(
861
- designSystem.Textarea,
862
- {
863
- id: "description",
864
- value: values.description || "",
865
- onChange: handleChange,
866
- label: formatMessage({
867
- id: "global.description",
868
- defaultMessage: "Description"
869
- }),
870
- error: errors?.description ? formatMessage({
871
- id: errors.description,
872
- defaultMessage: "Description is required"
873
- }) : false,
874
- required: true
875
- }
876
- ) })
877
- ] })
878
- ] }),
879
- !isLoadingPlugins && /* @__PURE__ */ jsxRuntime.jsx(
880
- UsersPermissions$1,
881
- {
882
- ref: permissionsRef,
883
- permissions: role.permissions,
884
- routes
885
- }
886
- )
887
- ]
888
- }
889
- ) })
890
- ] })
891
- }
892
- )
893
- ] });
894
- };
895
- const ProtectedRolesEditPage = () => /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Protect, { permissions: index.PERMISSIONS.updateRole, children: /* @__PURE__ */ jsxRuntime.jsx(EditPage, {}) });
896
- const EditLink = styled__default.default(designSystem.Link)`
897
- align-items: center;
898
- height: ${32 / 16}rem;
899
- display: flex;
900
- justify-content: center;
901
- padding: ${({ theme }) => `${theme.spaces[2]}}`};
902
- width: ${32 / 16}rem;
903
-
904
- svg {
905
- height: ${12 / 16}rem;
906
- width: ${12 / 16}rem;
907
-
908
- path {
909
- fill: ${({ theme }) => theme.colors.neutral500};
910
- }
911
- }
912
-
913
- &:hover,
914
- &:focus {
915
- svg {
916
- path {
917
- fill: ${({ theme }) => theme.colors.neutral800};
918
- }
919
- }
920
- }
921
- `;
922
- const TableBody = ({ sortedRoles, canDelete, canUpdate, setRoleToDelete, onDelete }) => {
923
- const { formatMessage } = reactIntl.useIntl();
924
- const navigate = reactRouterDom.useNavigate();
925
- const [showConfirmDelete, setShowConfirmDelete] = onDelete;
926
- const checkCanDeleteRole = (role) => canDelete && !["public", "authenticated"].includes(role.type);
927
- const handleClickDelete = (id) => {
928
- setRoleToDelete(id);
929
- setShowConfirmDelete(!showConfirmDelete);
930
- };
931
- return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Tbody, { children: sortedRoles?.map((role) => /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Tr, { onClick: () => navigate(role.id.toString()), children: [
932
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Td, { width: "20%", children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { children: role.name }) }),
933
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Td, { width: "50%", children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { children: role.description }) }),
934
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Td, { width: "30%", children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { children: formatMessage(
935
- {
936
- id: "Roles.RoleRow.user-count",
937
- defaultMessage: "{number, plural, =0 {# user} one {# user} other {# users}}"
938
- },
939
- { number: role.nb_users }
940
- ) }) }),
941
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Td, { children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { justifyContent: "end", onClick: (e) => e.stopPropagation(), children: [
942
- canUpdate ? /* @__PURE__ */ jsxRuntime.jsx(
943
- EditLink,
944
- {
945
- to: role.id.toString(),
946
- "aria-label": formatMessage(
947
- { id: "app.component.table.edit", defaultMessage: "Edit {target}" },
948
- { target: `${role.name}` }
949
- ),
950
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.Pencil, {})
951
- }
952
- ) : null,
953
- checkCanDeleteRole(role) && /* @__PURE__ */ jsxRuntime.jsx(
954
- designSystem.IconButton,
955
- {
956
- onClick: () => handleClickDelete(role.id.toString()),
957
- noBorder: true,
958
- icon: /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, {}),
959
- label: formatMessage(
960
- { id: "global.delete-target", defaultMessage: "Delete {target}" },
961
- { target: `${role.name}` }
962
- )
963
- }
964
- )
965
- ] }) })
966
- ] }, role.name)) });
967
- };
968
- TableBody.defaultProps = {
969
- canDelete: false,
970
- canUpdate: false
971
- };
972
- TableBody.propTypes = {
973
- onDelete: PropTypes__default.default.array.isRequired,
974
- permissions: PropTypes__default.default.object.isRequired,
975
- setRoleToDelete: PropTypes__default.default.func.isRequired,
976
- sortedRoles: PropTypes__default.default.array.isRequired,
977
- canDelete: PropTypes__default.default.bool,
978
- canUpdate: PropTypes__default.default.bool
979
- };
980
- const RolesListPage = () => {
981
- const { trackUsage } = admin.useTracking();
982
- const { formatMessage, locale } = reactIntl.useIntl();
983
- const { toggleNotification } = admin.useNotification();
984
- const { notifyStatus } = designSystem.useNotifyAT();
985
- const [{ query }] = admin.useQueryParams();
986
- const _q = query?._q || "";
987
- const [showConfirmDelete, setShowConfirmDelete] = React.useState(false);
988
- const [roleToDelete, setRoleToDelete] = React.useState();
989
- const { del, get: get2 } = admin.useFetchClient();
990
- const {
991
- isLoading: isLoadingForPermissions,
992
- allowedActions: { canRead, canDelete, canCreate, canUpdate }
993
- } = admin.useRBAC({
994
- create: index.PERMISSIONS.createRole,
995
- read: index.PERMISSIONS.readRoles,
996
- update: index.PERMISSIONS.updateRole,
997
- delete: index.PERMISSIONS.deleteRole
998
- });
999
- const {
1000
- isLoading: isLoadingForData,
1001
- data: { roles },
1002
- isFetching,
1003
- refetch
1004
- } = reactQuery.useQuery("get-roles", () => fetchData(toggleNotification, formatMessage, notifyStatus), {
1005
- initialData: {},
1006
- enabled: canRead
1007
- });
1008
- const { contains } = designSystem.useFilter(locale, {
1009
- sensitivity: "base"
1010
- });
1011
- const formatter = designSystem.useCollator(locale, {
1012
- sensitivity: "base"
1013
- });
1014
- const isLoading = isLoadingForData || isFetching || isLoadingForPermissions;
1015
- const handleShowConfirmDelete = () => {
1016
- setShowConfirmDelete(!showConfirmDelete);
1017
- };
1018
- const deleteData = async (id, formatMessage2, toggleNotification2) => {
1019
- try {
1020
- await del(`/users-permissions/roles/${id}`);
1021
- } catch (error) {
1022
- toggleNotification2({
1023
- type: "danger",
1024
- message: formatMessage2({ id: "notification.error", defaultMessage: "An error occured" })
1025
- });
1026
- }
1027
- };
1028
- const fetchData = async (toggleNotification2, formatMessage2, notifyStatus2) => {
1029
- try {
1030
- const { data } = await get2("/users-permissions/roles");
1031
- notifyStatus2("The roles have loaded successfully");
1032
- return data;
1033
- } catch (err) {
1034
- toggleNotification2({
1035
- type: "danger",
1036
- message: formatMessage2({ id: "notification.error", defaultMessage: "An error occurred" })
1037
- });
1038
- throw new Error(err);
1039
- }
1040
- };
1041
- const emptyLayout = {
1042
- roles: {
1043
- id: index.getTrad("Roles.empty"),
1044
- defaultMessage: "You don't have any roles yet."
1045
- },
1046
- search: {
1047
- id: index.getTrad("Roles.empty.search"),
1048
- defaultMessage: "No roles match the search."
1049
- }
1050
- };
1051
- const pageTitle = formatMessage({
1052
- id: "global.roles",
1053
- defaultMessage: "Roles"
1054
- });
1055
- const deleteMutation = reactQuery.useMutation((id) => deleteData(id, formatMessage, toggleNotification), {
1056
- async onSuccess() {
1057
- await refetch();
1058
- }
1059
- });
1060
- const handleConfirmDelete = async () => {
1061
- await deleteMutation.mutateAsync(roleToDelete);
1062
- setShowConfirmDelete(!showConfirmDelete);
1063
- };
1064
- const sortedRoles = (roles || []).filter((role) => contains(role.name, _q) || contains(role.description, _q)).sort(
1065
- (a, b) => formatter.compare(a.name, b.name) || formatter.compare(a.description, b.description)
1066
- );
1067
- const emptyContent = _q && !sortedRoles.length ? "search" : "roles";
1068
- const colCount = 4;
1069
- const rowCount = (roles?.length || 0) + 1;
1070
- if (isLoading) {
1071
- return /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Loading, {});
1072
- }
1073
- return /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Layout, { children: [
1074
- /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Title, { children: formatMessage(
1075
- { id: "Settings.PageTitle", defaultMessage: "Settings - {name}" },
1076
- { name: pageTitle }
1077
- ) }),
1078
- /* @__PURE__ */ jsxRuntime.jsxs(admin.Page.Main, { children: [
1079
- /* @__PURE__ */ jsxRuntime.jsx(
1080
- designSystem.HeaderLayout,
1081
- {
1082
- title: formatMessage({
1083
- id: "global.roles",
1084
- defaultMessage: "Roles"
1085
- }),
1086
- subtitle: formatMessage({
1087
- id: "Settings.roles.list.description",
1088
- defaultMessage: "List of roles"
1089
- }),
1090
- primaryAction: canCreate ? /* @__PURE__ */ jsxRuntime.jsx(
1091
- v2.LinkButton,
1092
- {
1093
- to: "new",
1094
- as: reactRouterDom.NavLink,
1095
- onClick: () => trackUsage("willCreateRole"),
1096
- startIcon: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
1097
- size: "S",
1098
- children: formatMessage({
1099
- id: index.getTrad("List.button.roles"),
1100
- defaultMessage: "Add new role"
1101
- })
1102
- }
1103
- ) : null,
1104
- navigationAction: /* @__PURE__ */ jsxRuntime.jsx(admin.BackButton, {})
1105
- }
1106
- ),
1107
- /* @__PURE__ */ jsxRuntime.jsx(
1108
- designSystem.ActionLayout,
1109
- {
1110
- startActions: /* @__PURE__ */ jsxRuntime.jsx(
1111
- admin.SearchInput,
1112
- {
1113
- label: formatMessage({
1114
- id: "app.component.search.label",
1115
- defaultMessage: "Search"
1116
- })
1117
- }
1118
- )
1119
- }
1120
- ),
1121
- /* @__PURE__ */ jsxRuntime.jsxs(designSystem.ContentLayout, { children: [
1122
- !canRead && /* @__PURE__ */ jsxRuntime.jsx(admin.Page.NoPermissions, {}),
1123
- canRead && sortedRoles && sortedRoles?.length ? /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Table, { colCount, rowCount, children: [
1124
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Thead, { children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Tr, { children: [
1125
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Th, { children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "sigma", textColor: "neutral600", children: formatMessage({ id: "global.name", defaultMessage: "Name" }) }) }),
1126
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Th, { children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "sigma", textColor: "neutral600", children: formatMessage({
1127
- id: "global.description",
1128
- defaultMessage: "Description"
1129
- }) }) }),
1130
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Th, { children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "sigma", textColor: "neutral600", children: formatMessage({
1131
- id: "global.users",
1132
- defaultMessage: "Users"
1133
- }) }) }),
1134
- /* @__PURE__ */ jsxRuntime.jsx(designSystem.Th, { children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.VisuallyHidden, { children: formatMessage({
1135
- id: "global.actions",
1136
- defaultMessage: "Actions"
1137
- }) }) })
1138
- ] }) }),
1139
- /* @__PURE__ */ jsxRuntime.jsx(
1140
- TableBody,
1141
- {
1142
- sortedRoles,
1143
- canDelete,
1144
- canUpdate,
1145
- permissions: index.PERMISSIONS,
1146
- setRoleToDelete,
1147
- onDelete: [showConfirmDelete, setShowConfirmDelete]
1148
- }
1149
- )
1150
- ] }) : /* @__PURE__ */ jsxRuntime.jsx(designSystem.EmptyStateLayout, { content: formatMessage(emptyLayout[emptyContent]) })
1151
- ] }),
1152
- /* @__PURE__ */ jsxRuntime.jsx(
1153
- admin.ConfirmDialog,
1154
- {
1155
- onConfirm: handleConfirmDelete,
1156
- onClose: handleShowConfirmDelete,
1157
- isOpen: showConfirmDelete
1158
- }
1159
- )
1160
- ] })
1161
- ] });
1162
- };
1163
- const ProtectedRolesListPage = () => {
1164
- return /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Protect, { permissions: index.PERMISSIONS.accessRoles, children: /* @__PURE__ */ jsxRuntime.jsx(RolesListPage, {}) });
1165
- };
1166
- const Roles = () => {
1167
- return /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Protect, { permissions: index.PERMISSIONS.accessRoles, children: /* @__PURE__ */ jsxRuntime.jsxs(reactRouterDom.Routes, { children: [
1168
- /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Route, { index: true, element: /* @__PURE__ */ jsxRuntime.jsx(ProtectedRolesListPage, {}) }),
1169
- /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Route, { path: "new", element: /* @__PURE__ */ jsxRuntime.jsx(ProtectedRolesCreatePage, {}) }),
1170
- /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Route, { path: ":id", element: /* @__PURE__ */ jsxRuntime.jsx(ProtectedRolesEditPage, {}) })
1171
- ] }) });
1172
- };
1173
- exports.default = Roles;
1174
- //# sourceMappingURL=index-BQQx9fm3.js.map