mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-29 17:04:13 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			149 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			149 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| /**
 | |
|  * User.js service
 | |
|  *
 | |
|  * @description: A set of functions similar to controller's actions to avoid code duplication.
 | |
|  */
 | |
| 
 | |
| const crypto = require('crypto');
 | |
| const bcrypt = require('bcryptjs');
 | |
| const urlJoin = require('url-join');
 | |
| 
 | |
| const { getAbsoluteAdminUrl, getAbsoluteServerUrl, sanitize } = require('@strapi/utils');
 | |
| const { getService } = require('../utils');
 | |
| 
 | |
| module.exports = ({ strapi }) => ({
 | |
|   /**
 | |
|    * Promise to count users
 | |
|    *
 | |
|    * @return {Promise}
 | |
|    */
 | |
| 
 | |
|   count(params) {
 | |
|     return strapi.query('plugin::users-permissions.user').count({ where: params });
 | |
|   },
 | |
| 
 | |
|   /**
 | |
|    * Promise to search count users
 | |
|    *
 | |
|    * @return {Promise}
 | |
|    */
 | |
| 
 | |
|   /**
 | |
|    * Promise to add a/an user.
 | |
|    * @return {Promise}
 | |
|    */
 | |
|   async add(values) {
 | |
|     return strapi.entityService.create('plugin::users-permissions.user', {
 | |
|       data: values,
 | |
|       populate: ['role'],
 | |
|     });
 | |
|   },
 | |
| 
 | |
|   /**
 | |
|    * Promise to edit a/an user.
 | |
|    * @param {string} userId
 | |
|    * @param {object} params
 | |
|    * @return {Promise}
 | |
|    */
 | |
|   async edit(userId, params = {}) {
 | |
|     return strapi.entityService.update('plugin::users-permissions.user', userId, {
 | |
|       data: params,
 | |
|       populate: ['role'],
 | |
|     });
 | |
|   },
 | |
| 
 | |
|   /**
 | |
|    * Promise to fetch a/an user.
 | |
|    * @return {Promise}
 | |
|    */
 | |
|   fetch(id, params) {
 | |
|     return strapi.entityService.findOne('plugin::users-permissions.user', id, params);
 | |
|   },
 | |
| 
 | |
|   /**
 | |
|    * Promise to fetch authenticated user.
 | |
|    * @return {Promise}
 | |
|    */
 | |
|   fetchAuthenticatedUser(id) {
 | |
|     return strapi
 | |
|       .query('plugin::users-permissions.user')
 | |
|       .findOne({ where: { id }, populate: ['role'] });
 | |
|   },
 | |
| 
 | |
|   /**
 | |
|    * Promise to fetch all users.
 | |
|    * @return {Promise}
 | |
|    */
 | |
|   fetchAll(params) {
 | |
|     return strapi.entityService.findMany('plugin::users-permissions.user', params);
 | |
|   },
 | |
| 
 | |
|   /**
 | |
|    * Promise to remove a/an user.
 | |
|    * @return {Promise}
 | |
|    */
 | |
|   async remove(params) {
 | |
|     return strapi.query('plugin::users-permissions.user').delete({ where: params });
 | |
|   },
 | |
| 
 | |
|   validatePassword(password, hash) {
 | |
|     return bcrypt.compare(password, hash);
 | |
|   },
 | |
| 
 | |
|   async sendConfirmationEmail(user) {
 | |
|     const userPermissionService = getService('users-permissions');
 | |
|     const pluginStore = await strapi.store({ type: 'plugin', name: 'users-permissions' });
 | |
|     const userSchema = strapi.getModel('plugin::users-permissions.user');
 | |
| 
 | |
|     const settings = await pluginStore
 | |
|       .get({ key: 'email' })
 | |
|       .then((storeEmail) => storeEmail.email_confirmation.options);
 | |
| 
 | |
|     // Sanitize the template's user information
 | |
|     const sanitizedUserInfo = await sanitize.sanitizers.defaultSanitizeOutput(userSchema, user);
 | |
| 
 | |
|     const confirmationToken = crypto.randomBytes(20).toString('hex');
 | |
| 
 | |
|     await this.edit(user.id, { confirmationToken });
 | |
| 
 | |
|     const apiPrefix = strapi.config.get('api.rest.prefix');
 | |
| 
 | |
|     try {
 | |
|       settings.message = await userPermissionService.template(settings.message, {
 | |
|         URL: urlJoin(getAbsoluteServerUrl(strapi.config), apiPrefix, '/auth/email-confirmation'),
 | |
|         SERVER_URL: getAbsoluteServerUrl(strapi.config),
 | |
|         ADMIN_URL: getAbsoluteAdminUrl(strapi.config),
 | |
|         USER: sanitizedUserInfo,
 | |
|         CODE: confirmationToken,
 | |
|       });
 | |
| 
 | |
|       settings.object = await userPermissionService.template(settings.object, {
 | |
|         USER: sanitizedUserInfo,
 | |
|       });
 | |
|     } catch {
 | |
|       strapi.log.error(
 | |
|         '[plugin::users-permissions.sendConfirmationEmail]: Failed to generate a template for "user confirmation email". Please make sure your email template is valid and does not contain invalid characters or patterns'
 | |
|       );
 | |
|       return;
 | |
|     }
 | |
| 
 | |
|     // Send an email to the user.
 | |
|     await strapi
 | |
|       .plugin('email')
 | |
|       .service('email')
 | |
|       .send({
 | |
|         to: user.email,
 | |
|         from:
 | |
|           settings.from.email && settings.from.name
 | |
|             ? `${settings.from.name} <${settings.from.email}>`
 | |
|             : undefined,
 | |
|         replyTo: settings.response_email,
 | |
|         subject: settings.object,
 | |
|         text: settings.message,
 | |
|         html: settings.message,
 | |
|       });
 | |
|   },
 | |
| });
 | 
