@strapi/plugin-users-permissions 4.0.0-next.9 → 4.0.3

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 (178) 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 +0 -8
  19. package/admin/src/pages/AdvancedSettings/index.js +203 -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 +117 -197
  26. package/admin/src/pages/EmailTemplates/utils/api.js +13 -0
  27. package/admin/src/pages/Providers/index.js +206 -221
  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/translations/ar.json +0 -8
  36. package/admin/src/translations/cs.json +0 -8
  37. package/admin/src/translations/de.json +0 -8
  38. package/admin/src/translations/dk.json +0 -8
  39. package/admin/src/translations/en.json +33 -12
  40. package/admin/src/translations/es.json +0 -8
  41. package/admin/src/translations/fr.json +0 -8
  42. package/admin/src/translations/id.json +0 -8
  43. package/admin/src/translations/it.json +0 -8
  44. package/admin/src/translations/ja.json +0 -8
  45. package/admin/src/translations/ko.json +93 -54
  46. package/admin/src/translations/ms.json +0 -8
  47. package/admin/src/translations/nl.json +0 -8
  48. package/admin/src/translations/pl.json +0 -8
  49. package/admin/src/translations/pt-BR.json +0 -8
  50. package/admin/src/translations/pt.json +0 -8
  51. package/admin/src/translations/ru.json +0 -8
  52. package/admin/src/translations/sk.json +0 -8
  53. package/admin/src/translations/sv.json +0 -8
  54. package/admin/src/translations/th.json +0 -8
  55. package/admin/src/translations/tr.json +0 -8
  56. package/admin/src/translations/uk.json +0 -8
  57. package/admin/src/translations/vi.json +0 -8
  58. package/admin/src/translations/zh-Hans.json +5 -14
  59. package/admin/src/translations/zh.json +0 -8
  60. package/admin/src/utils/axiosInstance.js +36 -0
  61. package/admin/src/utils/formatPluginName.js +26 -0
  62. package/admin/src/utils/index.js +1 -0
  63. package/documentation/1.0.0/overrides/users-permissions-Role.json +6 -6
  64. package/documentation/1.0.0/overrides/users-permissions-User.json +7 -7
  65. package/jest.config.front.js +10 -0
  66. package/package.json +36 -33
  67. package/server/bootstrap/index.js +19 -21
  68. package/server/config.js +3 -3
  69. package/server/content-types/index.js +3 -3
  70. package/server/content-types/permission/index.js +30 -3
  71. package/server/content-types/role/index.js +47 -3
  72. package/server/content-types/user/index.js +65 -4
  73. package/server/controllers/auth.js +82 -245
  74. package/server/controllers/content-manager-user.js +183 -0
  75. package/server/controllers/index.js +12 -6
  76. package/server/controllers/permissions.js +26 -0
  77. package/server/controllers/role.js +77 -0
  78. package/server/controllers/settings.js +85 -0
  79. package/server/controllers/user.js +119 -45
  80. package/server/controllers/validation/auth.js +29 -0
  81. package/server/controllers/validation/user.js +38 -0
  82. package/server/graphql/index.js +44 -0
  83. package/server/graphql/mutations/auth/email-confirmation.js +39 -0
  84. package/server/graphql/mutations/auth/forgot-password.js +38 -0
  85. package/server/graphql/mutations/auth/login.js +38 -0
  86. package/server/graphql/mutations/auth/register.js +39 -0
  87. package/server/graphql/mutations/auth/reset-password.js +41 -0
  88. package/server/graphql/mutations/crud/role/create-role.js +37 -0
  89. package/server/graphql/mutations/crud/role/delete-role.js +28 -0
  90. package/server/graphql/mutations/crud/role/update-role.js +38 -0
  91. package/server/graphql/mutations/crud/user/create-user.js +48 -0
  92. package/server/graphql/mutations/crud/user/delete-user.js +42 -0
  93. package/server/graphql/mutations/crud/user/update-user.js +49 -0
  94. package/server/graphql/mutations/index.js +42 -0
  95. package/server/graphql/queries/index.js +13 -0
  96. package/server/graphql/queries/me.js +17 -0
  97. package/server/graphql/resolvers-configs.js +37 -0
  98. package/server/graphql/types/create-role-payload.js +11 -0
  99. package/server/graphql/types/delete-role-payload.js +11 -0
  100. package/server/graphql/types/index.js +21 -0
  101. package/server/graphql/types/login-input.js +13 -0
  102. package/server/graphql/types/login-payload.js +12 -0
  103. package/server/graphql/types/me-role.js +14 -0
  104. package/server/graphql/types/me.js +16 -0
  105. package/server/graphql/types/password-payload.js +11 -0
  106. package/server/graphql/types/register-input.js +13 -0
  107. package/server/graphql/types/update-role-payload.js +11 -0
  108. package/server/graphql/utils.js +27 -0
  109. package/server/index.js +21 -0
  110. package/server/middlewares/index.js +2 -2
  111. package/server/{policies → middlewares}/rateLimit.js +3 -7
  112. package/server/register.js +11 -0
  113. package/server/routes/admin/index.js +10 -0
  114. package/server/routes/admin/permissions.js +20 -0
  115. package/server/routes/admin/role.js +79 -0
  116. package/server/routes/admin/settings.js +95 -0
  117. package/server/routes/content-api/auth.js +73 -0
  118. package/server/routes/content-api/index.js +11 -0
  119. package/server/routes/content-api/permissions.js +9 -0
  120. package/server/routes/content-api/role.js +29 -0
  121. package/server/routes/content-api/user.js +61 -0
  122. package/server/routes/index.js +4 -428
  123. package/server/services/index.js +10 -8
  124. package/server/services/jwt.js +9 -17
  125. package/server/services/providers.js +32 -33
  126. package/server/services/role.js +177 -0
  127. package/server/services/user.js +23 -22
  128. package/server/services/users-permissions.js +140 -338
  129. package/server/strategies/users-permissions.js +123 -0
  130. package/server/utils/index.d.ts +2 -0
  131. package/strapi-admin.js +3 -0
  132. package/strapi-server.js +1 -19
  133. package/admin/src/assets/images/logo.svg +0 -1
  134. package/admin/src/components/BaselineAlignement/index.js +0 -33
  135. package/admin/src/components/Bloc/index.js +0 -10
  136. package/admin/src/components/BoundRoute/Components.js +0 -78
  137. package/admin/src/components/ContainerFluid/index.js +0 -13
  138. package/admin/src/components/FormBloc/index.js +0 -61
  139. package/admin/src/components/IntlInput/index.js +0 -38
  140. package/admin/src/components/ListBaselineAlignment/index.js +0 -8
  141. package/admin/src/components/ListRow/Components.js +0 -74
  142. package/admin/src/components/ListRow/index.js +0 -35
  143. package/admin/src/components/ModalForm/Wrapper.js +0 -12
  144. package/admin/src/components/ModalForm/index.js +0 -59
  145. package/admin/src/components/Permissions/ListWrapper.js +0 -9
  146. package/admin/src/components/Permissions/PermissionRow/BaselineAlignment.js +0 -7
  147. package/admin/src/components/Permissions/PermissionRow/RowStyle.js +0 -28
  148. package/admin/src/components/Permissions/PermissionRow/SubCategory/ConditionsButtonWrapper.js +0 -13
  149. package/admin/src/components/Permissions/PermissionRow/SubCategory/PolicyWrapper.js +0 -8
  150. package/admin/src/components/Permissions/PermissionRow/SubCategory/SubCategoryWrapper.js +0 -26
  151. package/admin/src/components/Permissions/PermissionRow/SubCategory/index.js +0 -116
  152. package/admin/src/components/Policies/Components.js +0 -26
  153. package/admin/src/components/PrefixedIcon/index.js +0 -27
  154. package/admin/src/components/Roles/EmptyRole/BaselineAlignment.js +0 -7
  155. package/admin/src/components/Roles/EmptyRole/index.js +0 -27
  156. package/admin/src/components/Roles/RoleListWrapper/index.js +0 -17
  157. package/admin/src/components/Roles/RoleRow/RoleDescription.js +0 -9
  158. package/admin/src/components/Roles/RoleRow/index.js +0 -45
  159. package/admin/src/components/Roles/index.js +0 -3
  160. package/admin/src/components/SizedInput/index.js +0 -24
  161. package/admin/src/pages/AdvancedSettings/reducer.js +0 -65
  162. package/admin/src/pages/AdvancedSettings/utils/form.js +0 -52
  163. package/admin/src/pages/EmailTemplates/CustomTextInput.js +0 -105
  164. package/admin/src/pages/EmailTemplates/Wrapper.js +0 -36
  165. package/admin/src/pages/EmailTemplates/reducer.js +0 -58
  166. package/admin/src/pages/EmailTemplates/utils/forms.js +0 -81
  167. package/admin/src/pages/Roles/ListPage/BaselineAlignment.js +0 -8
  168. package/server/content-types/permission/schema.json +0 -48
  169. package/server/content-types/role/schema.json +0 -46
  170. package/server/content-types/user/schema.json +0 -66
  171. package/server/controllers/user/admin.js +0 -230
  172. package/server/controllers/user/api.js +0 -174
  173. package/server/controllers/users-permissions.js +0 -271
  174. package/server/middlewares/users-permissions.js +0 -44
  175. package/server/policies/index.js +0 -11
  176. package/server/policies/isAuthenticated.js +0 -9
  177. package/server/policies/permissions.js +0 -94
  178. 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 a 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,24 +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');
