2021-07-08 18:15:32 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2021-10-20 17:30:05 +02:00
|
|
|
const { ValidationError } = require('@strapi/utils').errors;
|
2021-07-08 18:15:32 +02:00
|
|
|
const { getService } = require('../utils');
|
|
|
|
const { isValidEmailTemplate } = require('./validation/email-template');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
async getEmailTemplate(ctx) {
|
2021-09-13 12:03:12 +02:00
|
|
|
ctx.send(await strapi.store({ type: 'plugin', name: 'users-permissions', key: 'email' }).get());
|
2021-07-08 18:15:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async updateEmailTemplate(ctx) {
|
|
|
|
if (_.isEmpty(ctx.request.body)) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ValidationError('Request body cannot be empty');
|
2021-07-08 18:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const emailTemplates = ctx.request.body['email-templates'];
|
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
for (const key of Object.keys(emailTemplates)) {
|
2021-07-08 18:15:32 +02:00
|
|
|
const template = emailTemplates[key].options.message;
|
|
|
|
|
|
|
|
if (!isValidEmailTemplate(template)) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ValidationError('Invalid template');
|
2021-07-08 18:15:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await strapi
|
2021-09-13 12:03:12 +02:00
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'email' })
|
2021-07-08 18:15:32 +02:00
|
|
|
.set({ value: emailTemplates });
|
|
|
|
|
|
|
|
ctx.send({ ok: true });
|
|
|
|
},
|
|
|
|
|
|
|
|
async getAdvancedSettings(ctx) {
|
2021-09-07 09:45:45 +02:00
|
|
|
const settings = await strapi
|
2021-09-13 12:03:12 +02:00
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
|
2021-09-07 09:45:45 +02:00
|
|
|
.get();
|
|
|
|
|
2022-03-03 22:55:29 +09:00
|
|
|
const roles = await getService('role').find();
|
2021-09-07 09:45:45 +02:00
|
|
|
|
|
|
|
ctx.send({ settings, roles });
|
2021-07-08 18:15:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async updateAdvancedSettings(ctx) {
|
|
|
|
if (_.isEmpty(ctx.request.body)) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ValidationError('Request body cannot be empty');
|
2021-07-08 18:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await strapi
|
2021-09-13 12:03:12 +02:00
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
|
2021-07-08 18:15:32 +02:00
|
|
|
.set({ value: ctx.request.body });
|
|
|
|
|
|
|
|
ctx.send({ ok: true });
|
|
|
|
},
|
|
|
|
|
|
|
|
async getProviders(ctx) {
|
|
|
|
const providers = await strapi
|
2021-09-13 12:03:12 +02:00
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'grant' })
|
2021-07-08 18:15:32 +02:00
|
|
|
.get();
|
|
|
|
|
|
|
|
for (const provider in providers) {
|
|
|
|
if (provider !== 'email') {
|
2021-08-19 22:27:00 +02:00
|
|
|
providers[provider].redirectUri = strapi
|
|
|
|
.plugin('users-permissions')
|
|
|
|
.service('providers')
|
|
|
|
.buildRedirectUri(provider);
|
2021-07-08 18:15:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.send(providers);
|
|
|
|
},
|
|
|
|
|
|
|
|
async updateProviders(ctx) {
|
|
|
|
if (_.isEmpty(ctx.request.body)) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ValidationError('Request body cannot be empty');
|
2021-07-08 18:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await strapi
|
2021-09-13 12:03:12 +02:00
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'grant' })
|
2021-07-08 18:15:32 +02:00
|
|
|
.set({ value: ctx.request.body.providers });
|
|
|
|
|
|
|
|
ctx.send({ ok: true });
|
|
|
|
},
|
|
|
|
};
|