mirror of
https://github.com/strapi/strapi.git
synced 2025-07-24 09:25:25 +00:00
99 lines
2.0 KiB
JavaScript
99 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const { validateRoleUpdateInput } = require('../validation/role');
|
|
|
|
module.exports = {
|
|
/**
|
|
* Returns on role by id
|
|
* @param {KoaContext} ctx - koa context
|
|
*/
|
|
async findOne(ctx) {
|
|
const { id } = ctx.params;
|
|
const role = await strapi.admin.services.role.findOneWithUsersCount({ id });
|
|
|
|
if (!role) {
|
|
return ctx.notFound('role.notFound');
|
|
}
|
|
|
|
ctx.body = {
|
|
data: role,
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Returns every roles
|
|
* @param {KoaContext} ctx - koa context
|
|
*/
|
|
async findAll(ctx) {
|
|
const roles = await strapi.admin.services.role.findAllWithUsersCount();
|
|
|
|
ctx.body = {
|
|
data: roles,
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Updates a role by id
|
|
* @param {KoaContext} ctx - koa context
|
|
*/
|
|
async update(ctx) {
|
|
const { id } = ctx.params;
|
|
|
|
try {
|
|
await validateRoleUpdateInput(ctx.request.body);
|
|
} catch (err) {
|
|
return ctx.badRequest('ValidationError', err);
|
|
}
|
|
|
|
const role = await strapi.admin.services.role.update({ id }, ctx.request.body);
|
|
|
|
if (!role) {
|
|
return ctx.notFound('role.notFound');
|
|
}
|
|
|
|
const sanitizedRole = strapi.admin.services.role.sanitizeRole(role);
|
|
|
|
ctx.body = {
|
|
data: sanitizedRole,
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Returns the permissions assigned to a role
|
|
* @param {KoaContext} ctx - koa context
|
|
*/
|
|
async getPermissions(ctx) {
|
|
const { id } = ctx.params;
|
|
|
|
const role = await strapi.admin.services.role.findOne({ id });
|
|
|
|
if (!role) {
|
|
return ctx.notFound('role.notFound');
|
|
}
|
|
|
|
const permissions = await strapi.admin.services.permissions.find({ role: role.id });
|
|
|
|
ctx.body = {
|
|
data: permissions,
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Updates the permissions assigned to a role
|
|
* @param {KoaContext} ctx - koa context
|
|
*/
|
|
async updatePermissions(ctx) {
|
|
const { id } = ctx.params;
|
|
|
|
const role = await strapi.admin.services.role.findOne({ id });
|
|
|
|
if (!role) {
|
|
return ctx.notFound('role.notFound');
|
|
}
|
|
|
|
ctx.body = {
|
|
data: [],
|
|
};
|
|
},
|
|
};
|