2022-02-27 13:54:25 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using VoidCat.Model;
|
|
|
|
using VoidCat.Services.Abstractions;
|
|
|
|
|
|
|
|
namespace VoidCat.Controllers;
|
|
|
|
|
2022-03-02 11:37:15 +00:00
|
|
|
[Route("user/{id}")]
|
2022-02-27 13:54:25 +00:00
|
|
|
public class UserController : Controller
|
|
|
|
{
|
|
|
|
private readonly IUserStore _store;
|
2022-02-27 23:01:57 +00:00
|
|
|
private readonly IUserUploadsStore _userUploads;
|
2022-03-02 11:37:15 +00:00
|
|
|
private readonly IEmailVerification _emailVerification;
|
2022-02-27 13:54:25 +00:00
|
|
|
|
2022-03-02 11:37:15 +00:00
|
|
|
public UserController(IUserStore store, IUserUploadsStore userUploads, IEmailVerification emailVerification)
|
2022-02-27 13:54:25 +00:00
|
|
|
{
|
|
|
|
_store = store;
|
2022-02-27 23:01:57 +00:00
|
|
|
_userUploads = userUploads;
|
2022-03-02 11:37:15 +00:00
|
|
|
_emailVerification = emailVerification;
|
2022-02-27 13:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-03-02 11:37:15 +00:00
|
|
|
public async Task<IActionResult> GetUser([FromRoute] string id)
|
2022-02-27 13:54:25 +00:00
|
|
|
{
|
|
|
|
var loggedUser = HttpContext.GetUserId();
|
2022-03-04 21:12:01 +00:00
|
|
|
var isMe = id.Equals("me", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
if (isMe && !loggedUser.HasValue) return Unauthorized();
|
|
|
|
|
|
|
|
var requestedId = isMe ? loggedUser!.Value : id.FromBase58Guid();
|
2022-02-27 13:54:25 +00:00
|
|
|
if (loggedUser == requestedId)
|
|
|
|
{
|
2022-03-07 13:38:53 +00:00
|
|
|
var pUser = await _store.Get<PrivateVoidUser>(requestedId);
|
|
|
|
if (pUser == default) return NotFound();
|
|
|
|
return Json(pUser);
|
2022-02-27 13:54:25 +00:00
|
|
|
}
|
2022-02-27 18:15:37 +00:00
|
|
|
|
2022-03-04 21:12:01 +00:00
|
|
|
var user = await _store.Get<PublicVoidUser>(requestedId);
|
2022-03-02 11:37:15 +00:00
|
|
|
if (!(user?.Flags.HasFlag(VoidUserFlags.PublicProfile) ?? false)) return NotFound();
|
2022-02-27 23:01:57 +00:00
|
|
|
|
2022-03-02 11:37:15 +00:00
|
|
|
return Json(user);
|
2022-02-27 18:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
public async Task<IActionResult> UpdateUser([FromRoute] string id, [FromBody] PublicVoidUser user)
|
|
|
|
{
|
2022-03-02 11:37:15 +00:00
|
|
|
var loggedUser = await GetAuthorizedUser(id);
|
|
|
|
if (loggedUser == default) return Unauthorized();
|
2022-02-27 18:15:37 +00:00
|
|
|
|
2022-03-02 11:37:15 +00:00
|
|
|
if (!loggedUser.Flags.HasFlag(VoidUserFlags.EmailVerified)) return Forbid();
|
2022-03-04 21:12:01 +00:00
|
|
|
|
2022-03-04 20:05:01 +00:00
|
|
|
await _store.UpdateProfile(user);
|
2022-02-27 18:15:37 +00:00
|
|
|
return Ok();
|
2022-02-27 13:54:25 +00:00
|
|
|
}
|
2022-02-27 23:01:57 +00:00
|
|
|
|
|
|
|
[HttpPost]
|
2022-03-02 11:37:15 +00:00
|
|
|
[Route("files")]
|
|
|
|
public async Task<IActionResult> ListUserFiles([FromRoute] string id,
|
|
|
|
[FromBody] PagedRequest request)
|
2022-02-27 23:01:57 +00:00
|
|
|
{
|
|
|
|
var loggedUser = HttpContext.GetUserId();
|
2022-03-02 11:37:15 +00:00
|
|
|
var isAdmin = HttpContext.IsRole(Roles.Admin);
|
|
|
|
|
|
|
|
var user = await GetRequestedUser(id);
|
|
|
|
if (user == default) return NotFound();
|
|
|
|
|
|
|
|
// not logged in user files, check public flag
|
|
|
|
var canViewUploads = loggedUser == user.Id || isAdmin;
|
|
|
|
if (!canViewUploads &&
|
|
|
|
!user.Flags.HasFlag(VoidUserFlags.PublicUploads)) return Forbid();
|
|
|
|
|
2022-02-27 23:01:57 +00:00
|
|
|
var results = await _userUploads.ListFiles(id.FromBase58Guid(), request);
|
2022-03-02 11:37:15 +00:00
|
|
|
return Json(await results.GetResults());
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
[Route("verify")]
|
|
|
|
public async Task<IActionResult> SendVerificationCode([FromRoute] string id)
|
|
|
|
{
|
|
|
|
var user = await GetAuthorizedUser(id);
|
|
|
|
if (user == default) return Unauthorized();
|
|
|
|
|
|
|
|
var isEmailVerified = (user?.Flags.HasFlag(VoidUserFlags.EmailVerified) ?? false);
|
|
|
|
if (isEmailVerified) return UnprocessableEntity();
|
|
|
|
|
|
|
|
await _emailVerification.SendNewCode(user!);
|
|
|
|
return Accepted();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
[Route("verify")]
|
|
|
|
public async Task<IActionResult> VerifyCode([FromRoute] string id, [FromBody] string code)
|
|
|
|
{
|
|
|
|
var user = await GetAuthorizedUser(id);
|
|
|
|
if (user == default) return Unauthorized();
|
|
|
|
|
|
|
|
var token = code.FromBase58Guid();
|
|
|
|
if (!await _emailVerification.VerifyCode(user, token)) return BadRequest();
|
|
|
|
|
|
|
|
user.Flags |= VoidUserFlags.EmailVerified;
|
2022-03-07 13:38:53 +00:00
|
|
|
await _store.Set(user.Id, user);
|
2022-03-02 11:37:15 +00:00
|
|
|
return Accepted();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<InternalVoidUser?> GetAuthorizedUser(string id)
|
|
|
|
{
|
|
|
|
var loggedUser = HttpContext.GetUserId();
|
|
|
|
var gid = id.FromBase58Guid();
|
|
|
|
var user = await _store.Get<InternalVoidUser>(gid);
|
|
|
|
return user?.Id != loggedUser ? default : user;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<InternalVoidUser?> GetRequestedUser(string id)
|
|
|
|
{
|
|
|
|
var gid = id.FromBase58Guid();
|
|
|
|
return await _store.Get<InternalVoidUser>(gid);
|
2022-02-27 23:01:57 +00:00
|
|
|
}
|
2022-03-02 11:37:15 +00:00
|
|
|
}
|