User löschen implementieren

This commit is contained in:
Andre Beging
2023-02-08 16:37:06 +01:00
parent 0acd9485d4
commit bfbe283e9d
3 changed files with 132 additions and 48 deletions

View File

@@ -6,17 +6,24 @@ using Microsoft.EntityFrameworkCore;
namespace FoodsharingSiegen.Server.Data.Service namespace FoodsharingSiegen.Server.Data.Service
{ {
/// <summary> /// <summary>
/// The user service class (a. beging, 11.04.2022) /// The user service class (a. beging, 11.04.2022)
/// </summary> /// </summary>
/// <seealso cref="ServiceBase"/> /// <seealso cref="ServiceBase" />
public class UserService : ServiceBase public class UserService : ServiceBase
{ {
#region Private Properties
/// <summary>
/// Gets the value of the audit service (ab)
/// </summary>
private AuditService AuditService { get; } private AuditService AuditService { get; }
#endregion
#region Setup/Teardown #region Setup/Teardown
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UserService"/> class /// Initializes a new instance of the <see cref="UserService" /> class
/// </summary> /// </summary>
/// <param name="context">The context</param> /// <param name="context">The context</param>
/// <param name="authService"></param> /// <param name="authService"></param>
@@ -28,7 +35,7 @@ namespace FoodsharingSiegen.Server.Data.Service
#region Public Method AddUserAsync #region Public Method AddUserAsync
/// <summary> /// <summary>
/// Adds the user using the specified user (a. beging, 11.04.2022) /// Adds the user using the specified user (a. beging, 11.04.2022)
/// </summary> /// </summary>
/// <param name="user">The user</param> /// <param name="user">The user</param>
/// <returns>A task containing an operation result of user</returns> /// <returns>A task containing an operation result of user</returns>
@@ -57,7 +64,6 @@ namespace FoodsharingSiegen.Server.Data.Service
{ {
return new OperationResult<User>(e); return new OperationResult<User>(e);
} }
} }
#endregion #endregion
@@ -66,9 +72,7 @@ namespace FoodsharingSiegen.Server.Data.Service
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets users asynchronous. </summary> /// <summary> Gets users asynchronous. </summary>
///
/// <remarks> A Beging, 20.10.2021. </remarks> /// <remarks> A Beging, 20.10.2021. </remarks>
///
/// <returns> An asynchronous result that yields the users. </returns> /// <returns> An asynchronous result that yields the users. </returns>
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
public async Task<OperationResult<List<User>>> GetUsersAsync() public async Task<OperationResult<List<User>>> GetUsersAsync()
@@ -86,10 +90,46 @@ namespace FoodsharingSiegen.Server.Data.Service
#endregion #endregion
#region Public Method Remove
/// <summary>
/// Removes the user id (a. beging, 08.02.2023)
/// </summary>
/// <param name="userId">The user id</param>
/// <returns>A task containing the operation result</returns>
public async Task<OperationResult> RemoveAsync(Guid userId)
{
try
{
var user = await Context.Users!.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == userId);
if (user == null) return new OperationResult(new Exception("User not found"));
// Interaktionen vom aktuellen Nutzer übernehmen
if(CurrentUser?.Id != null)
foreach (var userInteraction in user.Interactions)
{
userInteraction.UserID = CurrentUser.Id;
}
Context.Users?.Remove(user);
var saveR = await Context.SaveChangesAsync();
if (saveR < 1) return new OperationResult(new Exception("Fehler beim Löschen"));
await AuditService.Insert(AuditType.RemoveUser, user.Mail);
return new OperationResult();
}
catch (Exception e)
{
return new OperationResult(e);
}
}
#endregion
#region Public Method SetPassword #region Public Method SetPassword
/// <summary> /// <summary>
/// Sets the password using the specified user (a. beging, 20.05.2022) /// Sets the password using the specified user (a. beging, 20.05.2022)
/// </summary> /// </summary>
/// <param name="user">The user</param> /// <param name="user">The user</param>
/// <returns>A task containing the operation result</returns> /// <returns>A task containing the operation result</returns>
@@ -104,7 +144,7 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern")); if (saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern"));
var auditData = CurrentUser?.Id == user.Id ? "sich selbst" : user.Mail; var auditData = CurrentUser?.Id == user.Id ? "sich selbst" : user.Mail;
await AuditService.Insert(AuditType.SetUserPassword, auditData); await AuditService.Insert(AuditType.SetUserPassword, auditData);
@@ -122,7 +162,7 @@ namespace FoodsharingSiegen.Server.Data.Service
#region Public Method Update #region Public Method Update
/// <summary> /// <summary>
/// Updates the user (a. beging, 11.04.2022) /// Updates the user (a. beging, 11.04.2022)
/// </summary> /// </summary>
/// <param name="user">The user</param> /// <param name="user">The user</param>
/// <returns>A task containing the operation result</returns> /// <returns>A task containing the operation result</returns>
@@ -137,9 +177,7 @@ namespace FoodsharingSiegen.Server.Data.Service
entityUser.Verified != user.Verified || entityUser.Verified != user.Verified ||
entityUser.Type != user.Type || entityUser.Type != user.Type ||
entityUser.Groups != user.Groups) entityUser.Groups != user.Groups)
{
entityUser.ForceLogout = true; entityUser.ForceLogout = true;
}
entityUser.Memo = user.Memo; entityUser.Memo = user.Memo;
entityUser.Mail = user.Mail; entityUser.Mail = user.Mail;
@@ -154,7 +192,7 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern")); if (saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern"));
await AuditService.Insert(AuditType.UpdateUser, user.Mail); await AuditService.Insert(AuditType.UpdateUser, user.Mail);
return new OperationResult(); return new OperationResult();
} }

