2024-11-13 23:16:49 +00:00
|
|
|
using BotSharp.Abstraction.Roles;
|
|
|
|
|
using BotSharp.Abstraction.Users.Enums;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.OpenAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class RoleController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IRoleService _roleService;
|
|
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
|
|
|
|
|
public RoleController(
|
|
|
|
|
IServiceProvider services,
|
|
|
|
|
IRoleService roleService,
|
|
|
|
|
IUserIdentity user)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_roleService = roleService;
|
|
|
|
|
_user = user;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 23:33:26 +00:00
|
|
|
[HttpPost("/role/refresh")]
|
|
|
|
|
public async Task<bool> RefreshRoles()
|
|
|
|
|
{
|
|
|
|
|
var isValid = await IsValidUser();
|
|
|
|
|
if (!isValid)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await _roleService.RefreshRoles();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-11-13 23:16:49 +00:00
|
|
|
[HttpGet("/role/options")]
|
|
|
|
|
public async Task<IEnumerable<string>> GetRoleOptions()
|
|
|
|
|
{
|
|
|
|
|
return await _roleService.GetRoleOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/roles")]
|
|
|
|
|
public async Task<IEnumerable<RoleViewModel>> GetRoles([FromBody] RoleFilter? filter = null)
|
|
|
|
|
{
|
|
|
|
|
if (filter == null)
|
|
|
|
|
{
|
|
|
|
|
filter = RoleFilter.Empty();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 23:33:26 +00:00
|
|
|
var isValid = await IsValidUser();
|
|
|
|
|
if (!isValid)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<RoleViewModel>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 23:16:49 +00:00
|
|
|
var roles = await _roleService.GetRoles(filter);
|
|
|
|
|
return roles.Select(x => RoleViewModel.FromRole(x)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("/role/{id}/details")]
|
|
|
|
|
public async Task<RoleViewModel> GetRoleDetails([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
var role = await _roleService.GetRoleDetails(id);
|
|
|
|
|
return RoleViewModel.FromRole(role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("/role")]
|
|
|
|
|
public async Task<bool> UpdateRole([FromBody] RoleUpdateModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null) return false;
|
|
|
|
|
|
2024-11-14 23:33:26 +00:00
|
|
|
var isValid = await IsValidUser();
|
|
|
|
|
if (!isValid)
|
2024-11-13 23:16:49 +00:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var role = RoleUpdateModel.ToRole(model);
|
|
|
|
|
return await _roleService.UpdateRole(role, isUpdateRoleAgents: true);
|
|
|
|
|
}
|
2024-11-14 23:33:26 +00:00
|
|
|
|
|
|
|
|
private async Task<bool> IsValidUser()
|
|
|
|
|
{
|
|
|
|
|
var userService = _services.GetRequiredService<IUserService>();
|
2024-11-15 02:23:45 +00:00
|
|
|
return await userService.IsAdminUser(_user.Id);
|
2024-11-14 23:33:26 +00:00
|
|
|
}
|
2024-11-13 23:16:49 +00:00
|
|
|
}
|