S14 and the privacy items of P0: - signing up as "admin" no longer grants admin; `PrivaPub admin promote <root>` (and `demote`) does, run on the box against the configured database; - Swagger is served in Development only; - every service and controller answers "Something went wrong." where it used to send ex.Message, and the SMTP warnings no longer log the recipient's address; - sign-up and login no longer log the IP, User-Agent and root id together; - invitation sign-up takes the persona's own AvatarUserName (and optional AvatarName) instead of naming the avatar after the private login, and refuses a persona username equal to the login's. Invitation login uses the named persona, creating it if it is new; - recovery mail comes from "PrivaPub", not collAnon's support address name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
130 lines
3.8 KiB
C#
130 lines
3.8 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(Localizer["Something went wrong."]));
|
|
}
|
|
}
|
|
|
|
[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(Localizer["Something went wrong."]));
|
|
}
|
|
}
|
|
|
|
[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(Localizer["Something went wrong."]));
|
|
}
|
|
}
|
|
|
|
//[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(Localizer["Something went wrong."]));
|
|
// }
|
|
//}
|
|
}
|
|
}
|