130 lines
3.7 KiB
C#
130 lines
3.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
using PrivaPub.ClientModels;
|
|
using PrivaPub.ClientModels.User;
|
|
using PrivaPub.Extensions;
|
|
using PrivaPub.Resources;
|
|
using PrivaPub.Services;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
namespace PrivaPub.Controllers.ClientToServer
|
|
{
|
|
[ApiController,
|
|
Route("clientapi/admin")]
|
|
public class AdminController : ControllerBase
|
|
{
|
|
readonly IRootUsersService RootUsersService;
|
|
readonly ILogger<RootUserController> Logger;
|
|
readonly IStringLocalizer Localizer;
|
|
|
|
public AdminController(ILogger<RootUserController> logger,
|
|
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));
|
|
// }
|
|
//}
|
|
}
|
|
}
|