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 .
* /
2020-10-16 16:53:40 +02:00
const crypto = require ( 'crypto' ) ;
2020-10-27 11:27:17 +01:00
const bcrypt = require ( 'bcryptjs' ) ;
2022-01-10 18:11:32 +01:00
const urlJoin = require ( 'url-join' ) ;
2020-10-16 16:53:40 +02:00
2024-03-25 12:32:56 +01:00
const { sanitize } = require ( '@strapi/utils' ) ;
2024-04-18 15:58:46 +02:00
const { toNumber , getOr } = require ( 'lodash/fp' ) ;
2021-07-08 18:15:32 +02:00
const { getService } = require ( '../utils' ) ;
2017-11-14 11:11:22 +01:00
2024-03-11 11:35:58 +01:00
const USER _MODEL _UID = 'plugin::users-permissions.user' ;
2021-07-08 11:20:13 +02:00
module . exports = ( { strapi } ) => ( {
2020-04-17 17:33:21 +02:00
/ * *
* Promise to count users
*
* @ return { Promise }
* /
count ( params ) {
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . count ( { where : params } ) ;
2020-04-17 17:33:21 +02:00
} ,
2020-04-24 10:30:37 +02:00
/ * *
2024-04-18 15:58:46 +02:00
* Hashes password fields in the provided values object if they are present .
* It checks each key in the values object against the model ' s attributes and
* hashes it if the attribute type is 'password' ,
2020-04-24 10:30:37 +02:00
*
2024-04-18 15:58:46 +02:00
* @ param { object } values - The object containing the fields to be hashed .
* @ return { object } The values object with hashed password fields if they were present .
2020-04-24 10:30:37 +02:00
* /
2024-04-18 15:58:46 +02:00
async ensureHashedPasswords ( values ) {
const attributes = strapi . getModel ( USER _MODEL _UID ) . attributes ;
for ( const key in values ) {
if ( attributes [ key ] && attributes [ key ] . type === 'password' ) {
// Check if a custom encryption.rounds has been set on the password attribute
const rounds = toNumber ( getOr ( 10 , 'encryption.rounds' , attributes [ key ] ) ) ;
values [ key ] = await bcrypt . hash ( values [ key ] , rounds ) ;
}
}
return values ;
} ,
2020-04-24 10:30:37 +02:00
2017-11-14 11:11:22 +01:00
/ * *
* Promise to add a / an user .
* @ return { Promise }
* /
2019-07-15 23:16:50 +02:00
async add ( values ) {
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . create ( {
2024-04-18 15:58:46 +02:00
data : await this . ensureHashedPasswords ( values ) ,
2022-01-27 10:15:04 +01:00
populate : [ 'role' ] ,
} ) ;
2017-11-14 11:11:22 +01:00
} ,
/ * *
* Promise to edit a / an user .
2022-01-05 23:54:58 +09:00
* @ param { string } userId
* @ param { object } params
2017-11-14 11:11:22 +01:00
* @ return { Promise }
* /
2022-01-05 23:54:58 +09:00
async edit ( userId , params = { } ) {
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . update ( {
where : { id : userId } ,
2024-04-18 15:58:46 +02:00
data : await this . ensureHashedPasswords ( params ) ,
2022-01-10 18:11:32 +01:00
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 }
* /
2022-03-03 22:56:58 +09:00
fetch ( id , params ) {
2024-03-25 12:32:56 +01:00
const query = strapi . get ( 'query-params' ) . transform ( USER _MODEL _UID , params ? ? { } ) ;
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . findOne ( {
... query ,
where : {
$and : [ { id } , query . where || { } ] ,
} ,
} ) ;
2017-12-07 15:27:11 +01:00
} ,
2020-07-28 10:18:18 +03:00
/ * *
* Promise to fetch authenticated user .
* @ return { Promise }
* /
fetchAuthenticatedUser ( id ) {
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . findOne ( { where : { id } , populate : [ 'role' ] } ) ;
2020-07-28 10:18:18 +03:00
} ,
2017-12-07 15:27:11 +01:00
/ * *
* Promise to fetch all users .
* @ return { Promise }
* /
2022-03-03 22:56:58 +09:00
fetchAll ( params ) {
2024-03-25 12:32:56 +01:00
const query = strapi . get ( 'query-params' ) . transform ( USER _MODEL _UID , params ? ? { } ) ;
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . findMany ( query ) ;
2017-11-16 14:12:03 +01:00
} ,
2017-12-07 15:27:11 +01:00
/ * *
* Promise to remove a / an user .
* @ return { Promise }
* /
2019-07-15 23:16:50 +02:00
async remove ( params ) {
2024-03-11 11:35:58 +01:00
return strapi . db . query ( USER _MODEL _UID ) . delete ( { where : params } ) ;
2017-12-07 15:27:11 +01:00
} ,
2019-07-15 23:16:50 +02:00
validatePassword ( password , hash ) {
2020-09-01 20:33:37 +05:30
return bcrypt . compare ( password , hash ) ;
2019-07-15 23:16:50 +02:00
} ,
2020-10-16 16:53:40 +02:00
async sendConfirmationEmail ( user ) {
2021-08-02 08:28:10 +02:00
const userPermissionService = getService ( 'users-permissions' ) ;
2021-09-13 12:03:12 +02:00
const pluginStore = await strapi . store ( { type : 'plugin' , name : 'users-permissions' } ) ;
2024-03-11 11:35:58 +01:00
const userSchema = strapi . getModel ( USER _MODEL _UID ) ;
2020-10-16 16:53:40 +02:00
const settings = await pluginStore
. get ( { key : 'email' } )
2022-08-08 23:33:39 +02:00
. then ( ( storeEmail ) => storeEmail . email _confirmation . options ) ;
2020-10-16 16:53:40 +02:00
2021-11-05 10:45:25 +01:00
// Sanitize the template's user information
2024-09-16 10:26:16 +02:00
const sanitizedUserInfo = await sanitize . sanitizers . defaultSanitizeOutput (
{
schema : userSchema ,
getModel : strapi . getModel . bind ( strapi ) ,
} ,
user
) ;
2021-11-05 10:45:25 +01:00
2020-10-16 16:53:40 +02:00
const confirmationToken = crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
2022-01-05 23:54:58 +09:00
await this . edit ( user . id , { confirmationToken } ) ;
2020-10-16 16:53:40 +02:00
2022-01-10 18:11:32 +01:00
const apiPrefix = strapi . config . get ( 'api.rest.prefix' ) ;
2020-10-16 16:53:40 +02:00
2022-12-30 19:01:04 +01:00
try {
settings . message = await userPermissionService . template ( settings . message , {
2024-01-19 10:09:53 +01:00
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' ) ,
2022-12-30 19:01:04 +01:00
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 ;
}
2020-10-16 16:53:40 +02:00
// 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 ,
} ) ;
2020-10-16 16:53:40 +02:00
} ,
2021-07-08 11:20:13 +02:00
} ) ;