2017-11-17 11:17:20 +01:00
'use strict' ;
/ * *
* An asynchronous bootstrap function that runs before
* your application gets started .
*
* This gives you an opportunity to set up your data model ,
* run jobs , or perform some special logic .
* /
2022-01-24 18:13:27 +01:00
const crypto = require ( 'crypto' ) ;
2018-04-30 18:26:56 +02:00
const _ = require ( 'lodash' ) ;
2021-08-19 16:49:33 +02:00
const { getService } = require ( '../utils' ) ;
const usersPermissionsActions = require ( './users-permissions-actions' ) ;
2020-06-04 18:30:26 +02:00
2022-08-08 23:33:39 +02:00
const initGrant = async ( pluginStore ) => {
2024-06-26 11:06:02 +02:00
const allProviders = getService ( 'providers-registry' ) . getAll ( ) ;
2021-10-26 16:51:29 +02:00
2024-06-26 11:06:02 +02:00
const grantConfig = Object . entries ( allProviders ) . reduce ( ( acc , [ name , provider ] ) => {
const { icon , enabled , grantConfig } = provider ;
acc [ name ] = {
icon ,
enabled ,
... grantConfig ,
} ;
return acc ;
} , { } ) ;
2021-06-29 16:27:35 +02:00
2019-04-10 18:11:55 +02:00
const prevGrantConfig = ( await pluginStore . get ( { key : 'grant' } ) ) || { } ;
2024-06-26 11:06:02 +02:00
if ( ! prevGrantConfig || ! _ . isEqual ( prevGrantConfig , grantConfig ) ) {
2018-04-24 22:18:21 +08:00
// merge with the previous provider config.
2022-08-08 23:33:39 +02:00
_ . keys ( grantConfig ) . forEach ( ( key ) => {
2018-04-24 22:18:21 +08:00
if ( key in prevGrantConfig ) {
grantConfig [ key ] = _ . merge ( grantConfig [ key ] , prevGrantConfig [ key ] ) ;
}
} ) ;
2019-04-10 18:11:55 +02:00
await pluginStore . set ( { key : 'grant' , value : grantConfig } ) ;
2018-01-12 15:20:13 +01:00
}
2021-06-29 16:27:35 +02:00
} ;
2018-01-12 15:20:13 +01:00
2022-08-08 23:33:39 +02:00
const initEmails = async ( pluginStore ) => {
2019-04-10 18:11:55 +02:00
if ( ! ( await pluginStore . get ( { key : 'email' } ) ) ) {
2018-02-06 13:10:43 +01:00
const value = {
2019-04-10 18:11:55 +02:00
reset _password : {
2018-02-01 18:12:38 +01:00
display : 'Email.template.reset_password' ,
2019-11-19 16:17:15 +01:00
icon : 'sync' ,
2018-02-01 18:12:38 +01:00
options : {
from : {
name : 'Administration Panel' ,
2019-04-10 18:11:55 +02:00
email : 'no-reply@strapi.io' ,
2018-02-01 18:12:38 +01:00
} ,
response _email : '' ,
2020-01-11 11:29:24 -05:00
object : 'Reset password' ,
2018-02-01 18:12:38 +01:00
message : ` <p>We heard that you lost your password. Sorry about that!</p>
2018-01-15 14:50:53 +01:00
2018-01-26 17:31:52 +01:00
< p > But don ’ t worry ! You can use the following link to reset your password : < / p >
2018-01-26 17:37:01 +01:00
< p > < %= URL % > ? code = < %= TOKEN % > < / p >
2018-01-15 14:50:53 +01:00
2019-04-10 18:11:55 +02:00
< p > Thanks . < / p > ` ,
} ,
2018-08-08 17:57:02 +02:00
} ,
2019-04-10 18:11:55 +02:00
email _confirmation : {
2018-08-08 17:57:02 +02:00
display : 'Email.template.email_confirmation' ,
2019-11-19 16:17:15 +01:00
icon : 'check-square' ,
2018-08-08 17:57:02 +02:00
options : {
from : {
name : 'Administration Panel' ,
2019-04-10 18:11:55 +02:00
email : 'no-reply@strapi.io' ,
2018-08-08 17:57:02 +02:00
} ,
response _email : '' ,
object : 'Account confirmation' ,
2018-08-27 08:11:23 -07:00
message : ` <p>Thank you for registering!</p>
2018-08-08 17:57:02 +02:00
2018-08-23 12:00:46 +02:00
< p > You have to confirm your email address . Please click on the link below . < / p >
2018-08-08 17:57:02 +02:00
< p > < %= URL % > ? confirmation = < %= CODE % > < / p >
2019-04-10 18:11:55 +02:00
< p > Thanks . < / p > ` ,
} ,
} ,
2018-02-01 18:12:38 +01:00
} ;
2018-01-15 11:29:38 +01:00
2019-04-10 18:11:55 +02:00
await pluginStore . set ( { key : 'email' , value } ) ;
2018-01-15 11:29:38 +01:00
}
2021-06-29 16:27:35 +02:00
} ;
2018-01-15 11:29:38 +01:00
2022-08-08 23:33:39 +02:00
const initAdvancedOptions = async ( pluginStore ) => {
2019-04-10 18:11:55 +02:00
if ( ! ( await pluginStore . get ( { key : 'advanced' } ) ) ) {
2018-02-06 13:10:43 +01:00
const value = {
2018-02-01 18:12:38 +01:00
unique _email : true ,
2018-03-12 15:56:25 +01:00
allow _register : true ,
2018-08-08 17:57:02 +02:00
email _confirmation : false ,
2020-07-17 14:24:31 +02:00
email _reset _password : null ,
email _confirmation _redirection : null ,
2019-04-10 18:11:55 +02:00
default _role : 'authenticated' ,
2018-02-01 18:12:38 +01:00
} ;
2018-01-15 15:01:21 +01:00
2019-04-10 18:11:55 +02:00
await pluginStore . set ( { key : 'advanced' , value } ) ;
2018-01-15 15:01:21 +01:00
}
2017-11-17 11:17:20 +01:00
} ;
2022-08-08 23:33:39 +02:00
module . exports = async ( { strapi } ) => {
const pluginStore = strapi . store ( { type : 'plugin' , name : 'users-permissions' } ) ;
await initGrant ( pluginStore ) ;
await initEmails ( pluginStore ) ;
await initAdvancedOptions ( pluginStore ) ;
2024-03-27 23:16:51 +01:00
await strapi
. service ( 'admin::permission' )
. actionProvider . registerMany ( usersPermissionsActions . actions ) ;
2022-08-08 23:33:39 +02:00
await getService ( 'users-permissions' ) . initialize ( ) ;
2024-03-11 12:28:46 +01:00
if ( ! strapi . config . get ( 'plugin::users-permissions.jwtSecret' ) ) {
2022-08-08 23:33:39 +02:00
if ( process . env . NODE _ENV !== 'development' ) {
throw new Error (
` Missing jwtSecret. Please, set configuration variable "jwtSecret" for the users-permissions plugin in config/plugins.js (ex: you can generate one using Node with \` crypto.randomBytes(16).toString('base64') \` ).
For security reasons , prefer storing the secret in an environment variable and read it in config / plugins . js . See https : //docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/optional/environment.html#configuration-using-environment-variables.`
) ;
}
const jwtSecret = crypto . randomBytes ( 16 ) . toString ( 'base64' ) ;
2024-03-11 12:28:46 +01:00
strapi . config . set ( 'plugin::users-permissions.jwtSecret' , jwtSecret ) ;
2022-08-08 23:33:39 +02:00
if ( ! process . env . JWT _SECRET ) {
const envPath = process . env . ENV _PATH || '.env' ;
strapi . fs . appendFile ( envPath , ` JWT_SECRET= ${ jwtSecret } \n ` ) ;
strapi . log . info (
` The Users & Permissions plugin automatically generated a jwt secret and stored it in ${ envPath } under the name JWT_SECRET. `
) ;
}
}
} ;