From bfbe283e9d5c0f7f022349bc5f986e4dab0228a2 Mon Sep 17 00:00:00 2001 From: Andre Beging Date: Wed, 8 Feb 2023 16:37:06 +0100 Subject: [PATCH] =?UTF-8?q?User=20l=C3=B6schen=20implementieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/Service/UserService.cs | 92 +++++++++++++------ FoodsharingSiegen.Server/Pages/Users.razor | 2 + FoodsharingSiegen.Server/Pages/Users.razor.cs | 86 ++++++++++++----- 3 files changed, 132 insertions(+), 48 deletions(-) diff --git a/FoodsharingSiegen.Server/Data/Service/UserService.cs b/FoodsharingSiegen.Server/Data/Service/UserService.cs index f9b7325..b388ace 100644 --- a/FoodsharingSiegen.Server/Data/Service/UserService.cs +++ b/FoodsharingSiegen.Server/Data/Service/UserService.cs @@ -6,17 +6,24 @@ using Microsoft.EntityFrameworkCore; namespace FoodsharingSiegen.Server.Data.Service { /// - /// The user service class (a. beging, 11.04.2022) + /// The user service class (a. beging, 11.04.2022) /// - /// + /// public class UserService : ServiceBase { + #region Private Properties + + /// + /// Gets the value of the audit service (ab) + /// private AuditService AuditService { get; } - + + #endregion + #region Setup/Teardown /// - /// Initializes a new instance of the class + /// Initializes a new instance of the class /// /// The context /// @@ -28,7 +35,7 @@ namespace FoodsharingSiegen.Server.Data.Service #region Public Method AddUserAsync /// - /// Adds the user using the specified user (a. beging, 11.04.2022) + /// Adds the user using the specified user (a. beging, 11.04.2022) /// /// The user /// A task containing an operation result of user @@ -38,26 +45,25 @@ namespace FoodsharingSiegen.Server.Data.Service { if (await Context.Users!.AnyAsync(x => x.Mail.ToLower().Equals(user.Mail.ToLower()))) return new OperationResult(new Exception("Diese E-Mail Adresse wird bereits verwendet")); - + user.Created = DateTime.UtcNow; - + if (string.IsNullOrWhiteSpace(user.Password)) user.Password = string.Empty; - + await Context.Users!.AddAsync(user); - + var saveResult = await Context.SaveChangesAsync(); if (saveResult == 0) return new OperationResult(new Exception("Fehler bei der Registrierung")); - + await AuditService.Insert(AuditType.CreateUser, user.Mail); - + return new OperationResult(user); } catch (Exception e) { return new OperationResult(e); } - } #endregion @@ -66,9 +72,7 @@ namespace FoodsharingSiegen.Server.Data.Service //////////////////////////////////////////////////////////////////////////////////////////////////// /// Gets users asynchronous. - /// /// A Beging, 20.10.2021. - /// /// An asynchronous result that yields the users. //////////////////////////////////////////////////////////////////////////////////////////////////// public async Task>> GetUsersAsync() @@ -86,10 +90,46 @@ namespace FoodsharingSiegen.Server.Data.Service #endregion + #region Public Method Remove + + /// + /// Removes the user id (a. beging, 08.02.2023) + /// + /// The user id + /// A task containing the operation result + public async Task 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 /// - /// Sets the password using the specified user (a. beging, 20.05.2022) + /// Sets the password using the specified user (a. beging, 20.05.2022) /// /// The user /// A task containing the operation result @@ -101,14 +141,14 @@ namespace FoodsharingSiegen.Server.Data.Service if (entityUser == null) return new OperationResult(new Exception("User not found")); entityUser.Password = user.Password; - + 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; await AuditService.Insert(AuditType.SetUserPassword, auditData); - + return new OperationResult(); } catch (Exception e) @@ -122,7 +162,7 @@ namespace FoodsharingSiegen.Server.Data.Service #region Public Method Update /// - /// Updates the user (a. beging, 11.04.2022) + /// Updates the user (a. beging, 11.04.2022) /// /// The user /// A task containing the operation result @@ -132,15 +172,13 @@ namespace FoodsharingSiegen.Server.Data.Service { var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id); if (entityUser == null) return new OperationResult(new Exception("User not found")); - + if (entityUser.Mail != user.Mail || entityUser.Verified != user.Verified || entityUser.Type != user.Type || entityUser.Groups != user.Groups) - { entityUser.ForceLogout = true; - } - + entityUser.Memo = user.Memo; entityUser.Mail = user.Mail; entityUser.Name = user.Name; @@ -151,10 +189,10 @@ namespace FoodsharingSiegen.Server.Data.Service if (!Context.HasChanges()) return new OperationResult(new Exception("Nichts zum Speichern gefunden")); - + 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); return new OperationResult(); } diff --git a/FoodsharingSiegen.Server/Pages/Users.razor b/FoodsharingSiegen.Server/Pages/Users.razor index 6fb78ee..5725de0 100644 --- a/FoodsharingSiegen.Server/Pages/Users.razor +++ b/FoodsharingSiegen.Server/Pages/Users.razor @@ -38,6 +38,8 @@ PopupTitleTemplate="PopupTitleTemplate" RowInserted="RowInserted" RowUpdated="RowUpdated" + RowRemoving="RowRemoving" + RowRemoved="RowRemoved" PageSize="50" @bind-SelectedRow="SelectedUser" diff --git a/FoodsharingSiegen.Server/Pages/Users.razor.cs b/FoodsharingSiegen.Server/Pages/Users.razor.cs index 7fe39ea..0e1bd9d 100644 --- a/FoodsharingSiegen.Server/Pages/Users.razor.cs +++ b/FoodsharingSiegen.Server/Pages/Users.razor.cs @@ -1,5 +1,7 @@ +using Blazorise; using Blazorise.DataGrid; using FoodsharingSiegen.Contracts.Entity; +using FoodsharingSiegen.Contracts.Helper; using FoodsharingSiegen.Server.Data.Service; using FoodsharingSiegen.Server.Dialogs; using Microsoft.AspNetCore.Components; @@ -7,51 +9,49 @@ using Microsoft.AspNetCore.Components; namespace FoodsharingSiegen.Server.Pages { /// - /// The users class (a. beging, 01.04.2022) + /// The users class (a. beging, 01.04.2022) /// public partial class Users { - #region Dependencies (Injected) - + #region Dependencies + //////////////////////////////////////////////////////////////////////////////////////////////////// /// Gets or sets the user service. - /// /// The user service. //////////////////////////////////////////////////////////////////////////////////////////////////// [Inject] public UserService UserService { get; set; } = null!; - #endregion #region Private Properties /// - /// Gets or sets the value of the password modal (ab) + /// Gets or sets the value of the password modal (ab) /// private SetPasswordModal? PasswordModal { get; set; } /// - /// Gets or sets the value of the selected company texts (ab) + /// Gets or sets the value of the selected company texts (ab) /// private List SelectedCompanyTexts { get; set; } = new(); /// - /// Gets or sets the value of the selected user (ab) + /// Gets or sets the value of the selected user (ab) /// private User? SelectedUser { get; set; } /// - /// Gets or sets the value of the user data grid (ab) + /// Gets or sets the value of the user data grid (ab) /// private DataGrid? UserDataGrid { get; set; } /// - /// Gets the value of the user groups (ab) + /// Gets the value of the user groups (ab) /// private IEnumerable UserGroups => Enum.GetValues(); /// - /// Gets or sets the value of the user list (ab) + /// Gets or sets the value of the user list (ab) /// private List? UserList { get; set; } @@ -60,14 +60,14 @@ namespace FoodsharingSiegen.Server.Pages #region Override OnAfterRenderAsync /// - /// 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) /// /// The first render protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) await LoadUsers(); - + await base.OnAfterRenderAsync(firstRender); } @@ -76,13 +76,13 @@ namespace FoodsharingSiegen.Server.Pages #region Private Method LoadUsers /// - /// Loads the users (a. beging, 01.04.2022) + /// Loads the users (a. beging, 01.04.2022) /// private async Task LoadUsers() { var usersR = await UserService.GetUsersAsync(); if (usersR.Success) UserList = usersR.Data; - + await InvokeAsync(StateHasChanged); } @@ -91,13 +91,13 @@ namespace FoodsharingSiegen.Server.Pages #region Private Method OnPasswordSet /// - /// 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) /// /// The user private async Task OnPasswordSet(User user) { var setPasswordR = await UserService.SetPassword(user); - if(setPasswordR.Success) + if (setPasswordR.Success) await Notification.Success("Passwort gespeichert"); } @@ -106,7 +106,7 @@ namespace FoodsharingSiegen.Server.Pages #region Private Method RowInserted /// - /// Rows the inserted using the specified arg (a. beging, 01.04.2022) + /// Rows the inserted using the specified arg (a. beging, 01.04.2022) /// /// The arg private async Task RowInserted(SavedRowItem> arg) @@ -120,10 +120,55 @@ namespace FoodsharingSiegen.Server.Pages #endregion + #region Private Method RowRemoved + + /// + /// Rows the removed using the specified arg (a. beging, 08.02.2023) + /// + /// The arg + 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 + + /// + /// Rows the removing using the specified arg (a. beging, 08.02.2023) + /// + /// The arg + private async Task RowRemoving(CancellableRowChange 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 /// - /// Rows the updated using the specified arg (a. beging, 01.04.2022) + /// Rows the updated using the specified arg (a. beging, 01.04.2022) /// /// The arg private async Task RowUpdated(SavedRowItem> 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; var updateR = await UserService.Update(arg.Item); - if(!updateR.Success) + if (!updateR.Success) await Notification.Error($"Fehler beim Speichern: {updateR.ErrorMessage}")!; - } #endregion