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

@@ -0,0 +1,49 @@
using System.ComponentModel.DataAnnotations;
namespace FoodsharingSiegen.Contracts.Entity
{
/// <summary>
/// The audit class (a. beging, 23.05.2022)
/// </summary>
public class Audit
{
#region Public Properties
/// <summary>
/// Gets or sets the value of the created (ab)
/// </summary>
public DateTime? Created { get; set; }
/// <summary>
/// Gets or sets the value of the data 1 (ab)
/// </summary>
public string? Data1 { get; set; }
/// <summary>
/// Gets or sets the value of the data 2 (ab)
/// </summary>
public string? Data2 { get; set; }
/// <summary>
/// Gets or sets the value of the id (ab)
/// </summary>
[Key] public Guid Id { get; set; }
/// <summary>
/// Gets or sets the value of the type (ab)
/// </summary>
public AuditType Type { get; set; }
/// <summary>
/// Gets or sets the value of the user (ab)
/// </summary>
public User? User { get; set; }
/// <summary>
/// Gets or sets the value of the user id (ab)
/// </summary>
public Guid? UserID { get; set; }
#endregion
}
}

View File

@@ -0,0 +1,23 @@
namespace FoodsharingSiegen.Contracts.Entity
{
public enum AuditType : int
{
None = 0,
// Profile
SaveProfile = 10,
SetOwnPassword = 20,
// Usermanagement
CreateUser = 30,
UpdateUser = 40,
RemoveUser = 50,
SetUserPassword = 60,
// Prospect
CreateProspect = 70,
EditProspect = 80,
AddInteraction = 90,
RemoveInteraction = 100
}
}

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>

View File

@@ -1,5 +1,6 @@
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.BaseClasses
@@ -17,6 +18,11 @@ namespace FoodsharingSiegen.Server.BaseClasses
/// </summary>
[Inject] private AuthService? AuthService { get; set; }
/// <summary>
/// Gets or sets the value of the audit service (ab)
/// </summary>
[Inject] private AuditService? AuditService { get; set; }
#endregion
#region Override OnInitializedAsync

View File

