171 lines
4.2 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 { getAbsoluteServerUrl, 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) {
if (values.password) {
2021-08-02 08:28:10 +02:00
values.password = await getService('user').hashPassword(values);
}
return strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
.create({ 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 = {}) {
if (params.password) {
params.password = await getService('user').hashPassword(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(params, populate) {
2021-08-06 18:09:49 +02:00
return strapi.query('plugin::users-permissions.user').findOne({ where: params, populate });
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, populate) {
2021-08-06 18:09:49 +02:00
return strapi.query('plugin::users-permissions.user').findMany({ where: params, populate });
2017-11-16 14:12:03 +01:00
},
hashPassword(user = {}) {
return new Promise((resolve, reject) => {
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) => {
if (err) {
return reject(err);
}
resolve(hash);
2017-11-16 14:12:03 +01:00
});
}
});
},
isHashed(password) {
2017-11-16 14:12:03 +01:00
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}
*/
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' })
.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 });
settings.message = await userPermissionService.template(settings.message, {
URL: `${getAbsoluteServerUrl(strapi.config)}/auth/email-confirmation`,
USER: sanitizedUserInfo,
CODE: confirmationToken,
});
settings.object = await userPermissionService.template(settings.object, {
USER: sanitizedUserInfo,
});
// 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
});