302
219
  }
303
220
 
304
221
  // User blocked
305
222
  if (user.blocked) {
306
- return ctx.badRequest(
307
- null,
308
- formatError({
309
- id: 'Auth.form.error.user.blocked',
310
- message: 'This user is disabled.',
311
- })
312
- );
223
+ throw new ApplicationError('This user is disabled');
313
224
  }
314
225
 
315
226
  // Generate random token.
@@ -327,9 +238,7 @@ module.exports = {
327
238
  key: 'advanced',
328
239
  });
329
240
 
330
- const userInfo = sanitizeEntity(user, {
331
- model: strapi.getModel('plugin::users-permissions.user'),
332
- });
241
+ const userInfo = await sanitizeUser(user, ctx);
333
242
 
334
243
  settings.message = await getService('users-permissions').template(settings.message, {
335
244
  URL: advanced.email_reset_password,
@@ -358,7 +267,7 @@ module.exports = {
358
267
  html: settings.message,
359
268
  });
360
269
  } catch (err) {
361
- return ctx.badRequest(null, err);
270
+ throw new ApplicationError(err.message);
362
271
  }
363
272
 
364
273
  // Update the user.
@@ -370,24 +279,14 @@ module.exports = {
370
279
  },
371
280
 
372
281
  async register(ctx) {
373
- const pluginStore = await strapi.store({
374
- environment: '',
375
- type: 'plugin',
376
- name: 'users-permissions',
377
- });
282
+ const pluginStore = await strapi.store({ type: 'plugin', name: 'users-permissions' });
378
283
 
379
284
  const settings = await pluginStore.get({
380
285
  key: 'advanced',
381
286
  });
382
287
 
383
288
  if (!settings.allow_register) {
384
- return ctx.badRequest(
385
- null,
386
- formatError({
387
- id: 'Auth.advanced.allow_register',
388
- message: 'Register action is currently disabled.',
389
- })
390
- );
289
+ throw new ApplicationError('Register action is currently disabled');
391
290
  }
392
291
 
393
292
  const params = {
@@ -395,37 +294,13 @@ module.exports = {
395
294
  provider: 'local',
396
295
  };
397
296
 
398
- // Password is required.
399
- if (!params.password) {
400
- return ctx.badRequest(
401
- null,
402
- formatError({
403
- id: 'Auth.form.error.password.provide',
404
- message: 'Please provide your password.',
405
- })
406
- );
407
- }
408
-
409
- // Email is required.
410
- if (!params.email) {
411
- return ctx.badRequest(
412
- null,
413
- formatError({
414
- id: 'Auth.form.error.email.provide',
415
- message: 'Please provide your email.',
416
- })
417
- );
418
- }
297
+ await validateRegisterBody(params);
419
298
 
420
299
  // Throw an error if the password selected by the user
421
300
  // contains more than three times the symbol '$'.
422
301
  if (getService('user').isHashed(params.password)) {
423
- return ctx.badRequest(
424
- null,
425
- formatError({
426
- id: 'Auth.form.error.password.format',
427
- message: 'Your password cannot contain more than three times the symbol `$`.',
428
- })
302
+ throw new ValidationError(
303
+ 'Your password cannot contain more than three times the symbol `$`'
429
304
  );
430
305
  }
431
306
 
@@ -434,13 +309,7 @@ module.exports = {
434
309
  .findOne({ where: { type: settings.default_role } });
435
310
 
436
311
  if (!role) {
437
- return ctx.badRequest(
438
- null,
439
- formatError({
440
- id: 'Auth.form.error.role.notFound',
441
- message: 'Impossible to find the default role.',
442
- })
443
- );
312
+ throw new ApplicationError('Impossible to find the default role');
444
313
  }
445
314
 
446
315
  // Check if the provided email is valid or not.
@@ -449,13 +318,7 @@ module.exports = {
449
318
  if (isEmail) {
450
319
  params.email = params.email.toLowerCase();
451
320
  } else {
452
- return ctx.badRequest(
453
- null,
454
- formatError({
455
- id: 'Auth.form.error.email.format',
456
- message: 'Please provide valid email address.',
457
- })
458
- );
321
+ throw new ValidationError('Please provide a valid email address');
459
322
  }
460
323
 
461
324
  params.role = role.id;
@@ -466,23 +329,11 @@ module.exports = {
466
329
  });
467
330
 
468
331
  if (user && user.provider === params.provider) {
469
- return ctx.badRequest(
470
- null,
471
- formatError({
472
- id: 'Auth.form.error.email.taken',
473
- message: 'Email is already taken.',
474
- })
475
- );
332
+ throw new ApplicationError('Email is already taken');
476
333
  }
477
334
 
478
335
  if (user && user.provider !== params.provider && settings.unique_email) {
479
- return ctx.badRequest(
480
- null,
481
- formatError({
482
- id: 'Auth.form.error.email.taken',
483
- message: 'Email is already taken.',
484
- })
485
- );
336
+ throw new ApplicationError('Email is already taken');
486
337
  }
487
338
 
488
339
  try {
@@ -492,15 +343,13 @@ module.exports = {
492
343
 
493
344
  const user = await strapi.query('plugin::users-permissions.user').create({ data: params });
494
345
 
495
- const sanitizedUser = sanitizeEntity(user, {
496
- model: strapi.getModel('plugin::users-permissions.user'),
497
- });
346
+ const sanitizedUser = await sanitizeUser(user, ctx);
498
347
 
499
348
  if (settings.email_confirmation) {
500
349
  try {
501
- await getService('user').sendConfirmationEmail(user);
350
+ await getService('user').sendConfirmationEmail(sanitizedUser);
502
351
  } catch (err) {
503
- return ctx.badRequest(null, err);
352
+ throw new ApplicationError(err.message);
504
353
  }
505
354
 
506
355
  return ctx.send({ user: sanitizedUser });
@@ -513,14 +362,11 @@ module.exports = {
513
362
  user: sanitizedUser,
514
363
  });
515
364
  } catch (err) {
516
- const adminError = _.includes(err.message, 'username')
517
- ? {
518
- id: 'Auth.form.error.username.taken',
519
- message: 'Username already taken',
520
- }
521
- : { id: 'Auth.form.error.email.taken', message: 'Email already taken' };
522
-
523
- 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
+ }
524
370
  }
525
371
  },
526
372
 
@@ -531,32 +377,25 @@ module.exports = {
531
377
  const jwtService = getService('jwt');
532
378
 
533
379
  if (_.isEmpty(confirmationToken)) {
534
- return ctx.badRequest('token.invalid');
380
+ throw new ValidationError('token.invalid');
535
381
  }
536
382
 
537
383
  const user = await userService.fetch({ confirmationToken }, []);
538
384
 
539
385
  if (!user) {
540
- return ctx.badRequest('token.invalid');
386
+ throw new ValidationError('token.invalid');
541
387
  }
542
388
 
543
- await userService.edit({ id: user.id }, { confirmed: true, confirmationToken: null });
389
+ await userService.edit(user.id, { confirmed: true, confirmationToken: null });
544
390
 
545
391
  if (returnUser) {
546
392
  ctx.send({
547
393
  jwt: jwtService.issue({ id: user.id }),
548
- user: sanitizeEntity(user, {
549
- model: strapi.getModel('plugin::users-permissions.user'),
550
- }),
394
+ user: await sanitizeUser(user, ctx),
551
395
  });
552
396
  } else {
553
397
  const settings = await strapi
554
- .store({
555
- environment: '',
556
- type: 'plugin',
557
- name: 'users-permissions',
558
- key: 'advanced',
559
- })
398
+ .store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
560
399
  .get();
561
400
 
562
401
  ctx.redirect(settings.email_confirmation_redirection || '/');
@@ -566,16 +405,14 @@ module.exports = {
566
405
  async sendEmailConfirmation(ctx) {
567
406
  const params = _.assign(ctx.request.body);
568
407
 
569
- if (!params.email) {
570
- return ctx.badRequest('missing.email');
571
- }
408
+ await validateSendEmailConfirmationBody(params);
572
409
 
573
410
  const isEmail = emailRegExp.test(params.email);
574
411
 
575
412
  if (isEmail) {
576
413
  params.email = params.email.toLowerCase();
577
414
  } else {
578
- return ctx.badRequest('wrong.email');
415
+ throw new ValidationError('wrong.email');
579
416
  }
580
417
 
581
418
  const user = await strapi.query('plugin::users-permissions.user').findOne({
@@ -583,11 +420,11 @@ module.exports = {
583
420
  });
584
421
 
585
422
  if (user.confirmed) {
586
- return ctx.badRequest('already.confirmed');
423
+ throw new ApplicationError('already.confirmed');
587
424
  }
588
425
 
589
426
  if (user.blocked) {
590
- return ctx.badRequest('blocked.user');
427
+ throw new ApplicationError('blocked.user');
591
428
  }
592
429
 
593
430
  try {
@@ -597,7 +434,7 @@ module.exports = {
597
434
  sent: true,
598
435
  });
599
436
  } catch (err) {
600
- return ctx.badRequest(null, err);
437
+ throw new ApplicationError(err.message);
601
438
  }
602
439
  },
603
440
  };