153 lines
4.0 KiB
JavaScript
Raw Normal View History

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.
*/
const crypto = require('crypto');
const bcrypt = require('bcryptjs');
const urlJoin = require('url-join');
const { sanitize } = require('@strapi/utils');
2021-07-08 18:15:32 +02:00
const { getService } = require('../utils');
2017-11-14 11:11:22 +01:00
2021-07-08 11:20:13 +02:00
module.exports = ({ strapi }) => ({
/**
* Promise to count users
*
* @return {Promise}
*/
count(params) {
2021-08-06 18:09:49 +02:00
return strapi.query('plugin::users-permissions.user').count({ where: params });
},
/**
* Promise to search count users
*
* @return {Promise}
*/
2017-11-14 11:11:22 +01:00
/**
* Promise to add a/an user.
* @return {Promise}
*/
async add(values) {
2022-01-27 10:15:04 +01:00
return strapi.entityService.create('plugin::users-permissions.user', {
data: values,
populate: ['role'],
});
2017-11-14 11:11:22 +01:00
},
/**
* Promise to edit a/an user.
* @param {string} userId
* @param {object} params
2017-11-14 11:11:22 +01:00
* @return {Promise}
*/
async edit(userId, params = {}) {
return strapi.entityService.update('plugin::users-permissions.user', userId, {
data: params,
populate: ['role'],
});
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}
*/
fetch(id, params) {
return strapi.entityService.findOne('plugin::users-permissions.user', id, params);
2017-12-07 15:27:11 +01:00
},
/**
* Promise to fetch authenticated user.
* @return {Promise}
*/
fetchAuthenticatedUser(id) {
2021-07-08 18:15:32 +02:00
return strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.findOne({ where: { id }, populate: ['role'] });
},
2017-12-07 15:27:11 +01:00
/**
* Promise to fetch all users.
* @return {Promise}
*/
fetchAll(params) {
return strapi.entityService.findMany('plugin::users-permissions.user', params);
2017-11-16 14:12:03 +01:00
},
2017-12-07 15:27:11 +01:00
/**
* Promise to remove a/an user.
* @return {Promise}
*/
async remove(params) {
2021-08-06 18:09:49 +02:00
return strapi.query('plugin::users-permissions.user').delete({ where: params });
2017-12-07 15:27:11 +01:00
},
validatePassword(password, hash) {
return bcrypt.compare(password, hash);
},
async sendConfirmationEmail(user) {
2021-08-02 08:28:10 +02:00
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' })
2022-08-08 23:33:39 +02:00
.then((storeEmail) => storeEmail.email_confirmation.options);
// Sanitize the template's user information
2021-11-10 17:08:54 +01:00
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(
strapi.config.get('server.absoluteUrl'),
apiPrefix,
'/auth/email-confirmation'
),
SERVER_URL: strapi.config.get('server.absoluteUrl'),
ADMIN_URL: strapi.config.get('admin.absoluteUrl'),
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.
2021-08-19 22:27:00 +02:00
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,
});
},
2021-07-08 11:20:13 +02:00
});