| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * User.js service | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @description: A set of functions similar to controller's actions to avoid code duplication. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public dependencies.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2017-11-16 14:12:03 +01:00
										 |  |  | const bcrypt = require('bcryptjs'); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Promise to add a/an user. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * @return {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   add: async (values) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 16:10:13 +01:00
										 |  |  |     if (values.password) { | 
					
						
							|  |  |  |       values.password = await strapi.plugins['users-permissions'].services.user.hashPassword(values); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 18:23:15 +01:00
										 |  |  |     // Use Content Manager business logic to handle relation.
 | 
					
						
							|  |  |  |     if (strapi.plugins['content-manager']) { | 
					
						
							|  |  |  |       return await strapi.plugins['content-manager'].services['contentmanager'].add({ | 
					
						
							|  |  |  |         model: 'user' | 
					
						
							|  |  |  |       }, values, 'users-permissions'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 17:09:19 +01:00
										 |  |  |     return strapi.query('user', 'users-permissions').create(values); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Promise to edit a/an user. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * @return {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   edit: async (params, values) => { | 
					
						
							|  |  |  |     // Note: The current method will return the full response of Mongo.
 | 
					
						
							|  |  |  |     // To get the updated object, you have to execute the `findOne()` method
 | 
					
						
							|  |  |  |     // or use the `findOneOrUpdate()` method with `{ new:true }` option.
 | 
					
						
							| 
									
										
										
										
											2017-11-29 16:10:13 +01:00
										 |  |  |     if (values.password) { | 
					
						
							|  |  |  |       values.password = await strapi.plugins['users-permissions'].services.user.hashPassword(values); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 18:23:15 +01:00
										 |  |  |     // Use Content Manager business logic to handle relation.
 | 
					
						
							|  |  |  |     if (strapi.plugins['content-manager']) { | 
					
						
							| 
									
										
										
										
											2018-02-02 12:59:34 +01:00
										 |  |  |       params.model = 'user'; | 
					
						
							| 
									
										
										
										
											2018-02-05 12:41:10 +01:00
										 |  |  |       params.id = (params._id || params.id); | 
					
						
							| 
									
										
										
										
											2018-02-02 12:59:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 18:23:15 +01:00
										 |  |  |       return await strapi.plugins['content-manager'].services['contentmanager'].edit(params, values, 'users-permissions'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 18:45:51 +01:00
										 |  |  |     return strapi.query('user', 'users-permissions').update(_.assign(params, values)); | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2017-12-07 15:27:11 +01:00
										 |  |  |    * Promise to fetch a/an user. | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  |    * | 
					
						
							|  |  |  |    * @return {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 15:27:11 +01:00
										 |  |  |   fetch: (params) => { | 
					
						
							| 
									
										
										
										
											2017-12-11 12:31:26 +01:00
										 |  |  |     return strapi.query('user', 'users-permissions').findOne(_.pick(params, ['_id', 'id'])); | 
					
						
							| 
									
										
										
										
											2017-12-07 15:27:11 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Promise to fetch all users. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * @return {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fetchAll: (params) => { | 
					
						
							|  |  |  |     return strapi.query('user', 'users-permissions').find(strapi.utils.models.convertParams('user', params)); | 
					
						
							| 
									
										
										
										
											2017-11-16 14:12:03 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 15:46:28 +01:00
										 |  |  |   hashPassword: function (user = {}) { | 
					
						
							| 
									
										
										
										
											2017-11-16 14:12:03 +01:00
										 |  |  |     return new Promise((resolve) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 15:46:28 +01:00
										 |  |  |       if (!user.password || this.isHashed(user.password)) { | 
					
						
							| 
									
										
										
										
											2017-11-16 14:12:03 +01:00
										 |  |  |         resolve(null); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         bcrypt.hash(user.password, 10, (err, hash) => { | 
					
						
							|  |  |  |           resolve(hash) | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   isHashed: (password) => { | 
					
						
							|  |  |  |     if (typeof password !== 'string' || !password) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return password.split('$').length === 4; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-11-16 14:29:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 15:27:11 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Promise to remove a/an user. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * @return {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   remove: async params => { | 
					
						
							| 
									
										
										
										
											2017-12-11 18:23:15 +01:00
										 |  |  |     // Use Content Manager business logic to handle relation.
 | 
					
						
							|  |  |  |     if (strapi.plugins['content-manager']) { | 
					
						
							| 
									
										
										
										
											2018-02-19 16:00:37 +01:00
										 |  |  |       params.model = 'user'; | 
					
						
							|  |  |  |       params.id = (params._id || params.id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await strapi.plugins['content-manager'].services['contentmanager'].delete(params, {source: 'users-permissions'}); | 
					
						
							| 
									
										
										
										
											2017-12-11 18:23:15 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 15:27:11 +01:00
										 |  |  |     return strapi.query('user', 'users-permissions').delete(params); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 14:29:49 +01:00
										 |  |  |   validatePassword: (password, hash) => { | 
					
						
							|  |  |  |     return bcrypt.compareSync(password, hash); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-11-14 11:11:22 +01:00
										 |  |  | }; |