using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using RSecurityBackend.Models.Auth.Db; using RSecurityBackend.Models.Auth.Memory; using RSecurityBackend.Models.Generic; using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using RSecurityBackend.Services; namespace RSecurityBackend.Controllers { /// /// User roles /// [Produces("application/json")] [Route("api/roles")] public abstract class RoleControllerBase : Controller { /// /// All Roles Information /// /// All Roles Information [HttpGet] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.ViewOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task Get() { RServiceResult rolesInfo = await _roleService.GetAllRoles(); if (rolesInfo.Result == null) { return BadRequest(rolesInfo.ExceptionString); } return Ok(rolesInfo.Result); } /// /// returns role information /// /// role name /// role information [HttpGet("{roleName}")] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.ViewOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RAppRole))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task Get(string roleName) { RServiceResult roleInfo = await _roleService.GetRoleInformation(roleName); if (roleInfo.Result == null) { if (string.IsNullOrEmpty(roleInfo.ExceptionString)) return NotFound(); return BadRequest(roleInfo.ExceptionString); } return Ok(roleInfo.Result); } /// /// add a new role /// /// /// id if required could be retrieved from return value [HttpPost] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.AddOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RAppRole))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] public async Task Post([FromBody]RAppRole newGroupInfo) { RServiceResult result = await _roleService.AddRole(newGroupInfo); if (result.Result == null) return BadRequest(result.ExceptionString); return Ok(result.Result); } /// /// update existing role /// /// role name /// existingGroupInfo.id could be passed empty and it is ignored completely /// true if succeeds [HttpPut("{roleName}")] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] public async Task Put(string roleName, [FromBody]RAppRole existingGroupInfo) { RServiceResult res = await _roleService.ModifyRole(roleName, existingGroupInfo); if (!res.Result) return BadRequest(res.ExceptionString); return Ok(true); } /// /// delete role /// /// role name /// true if succeeds [HttpDelete("{roleName}")] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.DeleteOperationShortName)] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Forbidden)] public async Task Delete(string roleName) { RServiceResult res = await _roleService.DeleteRole(roleName); if (!res.Result) { return BadRequest(res.ExceptionString); } return Ok(true); } /// /// lists role permissions /// /// [HttpGet] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.ViewOperationShortName)] [Route("permissions/{roleName}")] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetRoleSecurableItemsStatus(string roleName) { RServiceResult res = await _roleService.GetRoleSecurableItemsStatus(roleName); if (res.Result == null) { return BadRequest(res.ExceptionString); } return Ok(res.Result); } /// /// Saves role permissions /// /// role name /// /// [HttpPut] [Authorize(Policy = SecurableItem.RoleEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] [Route("permissions/{roleName}")] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task SetRoleSecurableItemsStatus(string roleName, [FromBody]SecurableItem[] securableItems) { RServiceResult res = await _roleService.SetRoleSecurableItemsStatus(roleName, securableItems); if (!res.Result) { return BadRequest(res.ExceptionString); } return Ok(res.Result); } /// /// Get All SecurableItems /// /// All All SecurableItems [HttpGet] [Route("securableitems")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public IActionResult GetSecurableItems() { return Ok(_roleService.GetSecurableItems()); } /// /// IUserRoleService instance /// protected IUserRoleService _roleService; /// /// constructor /// /// public RoleControllerBase(IUserRoleService roleService) { _roleService = roleService; } } }