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.
|
|
|
|
|
*/
|
2018-04-30 18:26:56 +02:00
|
|
|
|
const _ = require('lodash');
|
2017-12-04 16:43:24 +01:00
|
|
|
|
const uuid = require('uuid/v4');
|
2021-06-29 16:27:35 +02:00
|
|
|
|
const { getService } = require('../../utils');
|
2017-12-04 16:43:24 +01:00
|
|
|
|
|
2020-06-08 11:01:20 +02:00
|
|
|
|
const usersPermissionsActions = require('../users-permissions-actions');
|
2020-06-04 18:30:26 +02:00
|
|
|
|
|
2019-08-12 15:35:40 +02:00
|
|
|
|
module.exports = async () => {
|
2018-02-06 13:10:43 +01:00
|
|
|
|
const pluginStore = strapi.store({
|
2018-02-13 15:04:21 +01:00
|
|
|
|
environment: '',
|
2018-02-06 13:10:43 +01:00
|
|
|
|
type: 'plugin',
|
2019-04-10 18:11:55 +02:00
|
|
|
|
name: 'users-permissions',
|
2018-02-06 13:10:43 +01:00
|
|
|
|
});
|
|
|
|
|
|
2021-06-29 16:27:35 +02:00
|
|
|
|
await initGrant(pluginStore);
|
|
|
|
|
await initEmails(pluginStore);
|
|
|
|
|
await initAdvancedOptions(pluginStore);
|
|
|
|
|
|
|
|
|
|
await strapi.admin.services.permission.actionProvider.registerMany(
|
|
|
|
|
usersPermissionsActions.actions
|
|
|
|
|
);
|
|
|
|
|
|
2021-07-08 21:53:30 +02:00
|
|
|
|
await getService('users-permissions').initialize();
|
2021-06-29 16:27:35 +02:00
|
|
|
|
|
2021-07-29 16:39:26 +02:00
|
|
|
|
// TODO: adapt with new extension system
|
2021-08-17 19:28:10 +02:00
|
|
|
|
if (!strapi.config.get('plugin.users-permissions.jwtSecret')) {
|
2021-06-29 16:27:35 +02:00
|
|
|
|
const jwtSecret = uuid();
|
2021-08-17 19:28:10 +02:00
|
|
|
|
strapi.config.set('plugin.users-permissions.jwtSecret', jwtSecret),
|
|
|
|
|
(strapi.reload.isWatching = false);
|
2021-06-29 16:27:35 +02:00
|
|
|
|
|
2021-08-13 15:35:19 +02:00
|
|
|
|
if (!process.env.JWT_SECRET) {
|
|
|
|
|
await strapi.fs.appendFile('.env', `JWT_SECRET=${jwtSecret}\n`);
|
|
|
|
|
}
|
2021-06-29 16:27:35 +02:00
|
|
|
|
|
|
|
|
|
strapi.reload.isWatching = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const initGrant = async pluginStore => {
|
2018-04-24 22:18:21 +08:00
|
|
|
|
const grantConfig = {
|
2018-04-21 12:29:00 +08:00
|
|
|
|
email: {
|
|
|
|
|
enabled: true,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
icon: 'envelope',
|
2018-04-21 12:29:00 +08:00
|
|
|
|
},
|
2018-07-29 17:43:45 -07:00
|
|
|
|
discord: {
|
|
|
|
|
enabled: false,
|
2019-11-25 17:08:30 +01:00
|
|
|
|
icon: 'discord',
|
2018-07-29 17:43:45 -07:00
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/discord/callback`,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
scope: ['identify', 'email'],
|
2018-07-29 17:43:45 -07:00
|
|
|
|
},
|
2018-04-21 12:29:00 +08:00
|
|
|
|
facebook: {
|
|
|
|
|
enabled: false,
|
2019-11-25 17:08:30 +01:00
|
|
|
|
icon: 'facebook-square',
|
2018-04-21 12:29:00 +08:00
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/facebook/callback`,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
scope: ['email'],
|
2018-04-21 12:29:00 +08:00
|
|
|
|
},
|
|
|
|
|
google: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'google',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/google/callback`,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
scope: ['email'],
|
2018-04-21 12:29:00 +08:00
|
|
|
|
},
|
|
|
|
|
github: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'github',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/github/callback`,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
scope: ['user', 'user:email'],
|
2018-04-21 12:29:00 +08:00
|
|
|
|
},
|
2018-07-22 22:03:29 -04:00
|
|
|
|
microsoft: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'windows',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/microsoft/callback`,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
scope: ['user.read'],
|
2018-07-22 22:03:29 -04:00
|
|
|
|
},
|
2018-04-21 12:29:00 +08:00
|
|
|
|
twitter: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'twitter',
|
|
|
|
|
key: '',
|
2018-08-23 18:28:13 +02:00
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/twitter/callback`,
|
2019-04-10 18:11:55 +02:00
|
|
|
|
},
|
2019-09-06 09:14:42 +02:00
|
|
|
|
instagram: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'instagram',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/instagram/callback`,
|
2021-01-18 14:53:53 +01:00
|
|
|
|
scope: ['user_profile'],
|
2019-09-06 09:14:42 +02:00
|
|
|
|
},
|
2019-12-06 01:38:06 +03:00
|
|
|
|
vk: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'vk',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
2020-05-08 13:50:00 +02:00
|
|
|
|
callback: `${strapi.config.server.url}/auth/vk/callback`,
|
2019-12-06 01:38:06 +03:00
|
|
|
|
scope: ['email'],
|
|
|
|
|
},
|
2020-05-12 10:48:18 -04:00
|
|
|
|
twitch: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'twitch',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
callback: `${strapi.config.server.url}/auth/twitch/callback`,
|
|
|
|
|
scope: ['user:read:email'],
|
|
|
|
|
},
|
2020-09-04 22:37:30 +10:00
|
|
|
|
linkedin: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'linkedin',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
callback: `${strapi.config.server.url}/auth/linkedin/callback`,
|
|
|
|
|
scope: ['r_liteprofile', 'r_emailaddress'],
|
|
|
|
|
},
|
2020-10-06 11:45:22 +02:00
|
|
|
|
cognito: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'aws',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
subdomain: 'my.subdomain.com',
|
|
|
|
|
callback: `${strapi.config.server.url}/auth/cognito/callback`,
|
|
|
|
|
scope: ['email', 'openid', 'profile'],
|
|
|
|
|
},
|
2021-01-22 13:24:33 -05:00
|
|
|
|
reddit: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'reddit',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
state: true,
|
|
|
|
|
callback: `${strapi.config.server.url}/auth/reddit/callback`,
|
|
|
|
|
scope: ['identity'],
|
|
|
|
|
},
|
2021-01-25 09:09:45 +01:00
|
|
|
|
auth0: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: '',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
subdomain: 'my-tenant.eu',
|
|
|
|
|
callback: `${strapi.config.server.url}/auth/auth0/callback`,
|
|
|
|
|
scope: ['openid', 'email', 'profile'],
|
|
|
|
|
},
|
2021-05-17 11:52:40 -04:00
|
|
|
|
cas: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
icon: 'book',
|
|
|
|
|
key: '',
|
|
|
|
|
secret: '',
|
|
|
|
|
callback: `${strapi.config.server.url}/auth/cas/callback`,
|
|
|
|
|
scope: ['openid email'], // scopes should be space delimited
|
|
|
|
|
subdomain: 'my.subdomain.com/cas',
|
|
|
|
|
},
|
2018-04-21 12:29:00 +08:00
|
|
|
|
};
|
2021-06-29 16:27:35 +02:00
|
|
|
|
|
2019-04-10 18:11:55 +02:00
|
|
|
|
const prevGrantConfig = (await pluginStore.get({ key: 'grant' })) || {};
|
2018-04-21 12:29:00 +08:00
|
|
|
|
// store grant auth config to db
|
|
|
|
|
// when plugin_users-permissions_grant is not existed in db
|
|
|
|
|
// or we have added/deleted provider here.
|
2020-03-02 15:18:08 +01:00
|
|
|
|
if (!prevGrantConfig || !_.isEqual(_.keys(prevGrantConfig), _.keys(grantConfig))) {
|
2018-04-24 22:18:21 +08:00
|
|
|
|
// merge with the previous provider config.
|
2019-04-10 18:11:55 +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
|
|
|
|
|
2021-06-29 16:27:35 +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
|
|
|
|
|
2021-06-29 16:27:35 +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
|
|
|
|
};
|