| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2021-04-29 13:51:12 +02:00
										 |  |  | const { sanitizeEntity } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  | const { getService } = require('../utils'); | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  | const adminUserController = require('./user/admin'); | 
					
						
							|  |  |  | const apiUserController = require('./user/api'); | 
					
						
							| 
									
										
										
										
											2019-09-12 10:50:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const sanitizeUser = user => | 
					
						
							|  |  |  |   sanitizeEntity(user, { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |     model: strapi.getModel('plugin::users-permissions.user'), | 
					
						
							| 
									
										
										
										
											2019-09-12 10:50:52 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-12-04 15:35:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  | const resolveController = ctx => { | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     state: { isAuthenticatedAdmin }, | 
					
						
							|  |  |  |   } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return isAuthenticatedAdmin ? adminUserController : apiUserController; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const resolveControllerMethod = method => ctx => { | 
					
						
							|  |  |  |   const controller = resolveController(ctx); | 
					
						
							|  |  |  |   const callbackFn = controller[method]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!_.isFunction(callbackFn)) { | 
					
						
							|  |  |  |     return ctx.notFound(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return callbackFn(ctx); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |   create: resolveControllerMethod('create'), | 
					
						
							|  |  |  |   update: resolveControllerMethod('update'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 } = {}) { | 
					
						
							| 
									
										
										
										
											2021-07-28 21:03:32 +02:00
										 |  |  |     const users = await getService('user').fetchAll(ctx.query, populate); | 
					
						
							| 
									
										
										
										
											2019-05-21 16:18:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     ctx.body = users.map(sanitizeUser); | 
					
						
							| 
									
										
										
										
											2020-04-17 17:33:21 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-07-08 22:07:52 +02:00
										 |  |  |     let data = await getService('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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     ctx.body = data; | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |    * Retrieve user count. | 
					
						
							|  |  |  |    * @return {Number} | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |   async count(ctx) { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |     ctx.body = await getService('user').count(ctx.query); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |    * Destroy a/an user record. | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |    * @return {Object} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |   async destroy(ctx) { | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const data = await getService('user').remove({ id }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     ctx.send(sanitizeUser(data)); | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2019-07-16 16:26:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |    * Retrieve authenticated user. | 
					
						
							|  |  |  |    * @return {Object|Array} | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |   async me(ctx) { | 
					
						
							|  |  |  |     const user = ctx.state.user; | 
					
						
							| 
									
										
										
										
											2018-06-06 16:20:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     if (!user) { | 
					
						
							| 
									
										
										
										
											2021-09-07 09:45:45 +02:00
										 |  |  |       return ctx.badRequest('Unauthenticated request'); | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-06 16:20:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     ctx.body = sanitizeUser(user); | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | }; |