View File

@@ -38,6 +38,8 @@
PopupTitleTemplate="PopupTitleTemplate" PopupTitleTemplate="PopupTitleTemplate"
RowInserted="RowInserted" RowInserted="RowInserted"
RowUpdated="RowUpdated" RowUpdated="RowUpdated"
RowRemoving="RowRemoving"
RowRemoved="RowRemoved"
PageSize="50" PageSize="50"
@bind-SelectedRow="SelectedUser" @bind-SelectedRow="SelectedUser"

View File

@@ -1,5 +1,7 @@
using Blazorise;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using FoodsharingSiegen.Contracts.Entity; using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Helper;
using FoodsharingSiegen.Server.Data.Service; using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Server.Dialogs; using FoodsharingSiegen.Server.Dialogs;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@@ -7,51 +9,49 @@ using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Pages namespace FoodsharingSiegen.Server.Pages
{ {
/// <summary> /// <summary>
/// The users class (a. beging, 01.04.2022) /// The users class (a. beging, 01.04.2022)
/// </summary> /// </summary>
public partial class Users public partial class Users
{ {
#region Dependencies (Injected) #region Dependencies
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets or sets the user service. </summary> /// <summary> Gets or sets the user service. </summary>
///
/// <value> The user service. </value> /// <value> The user service. </value>
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
[Inject] public UserService UserService { get; set; } = null!; [Inject] public UserService UserService { get; set; } = null!;
#endregion #endregion
#region Private Properties #region Private Properties
/// <summary> /// <summary>
/// Gets or sets the value of the password modal (ab) /// Gets or sets the value of the password modal (ab)
/// </summary> /// </summary>
private SetPasswordModal? PasswordModal { get; set; } private SetPasswordModal? PasswordModal { get; set; }
/// <summary> /// <summary>
/// Gets or sets the value of the selected company texts (ab) /// Gets or sets the value of the selected company texts (ab)
/// </summary> /// </summary>
private List<string> SelectedCompanyTexts { get; set; } = new(); private List<string> SelectedCompanyTexts { get; set; } = new();
/// <summary> /// <summary>
/// Gets or sets the value of the selected user (ab) /// Gets or sets the value of the selected user (ab)
/// </summary> /// </summary>
private User? SelectedUser { get; set; } private User? SelectedUser { get; set; }
/// <summary> /// <summary>
/// Gets or sets the value of the user data grid (ab) /// Gets or sets the value of the user data grid (ab)
/// </summary> /// </summary>
private DataGrid<User>? UserDataGrid { get; set; } private DataGrid<User>? UserDataGrid { get; set; }
/// <summary> /// <summary>
/// Gets the value of the user groups (ab) /// Gets the value of the user groups (ab)
/// </summary> /// </summary>
private IEnumerable<UserGroup> UserGroups => Enum.GetValues<UserGroup>(); private IEnumerable<UserGroup> UserGroups => Enum.GetValues<UserGroup>();
/// <summary> /// <summary>
/// Gets or sets the value of the user list (ab) /// Gets or sets the value of the user list (ab)
/// </summary> /// </summary>
private List<User>? UserList { get; set; } private List<User>? UserList { get; set; }
@@ -60,7 +60,7 @@ namespace FoodsharingSiegen.Server.Pages
#region Override OnAfterRenderAsync #region Override OnAfterRenderAsync
/// <summary> /// <summary>
/// Ons the after render using the specified first render (a. beging, 01.04.2022) /// Ons the after render using the specified first render (a. beging, 01.04.2022)
/// </summary> /// </summary>
/// <param name="firstRender">The first render</param> /// <param name="firstRender">The first render</param>
protected override async Task OnAfterRenderAsync(bool firstRender) protected override async Task OnAfterRenderAsync(bool firstRender)
@@ -76,7 +76,7 @@ namespace FoodsharingSiegen.Server.Pages
#region Private Method LoadUsers #region Private Method LoadUsers
/// <summary> /// <summary>
/// Loads the users (a. beging, 01.04.2022) /// Loads the users (a. beging, 01.04.2022)
/// </summary> /// </summary>
private async Task LoadUsers() private async Task LoadUsers()
{ {
@@ -91,13 +91,13 @@ namespace FoodsharingSiegen.Server.Pages
#region Private Method OnPasswordSet #region Private Method OnPasswordSet
/// <summary> /// <summary>
/// Ons the password set using the specified user (a. beging, 23.05.2022) /// Ons the password set using the specified user (a. beging, 23.05.2022)
/// </summary> /// </summary>
/// <param name="user">The user</param> /// <param name="user">The user</param>
private async Task OnPasswordSet(User user) private async Task OnPasswordSet(User user)
{ {
var setPasswordR = await UserService.SetPassword(user); var setPasswordR = await UserService.SetPassword(user);
if(setPasswordR.Success) if (setPasswordR.Success)
await Notification.Success("Passwort gespeichert"); await Notification.Success("Passwort gespeichert");
} }
@@ -106,7 +106,7 @@ namespace FoodsharingSiegen.Server.Pages
#region Private Method RowInserted #region Private Method RowInserted
/// <summary> /// <summary>
/// Rows the inserted using the specified arg (a. beging, 01.04.2022) /// Rows the inserted using the specified arg (a. beging, 01.04.2022)
/// </summary> /// </summary>
/// <param name="arg">The arg</param> /// <param name="arg">The arg</param>
private async Task RowInserted(SavedRowItem<User, Dictionary<string, object>> arg) private async Task RowInserted(SavedRowItem<User, Dictionary<string, object>> arg)
@@ -120,10 +120,55 @@ namespace FoodsharingSiegen.Server.Pages
#endregion #endregion
#region Private Method RowRemoved
/// <summary>
/// Rows the removed using the specified arg (a. beging, 08.02.2023)
/// </summary>
/// <param name="arg">The arg</param>
private async Task RowRemoved(User arg)
{
var removeR = await UserService.RemoveAsync(arg.Id);
if (!removeR.Success)
await Notification.Error($"Löschen: {removeR.ErrorMessage}")!;
else
await LoadUsers();
}
#endregion
#region Private Method RowRemoving
/// <summary>
/// Rows the removing using the specified arg (a. beging, 08.02.2023)
/// </summary>
/// <param name="arg">The arg</param>
private async Task RowRemoving(CancellableRowChange<User> arg)
{
if (arg.Item.IsAdmin())
{
await Notification.Error("Admins können nicht gelöscht werden!");
arg.Cancel = true;
return;
}
var confirm = await Message.Confirm($"User {arg.Item.Mail} löschen?", "Bestätigen", o =>
{
o.ConfirmButtonText = "Löschen";
o.CancelButtonText = "Abbrechen";
o.ShowMessageIcon = false;
o.ConfirmButtonColor = Color.Danger;
});
arg.Cancel = !confirm;
}
#endregion
#region Private Method RowUpdated #region Private Method RowUpdated
/// <summary> /// <summary>
/// Rows the updated using the specified arg (a. beging, 01.04.2022) /// Rows the updated using the specified arg (a. beging, 01.04.2022)
/// </summary> /// </summary>
/// <param name="arg">The arg</param> /// <param name="arg">The arg</param>
private async Task RowUpdated(SavedRowItem<User, Dictionary<string, object>> arg) private async Task RowUpdated(SavedRowItem<User, Dictionary<string, object>> arg)
@@ -131,9 +176,8 @@ namespace FoodsharingSiegen.Server.Pages
if (arg.Item?.Id == null || arg.Item.Id.Equals(Guid.Empty) || arg.Values?.Any() != true) return; if (arg.Item?.Id == null || arg.Item.Id.Equals(Guid.Empty) || arg.Values?.Any() != true) return;
var updateR = await UserService.Update(arg.Item); var updateR = await UserService.Update(arg.Item);
if(!updateR.Success) if (!updateR.Success)
await Notification.Error($"Fehler beim Speichern: {updateR.ErrorMessage}")!; await Notification.Error($"Fehler beim Speichern: {updateR.ErrorMessage}")!;
} }
#endregion #endregion