2019-07-16 17:23:40 +02:00

117 lines
2.8 KiB
JavaScript

'use strict';
/**
* User.js service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
*/
const bcrypt = require('bcryptjs');
module.exports = {
/**
* Promise to add a/an user.
* @return {Promise}
*/
async add(values) {
if (values.password) {
values.password = await strapi.plugins[
'users-permissions'
].services.user.hashPassword(values);
}
return strapi.query('user', 'users-permissions').create(values);
},
/**
* Promise to edit a/an user.
* @return {Promise}
*/
async edit(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.
if (values.password) {
values.password = await strapi.plugins[
'users-permissions'
].services.user.hashPassword(values);
}
return strapi.query('user', 'users-permissions').update(params, values);
},
/**
* Promise to fetch a/an user.
* @return {Promise}
*/
fetch(params) {
return strapi.query('user', 'users-permissions').findOne(params);
},
/**
* Promise to fetch all users.
* @return {Promise}
*/
fetchAll(params, populate) {
return strapi.query('user', 'users-permissions').find(params, populate);
},
hashPassword(user = {}) {
return new Promise(resolve => {
if (!user.password || this.isHashed(user.password)) {
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;
},
/**
* Promise to remove a/an user.
* @return {Promise}
*/
async remove(params) {
return strapi.query('user', 'users-permissions').delete(params);
},
async removeAll(params, query) {
// Use Content Manager business logic to handle relation.
if (strapi.plugins['content-manager']) {
params.model = 'user';
query.source = 'users-permissions';
return await strapi.plugins['content-manager'].services[
'contentmanager'
].deleteMany(params, query);
}
// TODO remove this logic when we develop plugins' dependencies
const primaryKey = strapi.query('user', 'users-permissions').primaryKey;
const toRemove = Object.keys(query).reduce((acc, curr) => {
if (curr !== 'source') {
return acc.concat([query[curr]]);
}
return acc;
}, []);
return strapi.query('user', 'users-permissions').deleteMany({
[primaryKey]: toRemove,
});
},
validatePassword(password, hash) {
return bcrypt.compareSync(password, hash);
},
};