User löschen implementieren
This commit is contained in:
@@ -11,8 +11,15 @@ namespace FoodsharingSiegen.Server.Data.Service
|
||||
/// <seealso cref="ServiceBase" />
|
||||
public class UserService : ServiceBase
|
||||
{
|
||||
#region Private Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the audit service (ab)
|
||||
/// </summary>
|
||||
private AuditService AuditService { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Setup/Teardown
|
||||
|
||||
/// <summary>
|
||||
@@ -57,7 +64,6 @@ namespace FoodsharingSiegen.Server.Data.Service
|
||||
{
|
||||
return new OperationResult<User>(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -66,9 +72,7 @@ namespace FoodsharingSiegen.Server.Data.Service
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Gets users asynchronous. </summary>
|
||||
///
|
||||
/// <remarks> A Beging, 20.10.2021. </remarks>
|
||||
///
|
||||
/// <returns> An asynchronous result that yields the users. </returns>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
public async Task<OperationResult<List<User>>> GetUsersAsync()
|
||||
@@ -86,6 +90,42 @@ namespace FoodsharingSiegen.Server.Data.Service
|
||||
|
||||
#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
|
||||
|
||||
/// <summary>
|
||||
@@ -137,9 +177,7 @@ namespace FoodsharingSiegen.Server.Data.Service
|
||||
entityUser.Verified != user.Verified ||
|
||||
entityUser.Type != user.Type ||
|
||||
entityUser.Groups != user.Groups)
|
||||
{
|
||||
entityUser.ForceLogout = true;
|
||||
}
|
||||
|
||||
entityUser.Memo = user.Memo;
|
||||
entityUser.Mail = user.Mail;
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
PopupTitleTemplate="PopupTitleTemplate"
|
||||
RowInserted="RowInserted"
|
||||
RowUpdated="RowUpdated"
|
||||
RowRemoving="RowRemoving"
|
||||
RowRemoved="RowRemoved"
|
||||
PageSize="50"
|
||||
|
||||
@bind-SelectedRow="SelectedUser"
|
||||
|
||||
@@ -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;
|
||||
@@ -11,16 +13,14 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
/// </summary>
|
||||
public partial class Users
|
||||
{
|
||||
#region Dependencies (Injected)
|
||||
#region Dependencies
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Gets or sets the user service. </summary>
|
||||
///
|
||||
/// <value> The user service. </value>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
[Inject] public UserService UserService { get; set; } = null!;
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
@@ -120,6 +120,51 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
|
||||
#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
|
||||
|
||||
/// <summary>
|
||||
@@ -133,7 +178,6 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
var updateR = await UserService.Update(arg.Item);
|
||||
if (!updateR.Success)
|
||||
await Notification.Error($"Fehler beim Speichern: {updateR.ErrorMessage}")!;
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user