@@ -1,6 +1,5 @@
using FoodsharingSiegen.Contracts.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace FoodsharingSiegen.Server.Data
{
@@ -27,6 +26,11 @@ namespace FoodsharingSiegen.Server.Data
/// </summary>
public DbSet<User>? Users { get; set; }
/// <summary>
/// Gets or sets the value of the audits (ab)
/// </summary>
public DbSet<Audit>? Audits { get; set; }
#endregion
#region Setup/Teardown

View File

@@ -0,0 +1,95 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
namespace FoodsharingSiegen.Server.Data.Service
{
/// <summary>
/// The audit service class (a. beging, 23.05.2022)
/// </summary>
/// <seealso cref="ServiceBase"/>
public class AuditService : ServiceBase
{
#region Setup/Teardown
/// <summary>
/// Initializes a new instance of the <see cref="AuditService"/> class
/// </summary>
/// <param name="context">The context (ab)</param>
/// <param name="authService"></param>
public AuditService(FsContext context, AuthService authService) : base(context, authService) { }
#endregion
#region Public Method Insert
/// <summary>
/// Inserts the type (a. beging, 23.05.2022)
/// </summary>
/// <param name="type">The type</param>
/// <param name="data1">The data</param>
/// <param name="data2">The data</param>
/// <returns>A task containing an operation result of audit</returns>
public async Task<OperationResult<Audit>> Insert(AuditType type, string? data1 = null, string? data2 = null)
{
try
{
var audit = new Audit
{
Type = type,
UserID = CurrentUser?.Id,
Data1 = data1,
Data2 = data2
};
Context.Audits?.Add(audit);
var saveR = await Context.SaveChangesAsync();
if (saveR > 0)
return new OperationResult<Audit>(audit);
return new OperationResult<Audit>(new Exception("Couldn't add audit"));
}
catch (Exception e)
{
return new OperationResult<Audit>(e);
}
}
#endregion
#region Public Method Load
/// <summary>
/// Loads the count (a. beging, 23.05.2022)
/// </summary>
/// <param name="count">The count</param>
/// <param name="type">The type</param>
/// <returns>A task containing an operation result of list audit</returns>
public async Task<OperationResult<List<Audit>>> Load(int count, AuditType? type)
{
try
{
var query = Context.Audits?.OrderBy(x => x.Created).AsQueryable();
if (count > 0)
query = query?.Take(count);
if (type != null)
query = query?.Where(x => x.Type == type);
var mat = query?.ToList();
if (mat != null) return new OperationResult<List<Audit>>(mat);
return new OperationResult<List<Audit>>(new Exception("Couldn't load audits"));
}
catch (Exception e)
{
return new OperationResult<List<Audit>>(e);
}
}
#endregion
}
}

View File

@@ -1,5 +1,6 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
@@ -10,13 +11,17 @@ namespace FoodsharingSiegen.Server.Data.Service
/// <seealso cref="ServiceBase"/>
public class ProspectService : ServiceBase
{
public AuditService AuditService { get; }
#region Setup/Teardown
/// <summary>
/// Initializes a new instance of the <see cref="ProspectService"/> class
/// </summary>
/// <param name="context">The context</param>
public ProspectService(FsContext context) : base(context) { }
/// <param name="authService"></param>
/// <param name="auditService"></param>
public ProspectService(FsContext context, AuthService authService, AuditService auditService) : base(context, authService) => AuditService = auditService;
#endregion
@@ -75,7 +80,10 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync();
if (saveR > 0)
{
await AuditService.Insert(AuditType.CreateProspect, prospect.Name, prospect.FsId.ToString());
return new OperationResult<Prospect>(prospect);
}
return new OperationResult<Prospect>(new Exception("Couldn't add prospect"));
}

View File

@@ -1,13 +1,23 @@
namespace FoodsharingSiegen.Server.Data.Service
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
namespace FoodsharingSiegen.Server.Data.Service
{
/// <summary>
/// The service base class (a. beging, 23.05.2022)
/// </summary>
public class ServiceBase
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Database Context </summary>
///
/// <value> The context. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected FsContext Context { get; }
#region Public Properties
/// <summary>
/// Gets the value of the auth service (ab)
/// </summary>
public AuthService AuthService { get; }
#endregion
#region Setup/Teardown
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Specialised constructor for use only by derived class. </summary>
@@ -15,7 +25,26 @@
/// <remarks> A Beging, 20.10.2021. </remarks>
///
/// <param name="context"> The context. </param>
/// <param name="authService"></param>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected ServiceBase(FsContext context) => Context = context;
protected ServiceBase(FsContext context, AuthService authService)
{
Context = context;
AuthService = authService;
}
#endregion
/// <summary>
/// Gets the value of the current user (ab)
/// </summary>
protected User? CurrentUser => AuthService.User;
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Database Context </summary>
///
/// <value> The context. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected FsContext Context { get; }
}
}

View File

