86 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

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) {
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
.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
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
2021-09-07 09:45:45 +02:00
.get();
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
.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
.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
.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 });
},
};