| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  | const { ApplicationError } = require('@strapi/utils').errors; | 
					
						
							| 
									
										
										
										
											2020-07-15 15:46:59 +02:00
										 |  |  | const { | 
					
						
							|  |  |  |   validateUserCreationInput, | 
					
						
							|  |  |  |   validateUserUpdateInput, | 
					
						
							|  |  |  |   validateUsersDeleteInput, | 
					
						
							|  |  |  | } = require('../validation/user'); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  | const { getService } = require('../utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |   async create(ctx) { | 
					
						
							|  |  |  |     const { body } = ctx.request; | 
					
						
							| 
									
										
										
										
											2022-09-21 14:07:29 +01:00
										 |  |  |     const cleanData = { ...body, email: _.get(body, `email`, ``).toLowerCase() }; | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 14:07:29 +01:00
										 |  |  |     await validateUserCreationInput(cleanData); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 14:07:29 +01:00
										 |  |  |     const attributes = _.pick(cleanData, [ | 
					
						
							| 
									
										
										
										
											2021-02-05 16:35:47 +01:00
										 |  |  |       'firstname', | 
					
						
							|  |  |  |       'lastname', | 
					
						
							|  |  |  |       'email', | 
					
						
							|  |  |  |       'roles', | 
					
						
							|  |  |  |       'preferedLanguage', | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const userAlreadyExists = await getService('user').exists({ | 
					
						
							| 
									
										
										
										
											2020-05-14 11:57:43 +02:00
										 |  |  |       email: attributes.email, | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (userAlreadyExists) { | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       throw new ApplicationError('Email already taken'); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const createdUser = await getService('user').create(attributes); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const userInfo = getService('user').sanitizeUser(createdUser); | 
					
						
							| 
									
										
										
										
											2020-05-18 16:07:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-13 11:33:25 +02:00
										 |  |  |     // Note: We need to assign manually the registrationToken to the
 | 
					
						
							|  |  |  |     // final user payload so that it's not removed in the sanitation process.
 | 
					
						
							|  |  |  |     Object.assign(userInfo, { registrationToken: createdUser.registrationToken }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  |     // Send 201 created
 | 
					
						
							| 
									
										
										
										
											2020-05-18 16:07:37 +02:00
										 |  |  |     ctx.created({ data: userInfo }); | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-19 10:55:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async find(ctx) { | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const userService = getService('user'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-09 11:35:50 +01:00
										 |  |  |     const permissionsManager = strapi.admin.services.permission.createPermissionsManager({ | 
					
						
							|  |  |  |       ability: ctx.state.userAbility, | 
					
						
							|  |  |  |       model: 'admin::user', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const sanitizedQuery = await permissionsManager.sanitizeQuery(ctx.query); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { results, pagination } = await userService.findPage(sanitizedQuery); | 
					
						
							| 
									
										
										
										
											2020-05-19 11:06:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-29 11:41:53 +02:00
										 |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2020-05-19 16:10:53 +02:00
										 |  |  |       data: { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |         results: results.map((user) => userService.sanitizeUser(user)), | 
					
						
							| 
									
										
										
										
											2020-05-19 16:10:53 +02:00
										 |  |  |         pagination, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-05-19 11:06:07 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-05-19 10:55:52 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-27 16:06:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 14:03:48 +02:00
										 |  |  |   async findOne(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 16:06:43 +02:00
										 |  |  |     const user = await getService('user').findOne(id); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:03:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!user) { | 
					
						
							| 
									
										
										
										
											2020-06-04 10:25:02 +02:00
										 |  |  |       return ctx.notFound('User does not exist'); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:03:48 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |       data: getService('user').sanitizeUser(user), | 
					
						
							| 
									
										
										
										
											2020-05-28 14:03:48 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 16:06:15 +02:00
										 |  |  |   async update(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  |     const { body: input } = ctx.request; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |     await validateUserUpdateInput(input); | 
					
						
							| 
									
										
										
										
											2020-05-27 16:06:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-04 12:34:22 +02:00
										 |  |  |     if (_.has(input, 'email')) { | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |       const uniqueEmailCheck = await getService('user').exists({ | 
					
						
							| 
									
										
										
										
											2021-06-22 17:13:11 +02:00
										 |  |  |         id: { $ne: id }, | 
					
						
							| 
									
										
										
										
											2020-06-04 12:34:22 +02:00
										 |  |  |         email: input.email, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (uniqueEmailCheck) { | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |         throw new ApplicationError('A user with this email address already exists'); | 
					
						
							| 
									
										
										
										
											2020-06-04 12:34:22 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const updatedUser = await getService('user').updateById(id, input); | 
					
						
							| 
									
										
										
										
											2020-05-27 16:06:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-04 10:25:02 +02:00
										 |  |  |     if (!updatedUser) { | 
					
						
							| 
									
										
										
										
											2020-06-04 10:48:02 +02:00
										 |  |  |       return ctx.notFound('User does not exist'); | 
					
						
							| 
									
										
										
										
											2020-05-27 16:06:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |       data: getService('user').sanitizeUser(updatedUser), | 
					
						
							| 
									
										
										
										
											2020-05-27 16:06:15 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-06-18 11:40:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 15:46:59 +02:00
										 |  |  |   async deleteOne(ctx) { | 
					
						
							| 
									
										
										
										
											2020-06-18 11:40:50 +02:00
										 |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const deletedUser = await getService('user').deleteById(id); | 
					
						
							| 
									
										
										
										
											2020-06-18 11:40:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!deletedUser) { | 
					
						
							|  |  |  |       return ctx.notFound('User not found'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ctx.deleted({ | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |       data: getService('user').sanitizeUser(deletedUser), | 
					
						
							| 
									
										
										
										
											2020-06-18 11:40:50 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-07-15 15:46:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Delete several users | 
					
						
							|  |  |  |    * @param {KoaContext} ctx - koa context | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async deleteMany(ctx) { | 
					
						
							|  |  |  |     const { body } = ctx.request; | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |     await validateUsersDeleteInput(body); | 
					
						
							| 
									
										
										
										
											2020-07-15 15:46:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  |     const users = await getService('user').deleteByIds(body.ids); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const sanitizedUsers = users.map(getService('user').sanitizeUser); | 
					
						
							| 
									
										
										
										
											2020-07-15 15:46:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ctx.deleted({ | 
					
						
							|  |  |  |       data: sanitizedUsers, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-13 12:27:46 +02:00
										 |  |  | }; |