@akemona-org/strapi-plugin-users-permissions 3.7.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.
Files changed (143) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +19 -0
  3. package/admin/src/assets/images/logo.svg +1 -0
  4. package/admin/src/components/BaselineAlignement/index.js +33 -0
  5. package/admin/src/components/Bloc/index.js +10 -0
  6. package/admin/src/components/BoundRoute/Components.js +78 -0
  7. package/admin/src/components/BoundRoute/index.js +56 -0
  8. package/admin/src/components/ContainerFluid/index.js +13 -0
  9. package/admin/src/components/FormBloc/index.js +61 -0
  10. package/admin/src/components/IntlInput/index.js +38 -0
  11. package/admin/src/components/ListBaselineAlignment/index.js +8 -0
  12. package/admin/src/components/ListRow/Components.js +74 -0
  13. package/admin/src/components/ListRow/index.js +35 -0
  14. package/admin/src/components/ModalForm/Wrapper.js +12 -0
  15. package/admin/src/components/ModalForm/index.js +59 -0
  16. package/admin/src/components/Permissions/ListWrapper.js +9 -0
  17. package/admin/src/components/Permissions/PermissionRow/BaselineAlignment.js +7 -0
  18. package/admin/src/components/Permissions/PermissionRow/CheckboxWrapper.js +37 -0
  19. package/admin/src/components/Permissions/PermissionRow/RowStyle.js +28 -0
  20. package/admin/src/components/Permissions/PermissionRow/SubCategory/ConditionsButtonWrapper.js +13 -0
  21. package/admin/src/components/Permissions/PermissionRow/SubCategory/PolicyWrapper.js +8 -0
  22. package/admin/src/components/Permissions/PermissionRow/SubCategory/SubCategoryWrapper.js +26 -0
  23. package/admin/src/components/Permissions/PermissionRow/SubCategory/index.js +116 -0
  24. package/admin/src/components/Permissions/PermissionRow/index.js +92 -0
  25. package/admin/src/components/Permissions/index.js +44 -0
  26. package/admin/src/components/Permissions/init.js +14 -0
  27. package/admin/src/components/Permissions/reducer.js +27 -0
  28. package/admin/src/components/Policies/Components.js +26 -0
  29. package/admin/src/components/Policies/index.js +61 -0
  30. package/admin/src/components/PrefixedIcon/index.js +27 -0
  31. package/admin/src/components/Roles/EmptyRole/BaselineAlignment.js +7 -0
  32. package/admin/src/components/Roles/EmptyRole/index.js +27 -0
  33. package/admin/src/components/Roles/RoleListWrapper/index.js +17 -0
  34. package/admin/src/components/Roles/RoleRow/RoleDescription.js +9 -0
  35. package/admin/src/components/Roles/RoleRow/index.js +45 -0
  36. package/admin/src/components/Roles/index.js +3 -0
  37. package/admin/src/components/SizedInput/index.js +24 -0
  38. package/admin/src/components/UsersPermissions/index.js +91 -0
  39. package/admin/src/components/UsersPermissions/init.js +11 -0
  40. package/admin/src/components/UsersPermissions/reducer.js +60 -0
  41. package/admin/src/containers/AdvancedSettings/index.js +218 -0
  42. package/admin/src/containers/AdvancedSettings/reducer.js +65 -0
  43. package/admin/src/containers/AdvancedSettings/utils/form.js +52 -0
  44. package/admin/src/containers/EmailTemplates/CustomTextInput.js +105 -0
  45. package/admin/src/containers/EmailTemplates/Wrapper.js +36 -0
  46. package/admin/src/containers/EmailTemplates/index.js +222 -0
  47. package/admin/src/containers/EmailTemplates/reducer.js +58 -0
  48. package/admin/src/containers/EmailTemplates/utils/forms.js +81 -0
  49. package/admin/src/containers/EmailTemplates/utils/schema.js +25 -0
  50. package/admin/src/containers/Providers/index.js +283 -0
  51. package/admin/src/containers/Providers/reducer.js +54 -0
  52. package/admin/src/containers/Providers/utils/createProvidersArray.js +21 -0
  53. package/admin/src/containers/Providers/utils/forms.js +205 -0
  54. package/admin/src/containers/Roles/CreatePage/index.js +167 -0
  55. package/admin/src/containers/Roles/CreatePage/utils/schema.js +9 -0
  56. package/admin/src/containers/Roles/EditPage/index.js +161 -0
  57. package/admin/src/containers/Roles/EditPage/utils/schema.js +9 -0
  58. package/admin/src/containers/Roles/ListPage/BaselineAlignment.js +8 -0
  59. package/admin/src/containers/Roles/ListPage/index.js +188 -0
  60. package/admin/src/containers/Roles/ProtectedCreatePage/index.js +12 -0
  61. package/admin/src/containers/Roles/ProtectedEditPage/index.js +12 -0
  62. package/admin/src/containers/Roles/ProtectedListPage/index.js +15 -0
  63. package/admin/src/containers/Roles/index.js +35 -0
  64. package/admin/src/contexts/EditPage/index.js +26 -0
  65. package/admin/src/contexts/HomePage/index.js +27 -0
  66. package/admin/src/contexts/UsersPermissionsContext/index.js +17 -0
  67. package/admin/src/hooks/index.js +5 -0
  68. package/admin/src/hooks/useFetchRole/index.js +55 -0
  69. package/admin/src/hooks/useFetchRole/reducer.js +31 -0
  70. package/admin/src/hooks/useForm/index.js +96 -0
  71. package/admin/src/hooks/useForm/reducer.js +59 -0
  72. package/admin/src/hooks/usePlugins/index.js +73 -0
  73. package/admin/src/hooks/usePlugins/init.js +5 -0
  74. package/admin/src/hooks/usePlugins/reducer.js +37 -0
  75. package/admin/src/hooks/useRolesList/index.js +62 -0
  76. package/admin/src/hooks/useRolesList/init.js +5 -0
  77. package/admin/src/hooks/useRolesList/reducer.js +31 -0
  78. package/admin/src/index.js +109 -0
  79. package/admin/src/permissions.js +33 -0
  80. package/admin/src/pluginId.js +5 -0
  81. package/admin/src/translations/ar.json +49 -0
  82. package/admin/src/translations/cs.json +55 -0
  83. package/admin/src/translations/de.json +68 -0
  84. package/admin/src/translations/dk.json +116 -0
  85. package/admin/src/translations/en.json +104 -0
  86. package/admin/src/translations/es.json +70 -0
  87. package/admin/src/translations/fr.json +55 -0
  88. package/admin/src/translations/id.json +69 -0
  89. package/admin/src/translations/index.js +55 -0
  90. package/admin/src/translations/it.json +68 -0
  91. package/admin/src/translations/ja.json +53 -0
  92. package/admin/src/translations/ko.json +55 -0
  93. package/admin/src/translations/ms.json +54 -0
  94. package/admin/src/translations/nl.json +53 -0
  95. package/admin/src/translations/pl.json +55 -0
  96. package/admin/src/translations/pt-BR.json +49 -0
  97. package/admin/src/translations/pt.json +53 -0
  98. package/admin/src/translations/ru.json +68 -0
  99. package/admin/src/translations/sk.json +57 -0
  100. package/admin/src/translations/sv.json +68 -0
  101. package/admin/src/translations/th.json +66 -0
  102. package/admin/src/translations/tr.json +53 -0
  103. package/admin/src/translations/uk.json +54 -0
  104. package/admin/src/translations/vi.json +55 -0
  105. package/admin/src/translations/zh-Hans.json +104 -0
  106. package/admin/src/translations/zh.json +53 -0
  107. package/admin/src/utils/cleanPermissions.js +25 -0
  108. package/admin/src/utils/formatPolicies.js +8 -0
  109. package/admin/src/utils/getRequestURL.js +5 -0
  110. package/admin/src/utils/getTrad.js +5 -0
  111. package/admin/src/utils/index.js +4 -0
  112. package/config/functions/bootstrap.js +234 -0
  113. package/config/layout.js +10 -0
  114. package/config/policies/isAuthenticated.js +9 -0
  115. package/config/policies/permissions.js +93 -0
  116. package/config/policies/rateLimit.js +33 -0
  117. package/config/request.json +6 -0
  118. package/config/routes.json +397 -0
  119. package/config/schema.graphql.js +280 -0
  120. package/config/security.json +5 -0
  121. package/config/users-permissions-actions.js +80 -0
  122. package/controllers/Auth.js +612 -0
  123. package/controllers/User.js +125 -0
  124. package/controllers/UsersPermissions.js +291 -0
  125. package/controllers/user/admin.js +224 -0
  126. package/controllers/user/api.js +173 -0
  127. package/controllers/validation/email-template.js +40 -0
  128. package/documentation/1.0.0/overrides/users-permissions-Role.json +281 -0
  129. package/documentation/1.0.0/overrides/users-permissions-User.json +325 -0
  130. package/middlewares/users-permissions/defaults.json +5 -0
  131. package/middlewares/users-permissions/index.js +40 -0
  132. package/models/Permission.js +7 -0
  133. package/models/Permission.settings.json +43 -0
  134. package/models/Role.js +7 -0
  135. package/models/Role.settings.json +42 -0
  136. package/models/User.config.js +15 -0
  137. package/models/User.js +7 -0
  138. package/models/User.settings.json +62 -0
  139. package/package.json +70 -0
  140. package/services/Jwt.js +65 -0
  141. package/services/Providers.js +596 -0
  142. package/services/User.js +167 -0
  143. package/services/UsersPermissions.js +416 -0
