SocialPub/PrivaPub/Controllers/ClientToServer/AdminController.cs

130 lines
3.7 KiB
C#
Raw Permalink Normal View History

2023-02-18 08:52:17 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
2023-02-19 00:43:43 +01:00
using PrivaPub.ClientModels;
using PrivaPub.ClientModels.User;
using PrivaPub.Extensions;
using PrivaPub.Resources;
using PrivaPub.Services;
2023-02-18 08:52:17 +01:00
using System.ComponentModel.DataAnnotations;
2023-02-19 00:43:43 +01:00
namespace PrivaPub.Controllers.ClientToServer
2023-02-18 08:52:17 +01:00
{
[ApiController,
Route("clientapi/admin")]
public class AdminController : ControllerBase
{
readonly IRootUsersService RootUsersService;
2023-02-19 00:43:43 +01:00
readonly ILogger<RootUserController> Logger;
2023-02-18 08:52:17 +01:00
readonly IStringLocalizer Localizer;
2023-02-19 00:43:43 +01:00
public AdminController(ILogger<RootUserController> logger,
2023-02-18 08:52:17 +01:00
IStringLocalizer<GenericRes> localizer,
IRootUsersService rootUsersService)
{
Logger = logger;
Localizer = localizer;
RootUsersService = rootUsersService;
}
[HttpDelete, Route("/clientapi/admin/remove/users"), Authorize(Policy = Policies.IsAdmin)]
public async Task<IActionResult> RemoveUsers([Required] UsersIds usersIds)
{
var result = new WebResult();
try
{
usersIds.UserIdList.Remove(User.GetUserId());
result = await RootUsersService.RemoveUserAsync(usersIds);
if (!result.IsValid)
return StatusCode(result.StatusCode, result);
return Ok();
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(User)}.{nameof(RemoveUsers)}()");
return BadRequest(result.Invalidate(ex.Message));
}
}
[HttpPost, Authorize(Policy = Policies.IsAdmin), Route("/clientapi/admin/ban/users")]
public async Task<IActionResult> BanUsers([Required] UsersIds usersIds)
{
if (!ModelState.IsValid)
return BadRequest(usersIds);
var result = new WebResult();
try
{
//var isUserResult = await UsersService.UserIsAdminAsync(User.GetUserId());
//if (isUserResult.IsValid && !(bool)isUserResult.Data)
//if (isUserResult is { IsValid: true } and not { Data: bool })
// return Unauthorized();
usersIds.UserIdList.Remove(User.GetUserId());
result = await RootUsersService.BanUserAsync(usersIds);
if (!result.IsValid)
return StatusCode(result.StatusCode, result);
return Ok();
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(User)}.{nameof(BanUsers)}()");
return BadRequest(result.Invalidate(ex.Message));
}
}
[HttpPost, Authorize(Policy = Policies.IsAdmin), Route("/clientapi/admin/unban/users")]
public async Task<IActionResult> UnbanUsers([Required] UsersIds usersIds)
{
if (!ModelState.IsValid)
return BadRequest(usersIds);
var result = new WebResult();
try
{
//var isUserResult = await UsersService.UserIsAdminAsync(User.GetUserId());
//if (isUserResult.IsValid && !(bool)isUserResult.Data)
// return Unauthorized();
usersIds.UserIdList.Remove(User.GetUserId());
result = await RootUsersService.UnbanUserAsync(usersIds);
if (!result.IsValid)
return StatusCode(result.StatusCode, result);
return Ok();
}
catch (Exception ex)
{
Logger.LogError(ex, $"{nameof(User)}.{nameof(UnbanUsers)}()");
return BadRequest(result.Invalidate(ex.Message));
}
}
//[HttpGet, Authorize(Policy = Policies.IsAdmin)]
//public async Task<IActionResult> GetUsers()
//{
// var result = new WebResult();
// try
// {
// var isUserResult = await UsersService.UserIsAdminAsync(User.GetUserId());
// if (isUserResult.IsValid && !(bool)isUserResult.Data)
// return Unauthorized();
// result = await UsersService.GetUsersAsync();
// if (!result.IsValid)
// return StatusCode(result.StatusCode, result);
// return Ok(result.Data);
// }
// catch (Exception ex)
// {
// Logger.LogError(ex, $"{nameof(User)}.{nameof(GetUsers)}()");
// return BadRequest(result.Invalidate(ex.Message));
// }
//}
}
}