| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * User.js controller | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @description: A set of functions called "actions" for managing `User`. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-04 15:35:45 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2019-09-12 10:50:52 +02:00
										 |  |  | const { sanitizeEntity } = require('strapi-utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sanitizeUser = user => | 
					
						
							|  |  |  |   sanitizeEntity(user, { | 
					
						
							| 
									
										
										
										
											2019-09-18 17:29:09 +02:00
										 |  |  |     model: strapi.query('user', 'users-permissions').model, | 
					
						
							| 
									
										
										
										
											2019-09-12 10:50:52 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-12-04 15:35:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 12:10:23 +02:00
										 |  |  | const formatError = error => [ | 
					
						
							|  |  |  |   { messages: [{ id: error.id, message: error.message, field: error.field }] }, | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  | ]; | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Retrieve user records. | 
					
						
							|  |  |  |    * @return {Object|Array} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async find(ctx, next, { populate } = {}) { | 
					
						
							|  |  |  |     let users; | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 16:18:18 +02:00
										 |  |  |     if (_.has(ctx.query, '_q')) { | 
					
						
							|  |  |  |       // use core strapi query to search for users
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       users = await strapi.query('user', 'users-permissions').search(ctx.query, populate); | 
					
						
							| 
									
										
										
										
											2019-05-21 16:18:18 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       users = await strapi.plugins['users-permissions'].services.user.fetchAll(ctx.query, populate); | 
					
						
							| 
									
										
										
										
											2019-05-21 16:18:18 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |     const data = users.map(sanitizeUser); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |     ctx.send(data); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 20:29:34 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Retrieve authenticated user. | 
					
						
							|  |  |  |    * @return {Object|Array} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async me(ctx) { | 
					
						
							| 
									
										
										
										
											2018-01-10 20:29:34 +01:00
										 |  |  |     const user = ctx.state.user; | 
					
						
							| 
									
										
										
										
											2018-01-11 16:24:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!user) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]); | 
					
						
							| 
									
										
										
										
											2018-01-11 16:24:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-18 14:10:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |     const data = sanitizeUser(user); | 
					
						
							| 
									
										
										
										
											2018-01-10 20:29:34 +01:00
										 |  |  |     ctx.send(data); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Retrieve a user record. | 
					
						
							|  |  |  |    * @return {Object} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async findOne(ctx) { | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  |     let data = await strapi.plugins['users-permissions'].services.user.fetch({ | 
					
						
							|  |  |  |       id, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-12-06 14:15:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (data) { | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       data = sanitizeUser(data); | 
					
						
							| 
									
										
										
										
											2017-12-06 14:15:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Send 200 `ok`
 | 
					
						
							|  |  |  |     ctx.send(data); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Create a/an user record. | 
					
						
							|  |  |  |    * @return {Object} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async create(ctx) { | 
					
						
							|  |  |  |     const advanced = await strapi | 
					
						
							|  |  |  |       .store({ | 
					
						
							|  |  |  |         environment: '', | 
					
						
							|  |  |  |         type: 'plugin', | 
					
						
							|  |  |  |         name: 'users-permissions', | 
					
						
							|  |  |  |         key: 'advanced', | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .get(); | 
					
						
							| 
									
										
										
										
											2018-06-12 19:19:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |     const { email, username, password, role } = ctx.request.body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!email) return ctx.badRequest('missing.email'); | 
					
						
							|  |  |  |     if (!username) return ctx.badRequest('missing.username'); | 
					
						
							|  |  |  |     if (!password) return ctx.badRequest('missing.password'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     const userWithSameUsername = await strapi | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |       .query('user', 'users-permissions') | 
					
						
							|  |  |  |       .findOne({ username }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     if (userWithSameUsername) { | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |       return ctx.badRequest( | 
					
						
							|  |  |  |         null, | 
					
						
							| 
									
										
										
										
											2019-08-21 12:10:23 +02:00
										 |  |  |         formatError({ | 
					
						
							|  |  |  |           id: 'Auth.form.error.username.taken', | 
					
						
							|  |  |  |           message: 'Username already taken.', | 
					
						
							|  |  |  |           field: ['username'], | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (advanced.unique_email) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       const userWithSameEmail = await strapi.query('user', 'users-permissions').findOne({ email }); | 
					
						
							| 
									
										
										
										
											2018-01-26 09:37:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |       if (userWithSameEmail) { | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |         return ctx.badRequest( | 
					
						
							|  |  |  |           null, | 
					
						
							| 
									
										
										
										
											2019-08-21 12:10:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           formatError({ | 
					
						
							|  |  |  |             id: 'Auth.form.error.email.taken', | 
					
						
							|  |  |  |             message: 'Email already taken.', | 
					
						
							|  |  |  |             field: ['email'], | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-01-26 09:37:24 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |     const user = { | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |       ...ctx.request.body, | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |       provider: 'local', | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!role) { | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       const defaultRole = await strapi | 
					
						
							|  |  |  |         .query('role', 'users-permissions') | 
					
						
							|  |  |  |         .findOne({ type: advanced.default_role }, []); | 
					
						
							| 
									
										
										
										
											2018-06-12 19:19:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 15:31:15 +02:00
										 |  |  |       user.role = defaultRole.id; | 
					
						
							| 
									
										
										
										
											2018-06-12 19:19:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 15:11:55 +01:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       const data = await strapi.plugins['users-permissions'].services.user.add(user); | 
					
						
							| 
									
										
										
										
											2018-11-27 14:59:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 15:11:55 +01:00
										 |  |  |       ctx.created(data); | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2019-08-21 12:10:23 +02:00
										 |  |  |       ctx.badRequest(null, formatError(error)); | 
					
						
							| 
									
										
										
										
											2017-12-06 15:11:55 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Update a/an user record. | 
					
						
							|  |  |  |    * @return {Object} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async update(ctx) { | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     const advancedConfigs = await strapi | 
					
						
							|  |  |  |       .store({ | 
					
						
							|  |  |  |         environment: '', | 
					
						
							|  |  |  |         type: 'plugin', | 
					
						
							|  |  |  |         name: 'users-permissions', | 
					
						
							|  |  |  |         key: 'advanced', | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .get(); | 
					
						
							| 
									
										
										
										
											2017-12-04 15:35:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  |     const { email, username, password } = ctx.request.body; | 
					
						
							| 
									
										
										
										
											2017-12-04 15:35:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 16:42:53 +01:00
										 |  |  |     const user = await strapi.plugins['users-permissions'].services.user.fetch({ | 
					
						
							|  |  |  |       id, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:56 +02:00
										 |  |  |     if (_.has(ctx.request.body, 'email') && !email) { | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |       return ctx.badRequest('email.notNull'); | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-18 14:10:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:56 +02:00
										 |  |  |     if (_.has(ctx.request.body, 'username') && !username) { | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |       return ctx.badRequest('username.notNull'); | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     if (_.has(ctx.request.body, 'password') && !password && user.provider === 'local') { | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |       return ctx.badRequest('password.notNull'); | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (_.has(ctx.request.body, 'username')) { | 
					
						
							|  |  |  |       const userWithSameUsername = await strapi | 
					
						
							|  |  |  |         .query('user', 'users-permissions') | 
					
						
							|  |  |  |         .findOne({ username }); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |       if (userWithSameUsername && userWithSameUsername.id != id) { | 
					
						
							|  |  |  |         return ctx.badRequest( | 
					
						
							|  |  |  |           null, | 
					
						
							| 
									
										
										
										
											2019-09-18 17:29:09 +02:00
										 |  |  |           formatError({ | 
					
						
							|  |  |  |             id: 'Auth.form.error.username.taken', | 
					
						
							|  |  |  |             message: 'username.alreadyTaken.', | 
					
						
							|  |  |  |             field: ['username'], | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-12-06 15:11:55 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |     if (_.has(ctx.request.body, 'email') && advancedConfigs.unique_email) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       const userWithSameEmail = await strapi.query('user', 'users-permissions').findOne({ email }); | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (userWithSameEmail && userWithSameEmail.id != id) { | 
					
						
							|  |  |  |         return ctx.badRequest( | 
					
						
							|  |  |  |           null, | 
					
						
							| 
									
										
										
										
											2019-08-21 12:10:23 +02:00
										 |  |  |           formatError({ | 
					
						
							|  |  |  |             id: 'Auth.form.error.email.taken', | 
					
						
							| 
									
										
										
										
											2019-10-17 18:01:09 +08:00
										 |  |  |             message: 'Email already taken', | 
					
						
							| 
									
										
										
										
											2019-08-21 12:10:23 +02:00
										 |  |  |             field: ['email'], | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let updateData = { | 
					
						
							|  |  |  |       ...ctx.request.body, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:45 +02:00
										 |  |  |     if (_.has(ctx.request.body, 'password') && password === user.password) { | 
					
						
							| 
									
										
										
										
											2019-08-05 10:31:18 +02:00
										 |  |  |       delete updateData.password; | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const data = await strapi.plugins['users-permissions'].services.user.edit({ id }, updateData); | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.send(data); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Destroy a/an user record. | 
					
						
							|  |  |  |    * @return {Object} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async destroy(ctx) { | 
					
						
							| 
									
										
										
										
											2019-07-16 16:49:36 +02:00
										 |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const data = await strapi.plugins['users-permissions'].services.user.remove({ id }); | 
					
						
							| 
									
										
										
										
											2018-06-06 16:20:52 +02:00
										 |  |  |     ctx.send(data); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async destroyAll(ctx) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const data = await strapi.plugins['users-permissions'].services.user.removeAll( | 
					
						
							|  |  |  |       {}, | 
					
						
							|  |  |  |       ctx.request.query | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-06-06 16:20:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |     ctx.send(data); | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | }; |