125 lines
2.9 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* UsersPermissions.js controller
*
* @description: A set of functions called "actions" of the `users-permissions` plugin.
*/
2017-11-16 14:12:03 +01:00
const fakeData = require('../config/fakeData.json');
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
return ctx.send({ ok: true });
},
deleteRole: async ctx => {
const { role } = ctx.params;
if (!role) {
return ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
}
2017-11-27 17:02:45 +01:00
if (role === '0' || role === '1') {
return ctx.badRequest(null, [{ messages: [{ id: 'Unauthorized' }] }]);
}
try {
await strapi.plugins['users-permissions'].services.userspermissions.deleteRole(role);
return ctx.send({ ok: true });
} catch(err) {
return ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
}
2017-11-23 18:08:14 +01:00
},
2017-11-23 17:13:46 +01:00
getPermissions: async (ctx) => {
try {
2017-11-16 17:59:41 +01:00
const permissions = await strapi.plugins['users-permissions'].services.userspermissions.getActions();
ctx.send({ permissions });
} catch(err) {
ctx.badRequest(null, [{ message: [{ id: 'Not Found' }] }]);
}
},
2017-11-23 17:13:46 +01:00
getRole: async (ctx) => {
const { id } = ctx.params;
const role = fakeData[id];
if (_.isEmpty(role)) {
2017-11-20 14:35:24 +01:00
return ctx.badRequest(null, [{ messages: [{ id: `Role don't exist` }] }]);
}
return 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' }] }]);
}
},
index: async (ctx) => {
// Add your own logic here.
// Send 200 `ok`
ctx.send({
message: 'ok'
});
},
init: async (ctx) => {
2017-11-15 17:27:07 +01:00
const hasAdmin = await strapi.query('user', 'users-permissions').find({
where: {
admin: true
}
});
ctx.send({ hasAdmin: !_.isEmpty(hasAdmin) });
2017-11-23 17:13:46 +01:00
},
searchUsers: async (ctx) => {
const data = await strapi.query('user', 'users-permissions').search(ctx.params);
return ctx.send(data);
},
2017-11-23 17:13:46 +01:00
updateRole: async (ctx) => {
try {
ctx.send({ ok: true });
} catch(error) {
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
}
}
};