Implemented user logout and deletion service. Added Auth, Deletion and Logout hook. Restructed controllers
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Services;
|
||||
using Moonlight.Shared;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/users/{id:int}")]
|
||||
public class UserActionsController : Controller
|
||||
{
|
||||
// Consider building a service for deletion and logout or actions in general
|
||||
|
||||
private readonly DatabaseRepository<User> UsersRepository;
|
||||
private readonly IMemoryCache Cache;
|
||||
|
||||
public UserActionsController(DatabaseRepository<User> usersRepository, IMemoryCache cache)
|
||||
{
|
||||
UsersRepository = usersRepository;
|
||||
Cache = cache;
|
||||
}
|
||||
|
||||
[HttpPost("logout")]
|
||||
[Authorize(Policy = Permissions.Users.Logout)]
|
||||
public async Task<ActionResult> LogoutAsync([FromRoute] int id)
|
||||
{
|
||||
var user = await UsersRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(u => u.Id == id);
|
||||
|
||||
if(user == null)
|
||||
return Problem("User not found", statusCode: 404);
|
||||
|
||||
user.InvalidateTimestamp = DateTimeOffset.UtcNow;
|
||||
await UsersRepository.UpdateAsync(user);
|
||||
|
||||
Cache.Remove(string.Format(UserAuthService.CacheKeyPattern, id));
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Frozen;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Services;
|
||||
using Moonlight.Shared;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin.Users;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/users")]
|
||||
[Authorize(Policy = Permissions.Users.Delete)]
|
||||
public class UserDeletionController : Controller
|
||||
{
|
||||
private readonly UserDeletionService UserDeletionService;
|
||||
private readonly DatabaseRepository<User> Repository;
|
||||
|
||||
public UserDeletionController(UserDeletionService userDeletionService, DatabaseRepository<User> repository)
|
||||
{
|
||||
UserDeletionService = userDeletionService;
|
||||
Repository = repository;
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<ActionResult> DeleteAsync([FromRoute] int id)
|
||||
{
|
||||
var userExists = await Repository
|
||||
.Query()
|
||||
.AnyAsync(user => user.Id == id);
|
||||
|
||||
if (!userExists)
|
||||
return Problem("No user with this id found", statusCode: 404);
|
||||
|
||||
var validationResult = await UserDeletionService.ValidateAsync(id);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return ValidationProblem(
|
||||
new ValidationProblemDetails(
|
||||
new Dictionary<string, string[]>()
|
||||
{
|
||||
{
|
||||
string.Empty,
|
||||
validationResult.ErrorMessages.ToArray()
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
await UserDeletionService.DeleteAsync(id);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Services;
|
||||
using Moonlight.Shared;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin.Users;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/users/{id:int}/logout")]
|
||||
[Authorize(Policy = Permissions.Users.Logout)]
|
||||
public class UserLogoutController : Controller
|
||||
{
|
||||
private readonly UserLogoutService LogoutService;
|
||||
private readonly DatabaseRepository<User> Repository;
|
||||
|
||||
public UserLogoutController(
|
||||
UserLogoutService logoutService,
|
||||
DatabaseRepository<User> repository
|
||||
)
|
||||
{
|
||||
LogoutService = logoutService;
|
||||
Repository = repository;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> LogoutAsync([FromRoute] int id)
|
||||
{
|
||||
var userExists = await Repository
|
||||
.Query()
|
||||
.AnyAsync(user => user.Id == id);
|
||||
|
||||
if (!userExists)
|
||||
return Problem("No user with this id found", statusCode: 404);
|
||||
|
||||
await LogoutService.LogoutAsync(id);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ using Moonlight.Shared.Http.Requests.Users;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
using Moonlight.Shared.Http.Responses.Users;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
namespace Moonlight.Api.Http.Controllers.Admin.Users;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
@@ -117,19 +117,4 @@ public class UsersController : Controller
|
||||
|
||||
return UserMapper.ToDto(user);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Users.Delete)]
|
||||
public async Task<ActionResult> DeleteAsync([FromRoute] int id)
|
||||
{
|
||||
var user = await UserRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(user => user.Id == id);
|
||||
|
||||
if (user == null)
|
||||
return Problem("No user with this id found", statusCode: 404);
|
||||
|
||||
await UserRepository.RemoveAsync(user);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user