@@ -0,0 +1,54 @@
1
+ import produce from 'immer';
2
+ import { set } from 'lodash';
3
+
4
+ const initialState = {
5
+ formErrors: {},
6
+ isLoading: true,
7
+ initialData: {},
8
+ modifiedData: {},
9
+ };
10
+
11
+ const reducer = (state, action) =>
12
+ // eslint-disable-next-line consistent-return
13
+ produce(state, draftState => {
14
+ switch (action.type) {
15
+ case 'GET_DATA': {
16
+ draftState.isLoading = true;
17
+ draftState.initialData = {};
18
+ draftState.modifiedData = {};
19
+
20
+ break;
21
+ }
22
+
23
+ case 'GET_DATA_SUCCEEDED': {
24
+ draftState.isLoading = false;
25
+ draftState.initialData = action.data;
26
+ draftState.modifiedData = action.data;
27
+
28
+ break;
29
+ }
30
+ case 'GET_DATA_ERROR': {
31
+ draftState.isLoading = true;
32
+ break;
33
+ }
34
+ case 'ON_CHANGE': {
35
+ set(draftState, ['modifiedData', ...action.keys.split('.')], action.value);
36
+ break;
37
+ }
38
+ case 'RESET_FORM': {
39
+ draftState.modifiedData = state.initialData;
40
+ draftState.formErrors = {};
41
+ break;
42
+ }
43
+ case 'SET_ERRORS': {
44
+ draftState.formErrors = action.errors;
45
+ break;
46
+ }
47
+ default: {
48
+ return draftState;
49
+ }
50
+ }
51
+ });
52
+
53
+ export default reducer;
54
+ export { initialState };
@@ -0,0 +1,21 @@
1
+ import { sortBy } from 'lodash';
2
+
3
+ const createProvidersArray = data => {
4
+ return sortBy(
5
+ Object.keys(data).reduce((acc, current) => {
6
+ const { icon: iconName, enabled, subdomain } = data[current];
7
+ const icon = iconName === 'envelope' ? ['fas', 'envelope'] : ['fab', iconName];
8
+
9
+ if (subdomain !== undefined) {
10
+ acc.push({ name: current, icon, enabled, subdomain });
11
+ } else {
12
+ acc.push({ name: current, icon, enabled });
13
+ }
14
+
15
+ return acc;
16
+ }, []),
17
+ 'name'
18
+ );
19
+ };
20
+
21
+ export default createProvidersArray;
@@ -0,0 +1,205 @@
1
+ import * as yup from 'yup';
2
+ import { translatedErrors } from 'strapi-helper-plugin';
3
+
4
+ import { getTrad } from '../../../utils';
5
+
6
+ const forms = {
7
+ email: {
8
+ form: [
9
+ {
10
+ autoFocus: true,
11
+ label: getTrad('PopUpForm.Providers.enabled.label'),
12
+ name: 'enabled',
13
+ type: 'bool',
14
+ description: getTrad('PopUpForm.Providers.enabled.description'),
15
+ size: { xs: 6 },
16
+ validations: {
17
+ required: true,
18
+ },
19
+ },
20
+ ],
21
+ schema: yup.object().shape({
22
+ enabled: yup.bool().required(translatedErrors.required),
23
+ }),
24
+ },
25
+ providers: {
26
+ form: [
27
+ {
28
+ autoFocus: true,
29
+ label: getTrad('PopUpForm.Providers.enabled.label'),
30
+ name: 'enabled',
31
+ type: 'bool',
32
+ description: getTrad('PopUpForm.Providers.enabled.description'),
33
+ size: { xs: 6 },
34
+ validations: {
35
+ required: true,
36
+ },
37
+ },
38
+ {
39
+ autoFocus: false,
40
+ label: getTrad('PopUpForm.Providers.key.label'),
41
+ name: 'key',
42
+ type: 'text',
43
+ placeholder: getTrad('PopUpForm.Providers.key.placeholder'),
44
+ size: { xs: 12 },
45
+ validations: {
46
+ required: true,
47
+ },
48
+ },
49
+ {
50
+ autoFocus: false,
51
+ label: getTrad('PopUpForm.Providers.secret.label'),
52
+ name: 'secret',
53
+ type: 'text',
54
+ placeholder: getTrad('PopUpForm.Providers.secret.placeholder'),
55
+ size: { xs: 12 },
56
+ validations: {
57
+ required: true,
58
+ },
59
+ },
60
+ {
61
+ autoFocus: false,
62
+ label: getTrad('PopUpForm.Providers.redirectURL.front-end.label'),
63
+ placeholder: 'http://www.client-app.com',
64
+ name: 'callback',
65
+ type: 'text',
66
+ size: { xs: 12 },
67
+ validations: {
68
+ required: true,
69
+ },
70
+ },
71
+ {
72
+ label: {
73
+ id: getTrad('PopUpForm.Providers.redirectURL.label'),
74
+ params: {
75
+ provider: 'VK',
76
+ },
77
+ },
78
+ name: 'noName',
79
+ type: 'text',
80
+ validations: {},
81
+ size: {
82
+ xs: 12,
83
+ },
84
+ disabled: true,
85
+ },
86
+ ],
87
+ schema: yup.object().shape({
88
+ enabled: yup.bool().required(translatedErrors.required),
89
+ key: yup.string().when('enabled', {
90
+ is: true,
91
+ then: yup.string().required(translatedErrors.required),
92
+ otherwise: yup.string(),
93
+ }),
94
+ secret: yup.string().when('enabled', {
95
+ is: true,
96
+ then: yup.string().required(translatedErrors.required),
97
+ otherwise: yup.string(),
98
+ }),
99
+ callback: yup.string().when('enabled', {
100
+ is: true,
101
+ then: yup.string().required(translatedErrors.required),
102
+ otherwise: yup.string(),
103
+ }),
104
+ }),
105
+ },
106
+ providersWithSubdomain: {
107
+ form: [
108
+ {
109
+ autoFocus: true,
110
+ label: getTrad('PopUpForm.Providers.enabled.label'),
111
+ name: 'enabled',
112
+ type: 'bool',
113
+ description: getTrad('PopUpForm.Providers.enabled.description'),
114
+ size: { xs: 6 },
115
+ validations: {
116
+ required: true,
117
+ },
118
+ },
119
+ {
120
+ autoFocus: false,
121
+ label: getTrad('PopUpForm.Providers.key.label'),
122
+ name: 'key',
123
+ type: 'text',
124
+ placeholder: getTrad('PopUpForm.Providers.key.placeholder'),
125
+ size: { xs: 12 },
126
+ validations: {
127
+ required: true,
128
+ },
129
+ },
130
+ {
131
+ autoFocus: false,
132
+ label: getTrad('PopUpForm.Providers.secret.label'),
133
+ name: 'secret',
134
+ type: 'text',
135
+ placeholder: getTrad('PopUpForm.Providers.secret.placeholder'),
136
+ size: { xs: 12 },
137
+ validations: {
138
+ required: true,
139
+ },
140
+ },
141
+ {
142
+ autoFocus: false,
143
+ label: getTrad('PopUpForm.Providers.subdomain.label'),
144
+ name: 'subdomain',
145
+ type: 'text',
146
+ placeholder: getTrad('PopUpForm.Providers.subdomain.placeholder'),
147
+ size: { xs: 12 },
148
+ validations: {
149
+ required: true,
150
+ },
151
+ },
152
+ {
153
+ autoFocus: false,
154
+ label: getTrad('PopUpForm.Providers.redirectURL.front-end.label'),
155
+ placeholder: 'http://www.client-app.com',
156
+ name: 'callback',
157
+ type: 'text',
158
+ size: { xs: 12 },
159
+ validations: {
160
+ required: true,
161
+ },
162
+ },
163
+ {
164
+ label: {
165
+ id: getTrad('PopUpForm.Providers.redirectURL.label'),
166
+ params: {
167
+ provider: 'VK',
168
+ },
169
+ },
170
+ name: 'noName',
171
+ type: 'text',
172
+ validations: {},
173
+ size: {
174
+ xs: 12,
175
+ },
176
+ disabled: true,
177
+ },
178
+ ],
179
+ schema: yup.object().shape({
180
+ enabled: yup.bool().required(translatedErrors.required),
181
+ key: yup.string().when('enabled', {
182
+ is: true,
183
+ then: yup.string().required(translatedErrors.required),
184
+ otherwise: yup.string(),
185
+ }),
186
+ secret: yup.string().when('enabled', {
187
+ is: true,
188
+ then: yup.string().required(translatedErrors.required),
189
+ otherwise: yup.string(),
190
+ }),
191
+ subdomain: yup.string().when('enabled', {
192
+ is: true,
193
+ then: yup.string().required(translatedErrors.required),
194
+ otherwise: yup.string(),
195
+ }),
196
+ callback: yup.string().when('enabled', {
197
+ is: true,
198
+ then: yup.string().required(translatedErrors.required),
199
+ otherwise: yup.string(),
200
+ }),
201
+ }),
202
+ },
203
+ };
204
+
205
+ export default forms;
@@ -0,0 +1,167 @@
1
+ import React, { useState, useRef } from 'react';
2
+ import { useHistory } from 'react-router-dom';
3
+ import { Header } from '@buffetjs/custom';
4
+ import { Padded } from '@buffetjs/core';
5
+ import { Formik } from 'formik';
6
+ import { useIntl } from 'react-intl';
7
+ import { request, useGlobalContext } from 'strapi-helper-plugin';
8
+ import BaselineAlignement from '../../../components/BaselineAlignement';
9
+ import ContainerFluid from '../../../components/ContainerFluid';
10
+ import FormCard from '../../../components/FormBloc';
11
+ import SizedInput from '../../../components/SizedInput';
12
+ import getTrad from '../../../utils/getTrad';
13
+ import pluginId from '../../../pluginId';
14
+ import UsersPermissions from '../../../components/UsersPermissions';
15
+ import { usePlugins } from '../../../hooks';
16
+ import schema from './utils/schema';
17
+
18
+ const CreatePage = () => {
19
+ const { formatMessage } = useIntl();
20
+ const { emitEvent } = useGlobalContext();
21
+ const { goBack } = useHistory();
22
+ const [isSubmiting, setIsSubmiting] = useState(false);
23
+ const { permissions, routes, policies, isLoading } = usePlugins();
24
+ const permissionsRef = useRef();
25
+
26
+ const headerActions = (handleSubmit, handleReset) => {
27
+ if (isLoading) {
28
+ return [];
29
+ }
30
+
31
+ return [
32
+ {
33
+ label: formatMessage({
34
+ id: getTrad('app.components.Button.reset'),
35
+ defaultMessage: 'Reset',
36
+ }),
37
+ onClick: () => {
38
+ handleReset();
39
+ permissionsRef.current.resetForm();
40
+ },
41
+ color: 'cancel',
42
+ type: 'button',
43
+ },
44
+ {
45
+ label: formatMessage({
46
+ id: 'app.components.Button.save',
47
+ defaultMessage: 'Save',
48
+ }),
49
+ onClick: handleSubmit,
50
+ color: 'success',
51
+ type: 'submit',
52
+ isLoading: isSubmiting,
53
+ },
54
+ ];
55
+ };
56
+
57
+ const handleCreateRoleSubmit = data => {
58
+ strapi.lockAppWithOverlay();
59
+ setIsSubmiting(true);
60
+
61
+ const permissions = permissionsRef.current.getPermissions();
62
+
63
+ Promise.resolve(
64
+ request(`/${pluginId}/roles`, {
65
+ method: 'POST',
66
+ body: { ...data, ...permissions, users: [] },
67
+ })
68
+ )
69
+ .then(() => {
70
+ emitEvent('didCreateRole');
71
+ strapi.notification.toggle({
72
+ type: 'success',
73
+ message: { id: 'Settings.roles.created' },
74
+ });
75
+ // Forcing redirecting since we don't have the id in the response
76
+ // TODO
77
+ goBack();
78
+ })
79
+ .catch(err => {
80
+ console.error(err);
81
+ strapi.notification.toggle({
82
+ type: 'warning',
83
+ message: { id: 'notification.error' },
84
+ });
85
+ })
86
+ .finally(() => {
87
+ setIsSubmiting(false);
88
+ strapi.unlockApp();
89
+ });
90
+ };
91
+
92
+ return (
93
+ <Formik
94
+ initialValues={{ name: '', description: '' }}
95
+ onSubmit={handleCreateRoleSubmit}
96
+ validationSchema={schema}
97
+ >
98
+ {({ handleSubmit, values, errors, handleReset, handleChange, handleBlur, touched }) => {
99
+ return (
100
+ <form onSubmit={handleSubmit}>
101
+ <ContainerFluid padding="0">
102
+ <Header
103
+ title={{
104
+ label: formatMessage({
105
+ id: getTrad('Settings.roles.create.title'),
106
+ defaultMessage: 'Create a role',
107
+ }),
108
+ }}
109
+ content={formatMessage({
110
+ id: getTrad('Settings.roles.create.description'),
111
+ defaultMessage: 'Define the rights given to the role',
112
+ })}
113
+ actions={headerActions(handleSubmit, handleReset)}
114
+ isLoading={isLoading}
115
+ />
116
+ <BaselineAlignement top size="3px" />
117
+ <FormCard
118
+ isLoading={isLoading}
119
+ title={formatMessage({
120
+ id: getTrad('EditPage.form.roles'),
121
+ defaultMessage: 'Role details',
122
+ })}
123
+ >
124
+ <SizedInput
125
+ label="Settings.roles.form.input.name"
126
+ defaultMessage="Name"
127
+ name="name"
128
+ type="text"
129
+ error={errors.name && touched.name ? { id: errors.name } : null}
130
+ onBlur={handleBlur}
131
+ value={values.name}
132
+ onChange={handleChange}
133
+ />
134
+ <SizedInput
135
+ label="Settings.roles.form.input.description"
136
+ defaultMessage="Description"
137
+ name="description"
138
+ type="textarea"
139
+ error={
140
+ errors.description && touched.description ? { id: errors.description } : null
141
+ }
142
+ onBlur={handleBlur}
143
+ value={values.description}
144
+ onChange={handleChange}
145
+ // Override the default height of the textarea
146
+ style={{ height: 115 }}
147
+ />
148
+ </FormCard>
149
+ </ContainerFluid>
150
+ <div style={{ paddingTop: '1.8rem' }} />
151
+ {!isLoading && (
152
+ <UsersPermissions
153
+ ref={permissionsRef}
154
+ permissions={permissions}
155
+ routes={routes}
156
+ policies={policies}
157
+ />
158
+ )}
159
+ <Padded top size="md" />
160
+ </form>
161
+ );
162
+ }}
163
+ </Formik>
164
+ );
165
+ };
166
+
167
+ export default CreatePage;
@@ -0,0 +1,9 @@
1
+ import * as yup from 'yup';
2
+ import { translatedErrors } from 'strapi-helper-plugin';
3
+
4
+ const schema = yup.object().shape({
5
+ name: yup.string().required(translatedErrors.required),
6
+ description: yup.string().required(translatedErrors.required),
7
+ });
8
+
9
+ export default schema;
@@ -0,0 +1,161 @@
1
+ import React, { useState, useRef } from 'react';
2
+ import { Header } from '@buffetjs/custom';
3
+ import { Padded } from '@buffetjs/core';
4
+ import { Formik } from 'formik';
5
+ import { useIntl } from 'react-intl';
6
+ import { useRouteMatch } from 'react-router-dom';
7
+ import { request, useGlobalContext } from 'strapi-helper-plugin';
8
+
9
+ import BaselineAlignement from '../../../components/BaselineAlignement';
10
+ import ContainerFluid from '../../../components/ContainerFluid';
11
+ import FormCard from '../../../components/FormBloc';
12
+ import SizedInput from '../../../components/SizedInput';
13
+ import getTrad from '../../../utils/getTrad';
14
+ import pluginId from '../../../pluginId';
15
+ import UsersPermissions from '../../../components/UsersPermissions';
16
+ import { usePlugins, useFetchRole } from '../../../hooks';
17
+
18
+ import schema from './utils/schema';
19
+
20
+ const EditPage = () => {
21
+ const { formatMessage } = useIntl();
22
+ const [isSubmiting, setIsSubmiting] = useState(false);
23
+ const { settingsBaseURL } = useGlobalContext();
24
+ const {
25
+ params: { id },
26
+ } = useRouteMatch(`${settingsBaseURL}/${pluginId}/roles/:id`);
27
+ const { routes, policies, isLoading } = usePlugins();
28
+ const { role, isLoading: isRoleLoading, onSubmitSucceeded } = useFetchRole(id);
29
+ const permissionsRef = useRef();
30
+
31
+ const headerActions = (handleSubmit, handleReset) => {
32
+ if (isLoading) {
33
+ return [];
34
+ }
35
+
36
+ return [
37
+ {
38
+ label: formatMessage({
39
+ id: 'app.components.Button.reset',
40
+ defaultMessage: 'Reset',
41
+ }),
42
+ onClick: () => {
43
+ handleReset();
44
+ permissionsRef.current.resetForm();
45
+ },
46
+ color: 'cancel',
47
+ type: 'button',
48
+ },
49
+ {
50
+ label: formatMessage({
51
+ id: 'app.components.Button.save',
52
+ defaultMessage: 'Save',
53
+ }),
54
+ onClick: handleSubmit,
55
+ color: 'success',
56
+ type: 'submit',
57
+ isLoading: isSubmiting,
58
+ },
59
+ ];
60
+ };
61
+
62
+ const handleCreateRoleSubmit = data => {
63
+ strapi.lockAppWithOverlay();
64
+ setIsSubmiting(true);
65
+
66
+ const permissions = permissionsRef.current.getPermissions();
67
+
68
+ Promise.resolve(
69
+ request(`/${pluginId}/roles/${id}`, {
70
+ method: 'PUT',
71
+ body: { ...data, ...permissions, users: [] },
72
+ })
73
+ )
74
+ .then(() => {
75
+ onSubmitSucceeded({ name: data.name, description: data.description });
76
+ permissionsRef.current.setFormAfterSubmit();
77
+ strapi.notification.toggle({
78
+ type: 'success',
79
+ message: { id: getTrad('Settings.roles.edited') },
80
+ });
81
+ })
82
+ .catch(err => {
83
+ console.error(err);
84
+ strapi.notification.toggle({
85
+ type: 'warning',
86
+ message: { id: 'notification.error' },
87
+ });
88
+ })
89
+ .finally(() => {
90
+ setIsSubmiting(false);
91
+ strapi.unlockApp();
92
+ });
93
+ };
94
+
95
+ return (
96
+ <Formik
97
+ enableReinitialize
98
+ initialValues={{ name: role.name, description: role.description }}
99
+ onSubmit={handleCreateRoleSubmit}
100
+ validationSchema={schema}
101
+ >
102
+ {({ handleSubmit, values, errors, handleReset, handleChange, handleBlur }) => (
103
+ <form onSubmit={handleSubmit}>
104
+ <ContainerFluid padding="0">
105
+ <Header
106
+ title={{
107
+ label: role.name,
108
+ }}
109
+ content={role.description}
110
+ actions={headerActions(handleSubmit, handleReset)}
111
+ isLoading={isRoleLoading}
112
+ />
113
+ <BaselineAlignement top size="3px" />
114
+ <FormCard
115
+ title={formatMessage({
116
+ id: getTrad('EditPage.form.roles'),
117
+ defaultMessage: 'Role details',
118
+ })}
119
+ isLoading={isLoading}
120
+ >
121
+ <SizedInput
122
+ label="Settings.roles.form.input.name"
123
+ defaultMessage="Name"
124
+ name="name"
125
+ type="text"
126
+ error={errors.name ? { id: errors.name } : null}
127
+ onBlur={handleBlur}
128
+ value={values.name}
129
+ onChange={handleChange}
130
+ />
131
+ <SizedInput
132
+ label="Settings.roles.form.input.description"
133
+ defaultMessage="Description"
134
+ name="description"
135
+ type="textarea"
136
+ error={errors.description ? { id: errors.description } : null}
137
+ onBlur={handleBlur}
138
+ value={values.description}
139
+ onChange={handleChange}
140
+ // Override the default height of the textarea
141
+ style={{ height: 115 }}
142
+ />
143
+ </FormCard>
144
+ </ContainerFluid>
145
+ <div style={{ paddingTop: '1.8rem' }} />
146
+ {!isLoading && !isRoleLoading && (
147
+ <UsersPermissions
148
+ ref={permissionsRef}
149
+ permissions={role.permissions}
150
+ routes={routes}
151
+ policies={policies}
152
+ />
153
+ )}
154
+ <Padded top size="md" />
155
+ </form>
156
+ )}
157
+ </Formik>
158
+ );
159
+ };
160
+
161
+ export default EditPage;
@@ -0,0 +1,9 @@
1
+ import * as yup from 'yup';
2
+ import { translatedErrors } from 'strapi-helper-plugin';
3
+
4
+ const schema = yup.object().shape({
5
+ name: yup.string().required(translatedErrors.required),
6
+ description: yup.string().required(translatedErrors.required),
7
+ });
8
+
9
+ export default schema;
@@ -0,0 +1,8 @@
1
+ import styled from 'styled-components';
2
+
3
+ // TODO : Temporary baseline alignment
4
+ const BaselineAlignment = styled.div`
5
+ padding-top: 3px;
6
+ `;
7
+
8
+ export default BaselineAlignment;