@strapi/plugin-users-permissions 4.0.0-next.7 → 4.0.1

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 (180) hide show
  1. package/admin/src/components/BoundRoute/getMethodColor.js +41 -0
  2. package/admin/src/components/BoundRoute/index.js +40 -24
  3. package/admin/src/components/FormModal/Input/index.js +121 -0
  4. package/admin/src/components/FormModal/index.js +123 -0
  5. package/admin/src/components/Permissions/PermissionRow/CheckboxWrapper.js +19 -26
  6. package/admin/src/components/Permissions/PermissionRow/SubCategory.js +118 -0
  7. package/admin/src/components/Permissions/PermissionRow/index.js +9 -48
  8. package/admin/src/components/Permissions/index.js +36 -24
  9. package/admin/src/components/Permissions/init.js +1 -6
  10. package/admin/src/components/Policies/index.js +46 -47
  11. package/admin/src/components/UsersPermissions/index.js +29 -26
  12. package/admin/src/components/UsersPermissions/init.js +1 -2
  13. package/admin/src/hooks/useFetchRole/index.js +17 -7
  14. package/admin/src/hooks/useForm/index.js +3 -29
  15. package/admin/src/hooks/useForm/reducer.js +2 -21
  16. package/admin/src/hooks/usePlugins/index.js +12 -21
  17. package/admin/src/hooks/usePlugins/reducer.js +0 -3
  18. package/admin/src/index.js +29 -34
  19. package/admin/src/pages/AdvancedSettings/index.js +210 -193
  20. package/admin/src/pages/AdvancedSettings/utils/api.js +13 -0
  21. package/admin/src/pages/AdvancedSettings/utils/layout.js +96 -0
  22. package/admin/src/pages/AdvancedSettings/utils/schema.js +21 -0
  23. package/admin/src/pages/EmailTemplates/components/EmailForm.js +173 -0
  24. package/admin/src/pages/EmailTemplates/components/EmailTable.js +116 -0
  25. package/admin/src/pages/EmailTemplates/index.js +125 -198
  26. package/admin/src/pages/EmailTemplates/utils/api.js +13 -0
  27. package/admin/src/pages/Providers/index.js +208 -216
  28. package/admin/src/pages/Providers/utils/api.js +21 -0
  29. package/admin/src/pages/Providers/utils/forms.js +168 -126
  30. package/admin/src/pages/Roles/CreatePage/index.js +155 -147
  31. package/admin/src/pages/Roles/EditPage/index.js +162 -134
  32. package/admin/src/pages/Roles/ListPage/components/TableBody.js +96 -0
  33. package/admin/src/pages/Roles/ListPage/index.js +176 -156
  34. package/admin/src/pages/Roles/ListPage/utils/api.js +28 -0
  35. package/admin/src/pages/Roles/index.js +14 -8
  36. package/admin/src/translations/ar.json +0 -8
  37. package/admin/src/translations/cs.json +0 -8
  38. package/admin/src/translations/de.json +0 -8
  39. package/admin/src/translations/dk.json +0 -8
  40. package/admin/src/translations/en.json +33 -12
  41. package/admin/src/translations/es.json +0 -8
  42. package/admin/src/translations/fr.json +0 -8
  43. package/admin/src/translations/id.json +0 -8
  44. package/admin/src/translations/it.json +0 -8
  45. package/admin/src/translations/ja.json +0 -8
  46. package/admin/src/translations/ko.json +93 -54
  47. package/admin/src/translations/ms.json +0 -8
  48. package/admin/src/translations/nl.json +0 -8
  49. package/admin/src/translations/pl.json +0 -8
  50. package/admin/src/translations/pt-BR.json +0 -8
  51. package/admin/src/translations/pt.json +0 -8
  52. package/admin/src/translations/ru.json +0 -8
  53. package/admin/src/translations/sk.json +0 -8
  54. package/admin/src/translations/sv.json +0 -8
  55. package/admin/src/translations/th.json +0 -8
  56. package/admin/src/translations/tr.json +0 -8
  57. package/admin/src/translations/uk.json +0 -8
  58. package/admin/src/translations/vi.json +0 -8
  59. package/admin/src/translations/zh-Hans.json +5 -14
  60. package/admin/src/translations/zh.json +0 -8
  61. package/admin/src/utils/axiosInstance.js +36 -0
  62. package/admin/src/utils/formatPluginName.js +26 -0
  63. package/admin/src/utils/index.js +1 -0
  64. package/documentation/1.0.0/overrides/users-permissions-Role.json +6 -6
  65. package/documentation/1.0.0/overrides/users-permissions-User.json +7 -7
  66. package/jest.config.front.js +10 -0
  67. package/package.json +35 -32
  68. package/server/bootstrap/index.js +20 -25
  69. package/server/config.js +3 -3
  70. package/server/content-types/index.js +3 -3
  71. package/server/content-types/permission/index.js +30 -3
  72. package/server/content-types/role/index.js +47 -3
  73. package/server/content-types/user/index.js +65 -4
  74. package/server/controllers/auth.js +85 -237
  75. package/server/controllers/content-manager-user.js +183 -0
  76. package/server/controllers/index.js +12 -6
  77. package/server/controllers/permissions.js +26 -0
  78. package/server/controllers/role.js +77 -0
  79. package/server/controllers/settings.js +85 -0
  80. package/server/controllers/user.js +119 -45
  81. package/server/controllers/validation/auth.js +29 -0
  82. package/server/controllers/validation/user.js +38 -0
  83. package/server/graphql/index.js +44 -0
  84. package/server/graphql/mutations/auth/email-confirmation.js +39 -0
  85. package/server/graphql/mutations/auth/forgot-password.js +38 -0
  86. package/server/graphql/mutations/auth/login.js +38 -0
  87. package/server/graphql/mutations/auth/register.js +39 -0
  88. package/server/graphql/mutations/auth/reset-password.js +41 -0
  89. package/server/graphql/mutations/crud/role/create-role.js +37 -0
  90. package/server/graphql/mutations/crud/role/delete-role.js +28 -0
  91. package/server/graphql/mutations/crud/role/update-role.js +38 -0
  92. package/server/graphql/mutations/crud/user/create-user.js +48 -0
  93. package/server/graphql/mutations/crud/user/delete-user.js +42 -0
  94. package/server/graphql/mutations/crud/user/update-user.js +49 -0
  95. package/server/graphql/mutations/index.js +42 -0
  96. package/server/graphql/queries/index.js +13 -0
  97. package/server/graphql/queries/me.js +17 -0
  98. package/server/graphql/resolvers-configs.js +37 -0
  99. package/server/graphql/types/create-role-payload.js +11 -0
  100. package/server/graphql/types/delete-role-payload.js +11 -0
  101. package/server/graphql/types/index.js +21 -0
  102. package/server/graphql/types/login-input.js +13 -0
  103. package/server/graphql/types/login-payload.js +12 -0
  104. package/server/graphql/types/me-role.js +14 -0
  105. package/server/graphql/types/me.js +16 -0
  106. package/server/graphql/types/password-payload.js +11 -0
  107. package/server/graphql/types/register-input.js +13 -0
  108. package/server/graphql/types/update-role-payload.js +11 -0
  109. package/server/graphql/utils.js +27 -0
  110. package/server/index.js +21 -0
  111. package/server/middlewares/index.js +2 -2
  112. package/server/{policies → middlewares}/rateLimit.js +3 -7
  113. package/server/register.js +11 -0
  114. package/server/routes/admin/index.js +10 -0
  115. package/server/routes/admin/permissions.js +20 -0
  116. package/server/routes/admin/role.js +79 -0
  117. package/server/routes/admin/settings.js +95 -0
  118. package/server/routes/content-api/auth.js +73 -0
  119. package/server/routes/content-api/index.js +11 -0
  120. package/server/routes/content-api/permissions.js +9 -0
  121. package/server/routes/content-api/role.js +29 -0
  122. package/server/routes/content-api/user.js +61 -0
  123. package/server/routes/index.js +4 -3
  124. package/server/services/index.js +10 -8
  125. package/server/services/jwt.js +9 -17
  126. package/server/services/providers.js +32 -33
  127. package/server/services/role.js +177 -0
  128. package/server/services/user.js +9 -15
  129. package/server/services/users-permissions.js +140 -338
  130. package/server/strategies/users-permissions.js +123 -0
  131. package/server/utils/index.d.ts +2 -0
  132. package/strapi-admin.js +3 -0
  133. package/strapi-server.js +1 -19
  134. package/admin/src/assets/images/logo.svg +0 -1
  135. package/admin/src/components/BaselineAlignement/index.js +0 -33
  136. package/admin/src/components/Bloc/index.js +0 -10
  137. package/admin/src/components/BoundRoute/Components.js +0 -78
  138. package/admin/src/components/ContainerFluid/index.js +0 -13
  139. package/admin/src/components/FormBloc/index.js +0 -61
  140. package/admin/src/components/IntlInput/index.js +0 -38
  141. package/admin/src/components/ListBaselineAlignment/index.js +0 -8
  142. package/admin/src/components/ListRow/Components.js +0 -74
  143. package/admin/src/components/ListRow/index.js +0 -35
  144. package/admin/src/components/ModalForm/Wrapper.js +0 -12
  145. package/admin/src/components/ModalForm/index.js +0 -59
  146. package/admin/src/components/Permissions/ListWrapper.js +0 -9
  147. package/admin/src/components/Permissions/PermissionRow/BaselineAlignment.js +0 -7
  148. package/admin/src/components/Permissions/PermissionRow/RowStyle.js +0 -28
  149. package/admin/src/components/Permissions/PermissionRow/SubCategory/ConditionsButtonWrapper.js +0 -13
  150. package/admin/src/components/Permissions/PermissionRow/SubCategory/PolicyWrapper.js +0 -8
  151. package/admin/src/components/Permissions/PermissionRow/SubCategory/SubCategoryWrapper.js +0 -26
  152. package/admin/src/components/Permissions/PermissionRow/SubCategory/index.js +0 -116
  153. package/admin/src/components/Policies/Components.js +0 -26
  154. package/admin/src/components/PrefixedIcon/index.js +0 -27
  155. package/admin/src/components/Roles/EmptyRole/BaselineAlignment.js +0 -7
  156. package/admin/src/components/Roles/EmptyRole/index.js +0 -27
  157. package/admin/src/components/Roles/RoleListWrapper/index.js +0 -17
  158. package/admin/src/components/Roles/RoleRow/RoleDescription.js +0 -9
  159. package/admin/src/components/Roles/RoleRow/index.js +0 -45
  160. package/admin/src/components/Roles/index.js +0 -3
  161. package/admin/src/components/SizedInput/index.js +0 -24
  162. package/admin/src/pages/AdvancedSettings/reducer.js +0 -65
  163. package/admin/src/pages/AdvancedSettings/utils/form.js +0 -52
  164. package/admin/src/pages/EmailTemplates/CustomTextInput.js +0 -105
  165. package/admin/src/pages/EmailTemplates/Wrapper.js +0 -36
  166. package/admin/src/pages/EmailTemplates/reducer.js +0 -58
  167. package/admin/src/pages/EmailTemplates/utils/forms.js +0 -81
  168. package/admin/src/pages/Roles/ListPage/BaselineAlignment.js +0 -8
  169. package/server/content-types/permission/schema.json +0 -48
  170. package/server/content-types/role/schema.json +0 -46
  171. package/server/content-types/user/schema.json +0 -66
  172. package/server/controllers/user/admin.js +0 -230
  173. package/server/controllers/user/api.js +0 -174
  174. package/server/controllers/users-permissions.js +0 -271
  175. package/server/middlewares/users-permissions.js +0 -36
  176. package/server/policies/index.js +0 -11
  177. package/server/policies/isAuthenticated.js +0 -9
  178. package/server/policies/permissions.js +0 -94
  179. package/server/routes/routes.json +0 -381
  180. package/server/schema.graphql.js +0 -317
