User löschen implementieren
This commit is contained in:
@@ -6,17 +6,24 @@ using Microsoft.EntityFrameworkCore;
|
||||
namespace FoodsharingSiegen.Server.Data.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// The user service class (a. beging, 11.04.2022)
|
||||
/// The user service class (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <seealso cref="ServiceBase"/>
|
||||
/// <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>
|
||||
/// Initializes a new instance of the <see cref="UserService"/> class
|
||||
/// Initializes a new instance of the <see cref="UserService" /> class
|
||||
/// </summary>
|
||||
/// <param name="context">The context</param>
|
||||
/// <param name="authService"></param>
|
||||
@@ -28,7 +35,7 @@ namespace FoodsharingSiegen.Server.Data.Service
|
||||
#region Public Method AddUserAsync
|
||||
|
||||
/// <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>
|
||||
/// <param name="user">The user</param>
|
||||
/// <returns>A task containing an operation result of user</returns>
|
||||
@@ -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<User>(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<User>(new Exception("Fehler bei der Registrierung"));
|
||||
|
||||
|
||||
await AuditService.Insert(AuditType.CreateUser, user.Mail);
|
||||
|
||||
|
||||
return new OperationResult<User>(user);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
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,10 +90,46 @@ 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>
|
||||
/// 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>
|
||||
/// <param name="user">The user</param>
|
||||
/// <returns>A task containing the operation result</returns>
|
||||
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Updates the user (a. beging, 11.04.2022)
|
||||
/// Updates the user (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="user">The user</param>
|
||||
/// <returns>A task containing the operation result</returns>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user