@@ -1,5 +1,6 @@
using FoodsharingSiegen.Contracts;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.Auth;
using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service
@@ -16,10 +17,8 @@ namespace FoodsharingSiegen.Server.Data.Service
/// Initializes a new instance of the <see cref="UserService"/> class
/// </summary>
/// <param name="context">The context</param>
public UserService(FsContext context) : base(context)
{
}
/// <param name="authService"></param>
public UserService(FsContext context, AuthService authService) : base(context, authService) { }
#endregion
@@ -58,28 +57,6 @@ namespace FoodsharingSiegen.Server.Data.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 GetUsersAsync
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -0,0 +1,195 @@
// <auto-generated />
using System;
using FoodsharingSiegen.Server.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FoodsharingSiegen.Server.Migrations
{
[DbContext(typeof(FsContext))]
[Migration("20220523082612_Audit")]
partial class Audit
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.1");
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Audit", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime?>("Created")
.HasColumnType("TEXT");
b.Property<string>("Data1")
.HasColumnType("TEXT");
b.Property<string>("Data2")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.Property<Guid?>("UserID")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("UserID");
b.ToTable("Audits");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Interaction", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<bool>("Alert")
.HasColumnType("INTEGER");
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<DateTime>("Date")
.HasColumnType("TEXT");
b.Property<string>("Info")
.HasColumnType("TEXT");
b.Property<bool>("NotNeeded")
.HasColumnType("INTEGER");
b.Property<Guid>("ProspectID")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.Property<Guid>("UserID")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("ProspectID");
b.HasIndex("UserID");
b.ToTable("Interactions");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Prospect", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<int>("FsId")
.HasColumnType("INTEGER");
b.Property<string>("Memo")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Prospects");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<string>("EncryptedPassword")
.IsRequired()
.HasColumnType("TEXT");
b.Property<bool>("ForceLogout")
.HasColumnType("INTEGER");
b.Property<string>("Groups")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Mail")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Memo")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.Property<bool>("Verified")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Audit", b =>
{
b.HasOne("FoodsharingSiegen.Contracts.Entity.User", "User")
.WithMany()
.HasForeignKey("UserID");
b.Navigation("User");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Interaction", b =>
{
b.HasOne("FoodsharingSiegen.Contracts.Entity.Prospect", "Prospect")
.WithMany("Interactions")
.HasForeignKey("ProspectID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FoodsharingSiegen.Contracts.Entity.User", "User")
.WithMany("Interactions")
.HasForeignKey("UserID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Prospect");
b.Navigation("User");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Prospect", b =>
{
b.Navigation("Interactions");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.User", b =>
{
b.Navigation("Interactions");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FoodsharingSiegen.Server.Migrations
{
public partial class Audit : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Audits",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Created = table.Column<DateTime>(type: "TEXT", nullable: true),
Data1 = table.Column<string>(type: "TEXT", nullable: true),
Data2 = table.Column<string>(type: "TEXT", nullable: true),
Type = table.Column<int>(type: "INTEGER", nullable: false),
UserID = table.Column<Guid>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Audits", x => x.Id);
table.ForeignKey(
name: "FK_Audits_Users_UserID",
column: x => x.UserID,
principalTable: "Users",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_Audits_UserID",
table: "Audits",
column: "UserID");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Audits");
}
}
}

View File

@@ -17,6 +17,34 @@ namespace FoodsharingSiegen.Server.Migrations
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.1");
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Audit", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime?>("Created")
.HasColumnType("TEXT");
b.Property<string>("Data1")
.HasColumnType("TEXT");
b.Property<string>("Data2")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.Property<Guid?>("UserID")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("UserID");
b.ToTable("Audits");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Interaction", b =>
{
b.Property<Guid>("Id")
@@ -122,6 +150,15 @@ namespace FoodsharingSiegen.Server.Migrations
b.ToTable("Users");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Audit", b =>
{
b.HasOne("FoodsharingSiegen.Contracts.Entity.User", "User")
.WithMany()
.HasForeignKey("UserID");
b.Navigation("User");
});
modelBuilder.Entity("FoodsharingSiegen.Contracts.Entity.Interaction", b =>
{
b.HasOne("FoodsharingSiegen.Contracts.Entity.Prospect", "Prospect")

View File

@@ -23,10 +23,12 @@ builder.Services.AddScoped<LocalStorageService>();
builder.Services.AddScoped<AuthenticationStateProvider, TokenAuthStateProvider>();
builder.Services.AddScoped<FsContext>();
builder.Services.AddScoped<AuditService>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<ProspectService>();
builder.Services.AddBlazorise( options => { options.Immediate = true; }).AddMaterialProviders().AddMaterialIcons();
var app = builder.Build();