245 lines
6.3 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* UsersPermissions.js controller
*
* @description: A set of functions called "actions" of the `users-permissions` plugin.
*/
const _ = require('lodash');
module.exports = {
/**
* Default action.
*
* @return {Object}
*/
2017-11-23 17:13:46 +01:00
createRole: async (ctx) => {
2017-11-23 11:03:26 +01:00
if (_.isEmpty(ctx.request.body)) {
return ctx.badRequest(null, [{ messages: [{ id: 'Cannot be empty' }] }]);
}
2017-11-23 17:13:46 +01:00
2017-11-23 11:03:26 +01:00
try {
2017-11-27 16:49:56 +01:00
await strapi.plugins['users-permissions'].services.userspermissions.createRole(ctx.request.body);
2017-11-23 11:03:26 +01:00
ctx.send({ ok: true });
} catch(err) {
ctx.badRequest(null, [{ messages: [{ id: 'An error occured' }] }]);
}
},
2017-11-23 18:08:14 +01:00
deleteProvider: async ctx => {
const { provider } = ctx.params;
if (!provider) {
return ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
}
// TODO handle dynamic
ctx.send({ ok: true });
2017-11-23 18:08:14 +01:00
},
deleteRole: async ctx => {
2018-03-12 16:37:20 +01:00
// Fetch root and public role.
const [root, publicRole] = await Promise.all([
strapi.query('role', 'users-permissions').findOne({ type: 'root' }),
2018-03-12 16:37:20 +01:00
strapi.query('role', 'users-permissions').findOne({ type: 'public' })
]);
const rootID = root.id || root._id;
2018-03-12 16:37:20 +01:00
const publicRoleID = publicRole.id || publicRole._id;
2017-11-23 18:08:14 +01:00
const roleID = ctx.params.role;
if (!roleID) {
2017-11-23 18:08:14 +01:00
return ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
}
// Prevent from removing the root role.
2018-03-12 16:37:20 +01:00
if (roleID.toString() === rootID.toString() || roleID.toString() === publicRoleID.toString()) {
2017-11-27 17:02:45 +01:00
return ctx.badRequest(null, [{ messages: [{ id: 'Unauthorized' }] }]);
}
try {
2018-03-12 16:37:20 +01:00
await strapi.plugins['users-permissions'].services.userspermissions.deleteRole(roleID, publicRoleID);
ctx.send({ ok: true });
2017-11-27 17:02:45 +01:00
} catch(err) {
ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
2017-11-27 17:02:45 +01:00
}
2017-11-23 18:08:14 +01:00
},
2017-11-23 17:13:46 +01:00
getPermissions: async (ctx) => {
try {
const { lang } = ctx.query;
const plugins = await strapi.plugins['users-permissions'].services.userspermissions.getPlugins(lang);
const permissions = await strapi.plugins['users-permissions'].services.userspermissions.getActions(plugins);
2017-11-16 17:59:41 +01:00
ctx.send({ permissions });
} catch(err) {
ctx.badRequest(null, [{ message: [{ id: 'Not Found' }] }]);
}
},
2017-11-29 18:45:51 +01:00
getPolicies: async (ctx) => {
ctx.send({
2017-11-29 18:45:51 +01:00
policies: _.without(_.keys(strapi.plugins['users-permissions'].config.policies), 'permissions')
});
},
2017-11-23 17:13:46 +01:00
getRole: async (ctx) => {
const { id } = ctx.params;
const { lang } = ctx.query;
const plugins = await strapi.plugins['users-permissions'].services.userspermissions.getPlugins(lang);
const role = await strapi.plugins['users-permissions'].services.userspermissions.getRole(id, plugins);
if (_.isEmpty(role)) {
2017-11-20 14:35:24 +01:00
return ctx.badRequest(null, [{ messages: [{ id: `Role don't exist` }] }]);
}
ctx.send({ role });
},
2017-11-27 16:04:57 +01:00
getRoles: async (ctx) => {
try {
const roles = await strapi.plugins['users-permissions'].services.userspermissions.getRoles();
ctx.send({ roles });
} catch(err) {
ctx.badRequest(null, [{ messages: [{ id: 'Not found' }] }]);
}
},
2017-11-30 16:34:43 +01:00
getRoutes: async (ctx) => {
try {
const routes = await strapi.plugins['users-permissions'].services.userspermissions.getRoutes();
ctx.send({ routes });
} catch(err) {
ctx.badRequest(null, [{ messages: [{ id: 'Not found' }] }]);
}
},
index: async (ctx) => {
// Add your own logic here.
// Send 200 `ok`
ctx.send({
message: 'ok'
});
},
init: async (ctx) => {
const role = await strapi.query('role', 'users-permissions').findOne({ type: 'root' }, ['users']);
ctx.send({ hasAdmin: !_.isEmpty(role.users) });
2017-11-23 17:13:46 +01:00
},
searchUsers: async (ctx) => {
const data = await strapi.query('user', 'users-permissions').search(ctx.params);
ctx.send(data);
},
2018-01-22 18:19:44 +01:00
updateRole: async function (ctx) {
// Fetch root role.
const root = await strapi.query('role', 'users-permissions').findOne({ type: 'root' });
const roleID = ctx.params.role;
const rootID = root.id || root._id;
// Prevent from updating the root role.
if (roleID === rootID) {
2017-11-27 17:50:51 +01:00
return ctx.badRequest(null, [{ messages: [{ id: 'Unauthorized' }] }]);
}
if (_.isEmpty(ctx.request.body)) {
return ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
}
2017-11-23 17:13:46 +01:00
try {
2018-01-22 18:19:44 +01:00
await strapi.plugins['users-permissions'].services.userspermissions.updateRole(roleID, ctx.request.body);
2017-11-23 17:13:46 +01:00
ctx.send({ ok: true });
} catch(error) {
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
}
2018-01-15 11:59:10 +01:00
},
getEmailTemplate: async (ctx) => {
ctx.send(await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
key: 'email'
}).get());
2018-01-15 12:05:01 +01:00
},
updateEmailTemplate: async (ctx) => {
if (_.isEmpty(ctx.request.body)) {
return ctx.badRequest(null, [{ messages: [{ id: 'Cannot be empty' }] }]);
}
await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
key: 'email'
}).set({value: ctx.request.body});
2018-01-15 12:05:01 +01:00
2018-01-18 16:23:31 +01:00
ctx.send({ ok: true });
2018-01-15 16:23:54 +01:00
},
getAdvancedSettings: async (ctx) => {
2018-03-12 15:56:25 +01:00
ctx.send({
settings: await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
key: 'advanced'
}).get(),
roles: await strapi.plugins['users-permissions'].services.userspermissions.getRoles()
});
2018-01-15 16:23:54 +01:00
},
updateAdvancedSettings: async (ctx) => {
if (_.isEmpty(ctx.request.body)) {
return ctx.badRequest(null, [{ messages: [{ id: 'Cannot be empty' }] }]);
}
await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
key: 'advanced'
}).set({value: ctx.request.body});
2018-01-15 16:23:54 +01:00
2018-01-18 16:23:31 +01:00
ctx.send({ ok: true });
2018-01-19 15:14:51 +01:00
},
getProviders: async (ctx) => {
ctx.send(await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
key: 'grant'
}).get());
2018-01-22 10:23:04 +01:00
},
updateProviders: async (ctx) => {
if (_.isEmpty(ctx.request.body)) {
return ctx.badRequest(null, [{ messages: [{ id: 'Cannot be empty' }] }]);
}
await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
key: 'grant'
}).set({value: ctx.request.body});
2018-01-22 10:23:04 +01:00
ctx.send({ ok: true });
}
};