@@ -9,52 +9,39 @@
9
9
  /* eslint-disable no-useless-escape */
10
10
  const crypto = require('crypto');
11
11
  const _ = require('lodash');
12
- const grant = require('grant-koa');
13
- const { sanitizeEntity } = require('@strapi/utils');
12
+ const utils = require('@strapi/utils');
14
13
  const { getService } = require('../utils');
14
+ const {
15
+ validateCallbackBody,
16
+ validateRegisterBody,
17
+ validateSendEmailConfirmationBody,
18
+ } = require('./validation/auth');
19
+
20
+ const { sanitize } = utils;
21
+ const { ApplicationError, ValidationError } = utils.errors;
15
22
 
16
23
  const emailRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
17
- const formatError = error => [
18
- { messages: [{ id: error.id, message: error.message, field: error.field }] },
19
- ];
24
+
25
+ const sanitizeUser = (user, ctx) => {
26
+ const { auth } = ctx.state;
27
+ const userSchema = strapi.getModel('plugin::users-permissions.user');
28
+
29
+ return sanitize.contentAPI.output(user, userSchema, { auth });
30
+ };
20
31
 
21
32
  module.exports = {
22
33
  async callback(ctx) {
23
34
  const provider = ctx.params.provider || 'local';
24
35
  const params = ctx.request.body;
25
36
 
26
- const store = await strapi.store({
27
- environment: '',
28
- type: 'plugin',
29
- name: 'users-permissions',
30
- });
37
+ const store = await strapi.store({ type: 'plugin', name: 'users-permissions' });
31
38
 
32
39
  if (provider === 'local') {
33
40
  if (!_.get(await store.get({ key: 'grant' }), 'email.enabled')) {
34
- return ctx.badRequest(null, 'This provider is disabled.');
41
+ throw new ApplicationError('This provider is disabled');
35
42
  }
36
43
 
37
- // The identifier is required.
38
- if (!params.identifier) {
39
- return ctx.badRequest(
40
- null,
41
- formatError({
42
- id: 'Auth.form.error.email.provide',
43
- message: 'Please provide your username or your e-mail.',
44
- })
45
- );
46
- }
47
-
48
- // The password is required.
49
- if (!params.password) {
50
- return ctx.badRequest(
51
- null,
52
- formatError({
53
- id: 'Auth.form.error.password.provide',
54
- message: 'Please provide your password.',
55
- })
56
- );
57
- }
44
+ await validateCallbackBody(params);
58
45
 
59
46
  const query = { provider };
60
47
 
@@ -72,47 +59,24 @@ module.exports = {
72
59
  const user = await strapi.query('plugin::users-permissions.user').findOne({ where: query });
73
60
 
74
61
  if (!user) {
75
- return ctx.badRequest(
76
- null,
77
- formatError({
78
- id: 'Auth.form.error.invalid',
79
- message: 'Identifier or password invalid.',
80
- })
81
- );
62
+ throw new ValidationError('Invalid identifier or password');
82
63
  }
83
64
 
84
65
  if (
85
66
  _.get(await store.get({ key: 'advanced' }), 'email_confirmation') &&
86
67
  user.confirmed !== true
87
68
  ) {
88
- return ctx.badRequest(
89
- null,
90
- formatError({
91
- id: 'Auth.form.error.confirmed',
92
- message: 'Your account email is not confirmed',
93
- })
94
- );
69
+ throw new ApplicationError('Your account email is not confirmed');
95
70
  }
96
71
 
97
72
  if (user.blocked === true) {
98
- return ctx.badRequest(
99
- null,
100
- formatError({
101
- id: 'Auth.form.error.blocked',
102
- message: 'Your account has been blocked by an administrator',
103
- })
104
- );
73
+ throw new ApplicationError('Your account has been blocked by an administrator');
105
74
  }
106
75
 
107
76
  // The user never authenticated with the `local` provider.
108
77
  if (!user.password) {
109
- return ctx.badRequest(
110
- null,
111
- formatError({
112
- id: 'Auth.form.error.password.local',
113
- message:
114
- 'This user never set a local password, please login with the provider used during account creation.',
115
- })
78
+ throw new ApplicationError(
79
+ 'This user never set a local password, please login with the provider used during account creation'
116
80
  );
117
81
  }
118
82
 
@@ -122,32 +86,18 @@ module.exports = {
122
86
  );
123
87
 
124
88
  if (!validPassword) {
125
- return ctx.badRequest(
126
- null,
127
- formatError({
128
- id: 'Auth.form.error.invalid',
129
- message: 'Identifier or password invalid.',
130
- })
131
- );
89
+ throw new ValidationError('Invalid identifier or password');
132
90
  } else {
133
91
  ctx.send({
134
92
  jwt: getService('jwt').issue({
135
93
  id: user.id,
136
94
  }),
137
- user: sanitizeEntity(user, {
138
- model: strapi.getModel('plugin::users-permissions.user'),
139
- }),
95
+ user: await sanitizeUser(user, ctx),
140
96
  });
141
97
  }
142
98
  } else {
143
99
  if (!_.get(await store.get({ key: 'grant' }), [provider, 'enabled'])) {
144
- return ctx.badRequest(
145
- null,
146
- formatError({
147
- id: 'provider.disabled',
148
- message: 'This provider is disabled.',
149
- })
150
- );
100
+ throw new ApplicationError('This provider is disabled');
151
101
  }
152
102
 
153
103
  // Connect the user with the third-party provider.
@@ -156,18 +106,16 @@ module.exports = {
156
106
  try {
157
107
  [user, error] = await getService('providers').connect(provider, ctx.query);
158
108
  } catch ([user, error]) {
159
- return ctx.badRequest(null, error === 'array' ? error[0] : error);
109
+ throw new ApplicationError(error.message);
160
110
  }
161
111
 
162
112
  if (!user) {
163
- return ctx.badRequest(null, error === 'array' ? error[0] : error);
113
+ throw new ApplicationError(error.message);
164
114
  }
165
115
 
166
116
  ctx.send({
167
117
  jwt: getService('jwt').issue({ id: user.id }),
168
- user: sanitizeEntity(user, {
169
- model: strapi.getModel('plugin::users-permissions.user'),
170
- }),
118
+ user: await sanitizeUser(user, ctx),
171
119
  });
172
120
  }
173
121
  },
@@ -186,13 +134,7 @@ module.exports = {
186
134
  .findOne({ where: { resetPasswordToken: `${params.code}` } });
187
135
 
188
136
  if (!user) {
189
- return ctx.badRequest(
190
- null,
191
- formatError({
192
- id: 'Auth.form.error.code.provide',
193
- message: 'Incorrect code provided.',
194
- })
195
- );
137
+ throw new ValidationError('Incorrect code provided');
196
138
  }
197
139
 
198
140
  const password = await getService('user').hashPassword({ password: params.password });
@@ -204,53 +146,44 @@ module.exports = {
204
146
 
205
147
  ctx.send({
206
148
  jwt: getService('jwt').issue({ id: user.id }),
207
- user: sanitizeEntity(user, {
208
- model: strapi.getModel('plugin::users-permissions.user'),
209
- }),
149
+ user: await sanitizeUser(user, ctx),
210
150
  });
211
151
  } else if (
212
152
  params.password &&
213
153
  params.passwordConfirmation &&
214
154
  params.password !== params.passwordConfirmation
215
155
  ) {
216
- return ctx.badRequest(
217
- null,
218
- formatError({
219
- id: 'Auth.form.error.password.matching',
220
- message: 'Passwords do not match.',
221
- })
222
- );
156
+ throw new ValidationError('Passwords do not match');
223
157
  } else {
224
- return ctx.badRequest(
225
- null,
226
- formatError({
227
- id: 'Auth.form.error.params.provide',
228
- message: 'Incorrect params provided.',
229
- })
230
- );
158
+ throw new ValidationError('Incorrect params provided');
231
159
  }
232
160
  },
233
161
 
234
162
  async connect(ctx, next) {
235
- const grantConfig = await strapi
236
- .store({
237
- environment: '',
238
- type: 'plugin',
239
- name: 'users-permissions',
240
- key: 'grant',
241
- })
163
+ const grant = require('grant-koa');
164
+
165
+ const providers = await strapi
166
+ .store({ type: 'plugin', name: 'users-permissions', key: 'grant' })
242
167
  .get();
243
168
 
169
+ const apiPrefix = strapi.config.get('api.rest.prefix');
170
+ const grantConfig = {
171
+ defaults: {
172
+ prefix: `${apiPrefix}/connect`,
173
+ },
174
+ ...providers,
175
+ };
176
+
244
177
  const [requestPath] = ctx.request.url.split('?');
245
- const provider = requestPath.split('/')[2];
178
+ const provider = requestPath.split('/connect/')[1].split('/')[0];
246
179
 
247
180
  if (!_.get(grantConfig[provider], 'enabled')) {
248
- return ctx.badRequest(null, 'This provider is disabled.');
181
+ throw new ApplicationError('This provider is disabled');
249
182
  }
250
183
 
251
184
  if (!strapi.config.server.url.startsWith('http')) {
252
185
  strapi.log.warn(
253
- 'You are using a third party provider for login. Make sure to set an absolute url in config/server.js. More info here: https://strapi.io/documentation/developer-docs/latest/development/plugins/users-permissions.html#setting-up-the-server-url'
186
+ 'You are using a third party provider for login. Make sure to set an absolute url in config/server.js. More info here: https://docs.strapi.io/developer-docs/latest/plugins/users-permissions.html#setting-up-the-server-url'
254
187
  );
255
188
  }
256
189
 
@@ -270,20 +203,10 @@ module.exports = {
270
203
  if (isEmail) {
271
204
  email = email.toLowerCase();
272
205
  } else {
273
- return ctx.badRequest(
274
- null,
275
- formatError({
276
- id: 'Auth.form.error.email.format',
277
- message: 'Please provide valid email address.',
278
- })
279
- );
206
+ throw new ValidationError('Please provide a valid email address');
280
207
  }
281
208
 
282
- const pluginStore = await strapi.store({
283
- environment: '',
284
- type: 'plugin',
285
- name: 'users-permissions',
286
- });
209
+ const pluginStore = await strapi.store({ type: 'plugin', name: 'users-permissions' });
287
210
 
288
211
  // Find the user by email.
289
212
  const user = await strapi
@@ -292,13 +215,12 @@ module.exports = {
292
215
 
293
216
  // User not found.
294
217
  if (!user) {
295
- return ctx.badRequest(
296
- null,
297
- formatError({
298
- id: 'Auth.form.error.user.not-exist',
299
- message: 'This email does not exist.',
300
- })
301
- );
218
+ throw new ApplicationError('This email does not exist');
219
+ }
220
+
221
+ // User blocked
222
+ if (user.blocked) {
223
+ throw new ApplicationError('This user is disabled');
302
224
  }
303
225
 
304
226
  // Generate random token.
@@ -316,9 +238,7 @@ module.exports = {
316
238
  key: 'advanced',
317
239
  });
318
240
 
319
- const userInfo = sanitizeEntity(user, {
320
- model: strapi.getModel('plugin::users-permissions.user'),
321
- });
241
+ const userInfo = await sanitizeUser(user, ctx);
322
242
 
323
243
  settings.message = await getService('users-permissions').template(settings.message, {
324
244
  URL: advanced.email_reset_password,
@@ -347,7 +267,7 @@ module.exports = {
347
267
  html: settings.message,
348
268
  });
349
269
  } catch (err) {
350
- return ctx.badRequest(null, err);
270
+ throw new ApplicationError(err.message);
351
271
  }
352
272
 
353
273
  // Update the user.
@@ -359,24 +279,14 @@ module.exports = {
359
279
  },
360
280
 
361
281
  async register(ctx) {
362
- const pluginStore = await strapi.store({
363
- environment: '',
364
- type: 'plugin',
365
- name: 'users-permissions',
366
- });
282
+ const pluginStore = await strapi.store({ type: 'plugin', name: 'users-permissions' });
367
283
 
368
284
  const settings = await pluginStore.get({
369
285
  key: 'advanced',
370
286
  });
371
287
 
372
288
  if (!settings.allow_register) {
373
- return ctx.badRequest(
374
- null,
375
- formatError({
376
- id: 'Auth.advanced.allow_register',
377
- message: 'Register action is currently disabled.',
378
- })
379
- );
289
+ throw new ApplicationError('Register action is currently disabled');
380
290
  }
381
291
 
382
292
  const params = {
@@ -384,37 +294,13 @@ module.exports = {
384
294
  provider: 'local',
385
295
  };
386
296
 
387
- // Password is required.
388
- if (!params.password) {
389
- return ctx.badRequest(
390
- null,
391
- formatError({
392
- id: 'Auth.form.error.password.provide',
393
- message: 'Please provide your password.',
394
- })
395
- );
396
- }
397
-
398
- // Email is required.
399
- if (!params.email) {
400
- return ctx.badRequest(
401
- null,
402
- formatError({
403
- id: 'Auth.form.error.email.provide',
404
- message: 'Please provide your email.',
405
- })
406
- );
407
- }
297
+ await validateRegisterBody(params);
408
298
 
409
299
  // Throw an error if the password selected by the user
410
300
  // contains more than three times the symbol '$'.
411
301
  if (getService('user').isHashed(params.password)) {
412
- return ctx.badRequest(
413
- null,
414
- formatError({
415
- id: 'Auth.form.error.password.format',
416
- message: 'Your password cannot contain more than three times the symbol `$`.',
417
- })
302
+ throw new ValidationError(
303
+ 'Your password cannot contain more than three times the symbol `$`'
418
304
  );
419
305
  }
420
306
 
@@ -423,13 +309,7 @@ module.exports = {
423
309
  .findOne({ where: { type: settings.default_role } });
424
310
 
425
311
  if (!role) {
426
- return ctx.badRequest(
427
- null,
428
- formatError({
429
- id: 'Auth.form.error.role.notFound',
430
- message: 'Impossible to find the default role.',
431
- })
432
- );
312
+ throw new ApplicationError('Impossible to find the default role');
433
313
  }
434
314
 
435
315
  // Check if the provided email is valid or not.
@@ -438,13 +318,7 @@ module.exports = {
438
318
  if (isEmail) {
439
319
  params.email = params.email.toLowerCase();
440
320
  } else {
441
- return ctx.badRequest(
442
- null,
443
- formatError({
444
- id: 'Auth.form.error.email.format',
445
- message: 'Please provide valid email address.',
446
- })
447
- );
321
+ throw new ValidationError('Please provide a valid email address');
448
322
  }
449
323
 
450
324
  params.role = role.id;
@@ -455,23 +329,11 @@ module.exports = {
455
329
  });
456
330
 
457
331
  if (user && user.provider === params.provider) {
458
- return ctx.badRequest(
459
- null,
460
- formatError({
461
- id: 'Auth.form.error.email.taken',
462
- message: 'Email is already taken.',
463
- })
464
- );
332
+ throw new ApplicationError('Email is already taken');
465
333
  }
466
334
 
467
335
  if (user && user.provider !== params.provider && settings.unique_email) {
468
- return ctx.badRequest(
469
- null,
470
- formatError({
471
- id: 'Auth.form.error.email.taken',
472
- message: 'Email is already taken.',
473
- })
474
- );
336
+ throw new ApplicationError('Email is already taken');
475
337
  }
476
338
 
477
339
  try {
@@ -481,15 +343,13 @@ module.exports = {
481
343
 
482
344
  const user = await strapi.query('plugin::users-permissions.user').create({ data: params });
483
345
 
484
- const sanitizedUser = sanitizeEntity(user, {
485
- model: strapi.getModel('plugin::users-permissions.user'),
486
- });
346
+ const sanitizedUser = await sanitizeUser(user, ctx);
487
347
 
488
348
  if (settings.email_confirmation) {
489
349
  try {
490
- await getService('user').sendConfirmationEmail(user);
350
+ await getService('user').sendConfirmationEmail(sanitizedUser);
491
351
  } catch (err) {
492
- return ctx.badRequest(null, err);
352
+ throw new ApplicationError(err.message);
493
353
  }
494
354
 
495
355
  return ctx.send({ user: sanitizedUser });
@@ -502,14 +362,11 @@ module.exports = {
502
362
  user: sanitizedUser,
503
363
  });
504
364
  } catch (err) {
505
- const adminError = _.includes(err.message, 'username')
506
- ? {
507
- id: 'Auth.form.error.username.taken',
508
- message: 'Username already taken',
509
- }
510
- : { id: 'Auth.form.error.email.taken', message: 'Email already taken' };
511
-
512
- ctx.badRequest(null, formatError(adminError));
365
+ if (_.includes(err.message, 'username')) {
366
+ throw new ApplicationError('Username already taken');
367
+ } else {
368
+ throw new ApplicationError('Email already taken');
369
+ }
513
370
  }
514
371
  },
515
372
 
@@ -520,13 +377,13 @@ module.exports = {
520
377
  const jwtService = getService('jwt');
521
378
 
522
379
  if (_.isEmpty(confirmationToken)) {
523
- return ctx.badRequest('token.invalid');
380
+ throw new ValidationError('token.invalid');
524
381
  }
525
382
 
526
383
  const user = await userService.fetch({ confirmationToken }, []);
527
384
 
528
385
  if (!user) {
529
- return ctx.badRequest('token.invalid');
386
+ throw new ValidationError('token.invalid');
530
387
  }
531
388
 
532
389
  await userService.edit({ id: user.id }, { confirmed: true, confirmationToken: null });
@@ -534,18 +391,11 @@ module.exports = {
534
391
  if (returnUser) {
535
392
  ctx.send({
536
393
  jwt: jwtService.issue({ id: user.id }),
537
- user: sanitizeEntity(user, {
538
- model: strapi.getModel('plugin::users-permissions.user'),
539
- }),
394
+ user: await sanitizeUser(user, ctx),
540
395
  });
541
396
  } else {
542
397
  const settings = await strapi
543
- .store({
544
- environment: '',
545
- type: 'plugin',
546
- name: 'users-permissions',
547
- key: 'advanced',
548
- })
398
+ .store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
549
399
  .get();
550
400
 
551
401
  ctx.redirect(settings.email_confirmation_redirection || '/');
@@ -555,16 +405,14 @@ module.exports = {
555
405
  async sendEmailConfirmation(ctx) {
556
406
  const params = _.assign(ctx.request.body);
557
407
 
558
- if (!params.email) {
559
- return ctx.badRequest('missing.email');
560
- }
408
+ await validateSendEmailConfirmationBody(params);
561
409
 
562
410
  const isEmail = emailRegExp.test(params.email);
563
411
 
564
412
  if (isEmail) {
565
413
  params.email = params.email.toLowerCase();
566
414
  } else {
567
- return ctx.badRequest('wrong.email');
415
+ throw new ValidationError('wrong.email');
568
416
  }
569
417
 
570
418
  const user = await strapi.query('plugin::users-permissions.user').findOne({
@@ -572,11 +420,11 @@ module.exports = {
572
420
  });
573
421
 
574
422
  if (user.confirmed) {
575
- return ctx.badRequest('already.confirmed');
423
+ throw new ApplicationError('already.confirmed');
576
424
  }
577
425
 
578
426
  if (user.blocked) {
579
- return ctx.badRequest('blocked.user');
427
+ throw new ApplicationError('blocked.user');
580
428
  }
581
429
 
582
430
  try {
@@ -586,7 +434,7 @@ module.exports = {
586
434
  sent: true,
587
435
  });
588
436
  } catch (err) {
589
- return ctx.badRequest(null, err);
437
+ throw new ApplicationError(err.message);
590
438
  }
591
439
  },
592
440
  };