Audit Service

This commit is contained in:
Andre Beging
2022-05-23 10:29:10 +02:00
parent 5961c06004
commit 5d713db83f
14 changed files with 552 additions and 49 deletions

View File

@@ -13,8 +13,7 @@ namespace FoodsharingSiegen.Server.Auth
/// <summary>
/// The auth service class (a. beging, 04.04.2022)
/// </summary>
/// <seealso cref="ServiceBase"/>
public class AuthService : ServiceBase
public class AuthService
{
#region Public Properties
@@ -25,6 +24,15 @@ namespace FoodsharingSiegen.Server.Auth
#endregion
#region Private Properties
/// <summary>
/// Gets the value of the context (ab)
/// </summary>
private FsContext Context { get; }
#endregion
#region Private Fields
/// <summary>
@@ -55,8 +63,9 @@ namespace FoodsharingSiegen.Server.Auth
public AuthService(
FsContext context,
LocalStorageService localStorageService,
AuthenticationStateProvider authenticationStateProvider) : base(context)
AuthenticationStateProvider authenticationStateProvider)
{
Context = context;
_localStorageService = localStorageService;
_authenticationStateProvider = authenticationStateProvider;
}

View File

@@ -1,9 +1,11 @@
using System.Security.Claims;
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Server.Auth;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Data;
using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Shared.Helper;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Service
{
@@ -13,13 +15,13 @@ namespace FoodsharingSiegen.Server.Service
/// <seealso cref="AuthenticationStateProvider"/>
public class TokenAuthStateProvider : AuthenticationStateProvider
{
private FsContext Context { get; }
#region Private Fields
/// <summary> LocalStorageService </summary>
private readonly LocalStorageService _localStorageService;
private readonly UserService _userService;
#endregion
#region Setup/Teardown
@@ -28,11 +30,11 @@ namespace FoodsharingSiegen.Server.Service
/// Constructor
/// </summary>
/// <param name="localStorageService"></param>
/// <param name="userService"></param>
public TokenAuthStateProvider(LocalStorageService localStorageService, UserService userService)
/// <param name="context"></param>
public TokenAuthStateProvider(LocalStorageService localStorageService, FsContext context)
{
Context = context;
_localStorageService = localStorageService;
_userService = userService;
}
#endregion
@@ -48,7 +50,7 @@ namespace FoodsharingSiegen.Server.Service
var token = await _localStorageService.GetItem<string>(StorageKeys.TokenKey);
var tokenValid = AuthHelper.ValidateToken(token, out var user);
var checkR = await _userService.CheckForceLogout(user);
var checkR = await CheckForceLogout(user);
if (checkR.Success && checkR.Data)
tokenValid = false;
@@ -75,6 +77,28 @@ namespace FoodsharingSiegen.Server.Service
#endregion
#region Public Method CheckForceLogout
/// <summary>
/// Checks the force logout using the specified user (a. beging, 11.04.2022)
/// </summary>
/// <param name="user">The user</param>
/// <returns>A task containing an operation result of bool</returns>
public async Task<OperationResult<bool>> CheckForceLogout(User user)
{
try
{
var anyR = await Context.Users.AnyAsync(x => x.Id == user.Id && x.ForceLogout);
return new OperationResult<bool>(anyR);
}
catch (Exception e)
{
return new OperationResult<bool>(e);
}
}
#endregion
#region Public Method MarkUserAsLoggedOut
/// <summary>