Spaces:
Runtime error
Runtime error
File size: 5,084 Bytes
3dd5c9a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using TaskTrackingSystem.Shared;
using TaskTrackingSystem.Shared.Models.Role;
using TaskTrackingSystem.WebApi.Infrastructure;
namespace TaskTrackingSystem.WebApi.Features.Role
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class RoleController : ControllerBase
{
private readonly RoleService _roleService;
private readonly PermissionAuthorizationService _permissionAuthorizationService;
public RoleController(RoleService roleService, PermissionAuthorizationService permissionAuthorizationService)
{
_roleService = roleService;
_permissionAuthorizationService = permissionAuthorizationService;
}
[HttpGet]
[ProducesResponseType(typeof(PagedResult<RoleDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> GetRoles(
[FromQuery] string? search,
[FromQuery] PaginationQuery? paging = null)
{
if (paging == null || (!paging.Page.HasValue && !paging.Limit.HasValue))
{
var roles = await _roleService.GetAllRolesAsync();
return Ok(roles);
}
var page = PaginationExtensions.NormalizePage(paging.Page);
var limit = PaginationExtensions.NormalizePageSize(paging.Limit ?? 0);
var paged = await _roleService.GetPagedRolesAsync(search, page, limit);
return Ok(paged);
}
[HttpGet("{id}")]
public async Task<ActionResult<RoleDto>> GetRole(long id)
{
var role = await _roleService.GetRoleByIdAsync(id);
if (role == null)
{
return NotFound(new { message = $"Role with ID {id} not found." });
}
return Ok(role);
}
[HttpPost]
public async Task<ActionResult<Result<RoleDto>>> CreateRole([FromBody] CreateRoleDto createRoleDto)
{
if (!await _permissionAuthorizationService.CanAccessAsync(User, "api/Role", "Create"))
{
return StatusCode(403, Result<RoleDto>.Failure("You do not have permission to create roles.", 403));
}
long? currentUserId = User.GetUserId();
var result = await _roleService.CreateRoleAsync(createRoleDto, currentUserId);
return StatusCode(result.StatusCode, result);
}
[HttpPut("{id}")]
public async Task<ActionResult<Result>> UpdateRole(long id, [FromBody] UpdateRoleDto updateRoleDto)
{
if (!await _permissionAuthorizationService.CanAccessAsync(User, "api/Role", "Update"))
{
return StatusCode(403, Result.Failure("You do not have permission to update roles.", 403));
}
long? currentUserId = User.GetUserId();
var result = await _roleService.UpdateRoleAsync(id, updateRoleDto, currentUserId);
return StatusCode(result.StatusCode, result);
}
[HttpDelete("{id}")]
public async Task<ActionResult<Result>> DeleteRole(long id)
{
if (!await _permissionAuthorizationService.CanAccessAsync(User, "api/Role", "Delete"))
{
return StatusCode(403, Result.Failure("You do not have permission to delete roles.", 403));
}
var result = await _roleService.SoftDeleteRoleAsync(id, User.GetUserId());
return StatusCode(result.StatusCode, result);
}
[HttpGet("{id}/menus")]
[HttpGet("{id}/access")]
public async Task<ActionResult<Result<List<string>>>> GetAssignedAccessCodes(long id)
{
if (!await _permissionAuthorizationService.CanAccessAsync(User, "api/Role", "Update"))
{
return StatusCode(403, Result<List<string>>.Failure("You do not have permission to view role access.", 403));
}
var result = await _roleService.GetAssignedAccessCodesByRoleIdAsync(id);
if (!result.IsSuccess)
{
return StatusCode(result.StatusCode, new { message = result.ErrorMessage });
}
return Ok(result);
}
[HttpPost("{id}/menus")]
[HttpPost("{id}/access")]
public async Task<IActionResult> AssignAccess(long id, [FromBody] AssignAccessDto dto)
{
if (!await _permissionAuthorizationService.CanAccessAsync(User, "api/Role", "Update"))
{
return StatusCode(403, Result.Failure("You do not have permission to assign role access.", 403));
}
var result = await _roleService.AssignAccessToRoleAsync(id, dto, User.GetUserId());
if (!result.IsSuccess)
{
return StatusCode(result.StatusCode, new { message = result.ErrorMessage });
}
return Ok();
}
}
}
|