try
This commit is contained in:
19
Webshop.Application/DTOs/Auth/ChangePasswordRequestDto.cs
Normal file
19
Webshop.Application/DTOs/Auth/ChangePasswordRequestDto.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
// src/Webshop.Application/DTOs/Auth/ChangePasswordRequestDto.cs
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Webshop.Application.DTOs.Auth
|
||||
{
|
||||
public class ChangePasswordRequestDto
|
||||
{
|
||||
[Required(ErrorMessage = "Altes Passwort ist erforderlich.")]
|
||||
public string OldPassword { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Neues Passwort ist erforderlich.")]
|
||||
[MinLength(6, ErrorMessage = "Passwort muss mindestens 6 Zeichen lang sein.")]
|
||||
public string NewPassword { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Passwortbestätigung ist erforderlich.")]
|
||||
[Compare("NewPassword", ErrorMessage = "Neues Passwort und Bestätigung stimmen nicht überein.")]
|
||||
public string ConfirmNewPassword { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,26 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
// src/Webshop.Application/DTOs/Customers/UpdateCustomerProfileDto.cs
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Webshop.Application.DTOs.Customers
|
||||
{
|
||||
public class UpdateCustomerProfileDto
|
||||
{
|
||||
[Required(ErrorMessage = "Vorname ist erforderlich.")]
|
||||
[MaxLength(100)]
|
||||
[StringLength(100)]
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Nachname ist erforderlich.")]
|
||||
[MaxLength(100)]
|
||||
[StringLength(100)]
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
|
||||
|
||||
[Phone(ErrorMessage = "Ungültiges Telefonnummernformat.")]
|
||||
public string? PhoneNumber { get; set; } // Telefonnummer des Benutzers
|
||||
|
||||
[EmailAddress(ErrorMessage = "Ungültiges E-Mail-Format.")]
|
||||
public string? Email { get; set; } // E-Mail des Benutzers
|
||||
|
||||
// Optional, aber gute Sicherheitspraxis: Aktuelles Passwort zur Bestätigung sensibler Änderungen
|
||||
[Required(ErrorMessage = "Aktuelles Passwort ist zur Bestätigung erforderlich.")]
|
||||
public string CurrentPassword { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// src/Webshop.Application/DTOs/Users/AdminResetPasswordRequestDto.cs
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Webshop.Application.DTOs.Users
|
||||
{
|
||||
public class AdminResetPasswordRequestDto
|
||||
{
|
||||
[Required(ErrorMessage = "Benutzer-ID ist erforderlich.")]
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Neues Passwort ist erforderlich.")]
|
||||
[MinLength(6, ErrorMessage = "Passwort muss mindestens 6 Zeichen lang sein.")]
|
||||
public string NewPassword { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Passwortbestätigung ist erforderlich.")]
|
||||
[Compare("NewPassword", ErrorMessage = "Neues Passwort und Bestätigung stimmen nicht überein.")]
|
||||
public string ConfirmNewPassword { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,25 @@
|
||||
// src/Webshop.Application/DTOs/Users/UserDto.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Webshop.Application.DTOs.Users
|
||||
{
|
||||
public class UserDto
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
public List<string> Roles { get; set; } = new List<string>();
|
||||
public DateTimeOffset CreatedDate { get; set; }
|
||||
public bool EmailConfirmed { get; set; }
|
||||
public string Id { get; set; } = string.Empty; // Vom ApplicationUser.Id
|
||||
public string Email { get; set; } = string.Empty; // Vom ApplicationUser.Email
|
||||
public string UserName { get; set; } = string.Empty; // Vom ApplicationUser.UserName
|
||||
public List<string> Roles { get; set; } = new List<string>(); // Aus Identity-System
|
||||
public DateTimeOffset CreatedDate { get; set; } // Vom ApplicationUser.CreatedDate
|
||||
public bool EmailConfirmed { get; set; } // Vom ApplicationUser.EmailConfirmed
|
||||
|
||||
// Hinzugef<65>gte Felder
|
||||
public DateTimeOffset? LastActive { get; set; }
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public DateTimeOffset? LastActive { get; set; } // Vom ApplicationUser.LastActive
|
||||
public string? PhoneNumber { get; set; } // Vom ApplicationUser.PhoneNumber
|
||||
|
||||
// << NEU: Customer-Felder, die NICHT in ApplicationUser sind >>
|
||||
public string FirstName { get; set; } = string.Empty; // Vom Customer.FirstName
|
||||
public string LastName { get; set; } = string.Empty; // Vom Customer.LastName
|
||||
public Guid? DefaultShippingAddressId { get; set; } // Vom Customer
|
||||
public Guid? DefaultBillingAddressId { get; set; } // Vom Customer
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using System.Linq;
|
||||
using Webshop.Application.DTOs.Products;
|
||||
using Webshop.Application.Services.Admin.Interfaces; // F<>r Select
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminProductService : IAdminProductService // Sicherstellen, dass IAdminProductService implementiert wird
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Suppliers;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
using Webshop.Domain.Identity;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
|
||||
@@ -8,6 +8,7 @@ using Webshop.Application.DTOs.Users;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Infrastructure.Data; // WICHTIG: Stellt sicher, dass ApplicationDbContext gefunden wird.
|
||||
using Webshop.Domain.Identity;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
// src/Webshop.Application/Services/Admin/Interfaces/IAdminUserService.cs
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
|
||||
@@ -1,52 +1,122 @@
|
||||
// src/Webshop.Application/Services/Customers/CustomerService.cs
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Customers;
|
||||
using Webshop.Application.Services.Customers.Interfaces;
|
||||
using Webshop.Domain.Interfaces; // Wichtig für ICustomerRepository
|
||||
using Webshop.Application.DTOs; // CustomerDto
|
||||
using Webshop.Application.DTOs.Auth; // ChangePasswordRequestDto
|
||||
using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
|
||||
using Webshop.Domain.Entities; // Customer Entity
|
||||
using Webshop.Domain.Interfaces; // ICustomerRepository
|
||||
using Webshop.Domain.Identity; // Für ApplicationUser
|
||||
using System.Linq; // Für Select
|
||||
using System.Collections.Generic; // Für IEnumerable
|
||||
|
||||
namespace Webshop.Application.Services.Customers
|
||||
{
|
||||
public class CustomerService : ICustomerService
|
||||
{
|
||||
private readonly ICustomerRepository _customerRepository;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public CustomerService(ICustomerRepository customerRepository)
|
||||
public CustomerService(ICustomerRepository customerRepository, UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_customerRepository = customerRepository;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
public async Task<CustomerDto?> GetMyProfileAsync(string userId)
|
||||
{
|
||||
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
||||
if (customer == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (customer == null) return null;
|
||||
|
||||
var identityUser = await _userManager.FindByIdAsync(userId);
|
||||
if (identityUser == null) return null;
|
||||
|
||||
// Mappe die Entity auf das CustomerDto
|
||||
return new CustomerDto
|
||||
{
|
||||
Id = customer.Id,
|
||||
UserId = customer.AspNetUserId,
|
||||
FirstName = customer.FirstName,
|
||||
LastName = customer.LastName,
|
||||
// Fügen Sie hier weitere Felder hinzu, die der Kunde sehen soll (Email, Phone etc.)
|
||||
Email = identityUser.Email ?? string.Empty, // E-Mail vom ApplicationUser
|
||||
PhoneNumber = identityUser.PhoneNumber, // Telefonnummer vom ApplicationUser
|
||||
DefaultShippingAddressId = customer.DefaultShippingAddressId,
|
||||
DefaultBillingAddressId = customer.DefaultBillingAddressId
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto)
|
||||
public async Task<(bool Success, string ErrorMessage)> ChangePasswordAsync(string userId, ChangePasswordRequestDto request)
|
||||
{
|
||||
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
||||
if (customer == null)
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user == null) return (false, "Benutzer nicht gefunden.");
|
||||
|
||||
var result = await _userManager.ChangePasswordAsync(user, request.OldPassword, request.NewPassword);
|
||||
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
return false; // Kunde nicht gefunden
|
||||
var errors = string.Join(" ", result.Errors.Select(e => e.Description));
|
||||
return (false, errors);
|
||||
}
|
||||
|
||||
// Aktualisiere die Felder
|
||||
return (true, "Passwort erfolgreich geändert.");
|
||||
}
|
||||
|
||||
// << NEUE IMPLEMENTIERUNG: UpdateMyProfileAsync verarbeitet alle Felder >>
|
||||
public async Task<(bool Success, string ErrorMessage)> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto)
|
||||
{
|
||||
var customer = await _customerRepository.GetByUserIdAsync(userId);
|
||||
if (customer == null) return (false, "Kundenprofil nicht gefunden.");
|
||||
|
||||
var identityUser = await _userManager.FindByIdAsync(userId);
|
||||
if (identityUser == null) return (false, "Benutzerkonto nicht gefunden.");
|
||||
|
||||
// 1. Aktuelles Passwort prüfen (für alle sensiblen Änderungen)
|
||||
if (!await _userManager.CheckPasswordAsync(identityUser, profileDto.CurrentPassword))
|
||||
{
|
||||
return (false, "Falsches aktuelles Passwort zur Bestätigung.");
|
||||
}
|
||||
|
||||
// 2. Felder der Customer-Entität aktualisieren (FirstName, LastName)
|
||||
customer.FirstName = profileDto.FirstName;
|
||||
customer.LastName = profileDto.LastName;
|
||||
// customer.PhoneNumber = profileDto.PhoneNumber; // Entfernt, da es jetzt in ApplicationUser zentralisiert ist
|
||||
await _customerRepository.UpdateAsync(customer); // Speichert Änderungen im Customer-Profil
|
||||
|
||||
await _customerRepository.UpdateAsync(customer);
|
||||
return true;
|
||||
// 3. Felder des ApplicationUser (IdentityUser) aktualisieren (Email, PhoneNumber)
|
||||
bool identityUserChanged = false;
|
||||
|
||||
// E-Mail aktualisieren (wenn anders und nicht leer)
|
||||
if (!string.IsNullOrEmpty(profileDto.Email) && identityUser.Email != profileDto.Email)
|
||||
{
|
||||
identityUser.Email = profileDto.Email;
|
||||
identityUser.NormalizedEmail = _userManager.NormalizeEmail(profileDto.Email);
|
||||
identityUser.UserName = profileDto.Email; // Oft wird der UserName auch mit der E-Mail synchronisiert
|
||||
identityUser.NormalizedUserName = _userManager.NormalizeName(profileDto.Email);
|
||||
// Optional: user.EmailConfirmed = false; wenn Sie Bestätigungs-E-Mails senden
|
||||
identityUserChanged = true;
|
||||
}
|
||||
|
||||
// Telefonnummer aktualisieren (wenn anders und nicht leer)
|
||||
if (!string.IsNullOrEmpty(profileDto.PhoneNumber) && identityUser.PhoneNumber != profileDto.PhoneNumber)
|
||||
{
|
||||
identityUser.PhoneNumber = profileDto.PhoneNumber;
|
||||
// Optional: identityUser.PhoneNumberConfirmed = false;
|
||||
identityUserChanged = true;
|
||||
}
|
||||
|
||||
if (identityUserChanged)
|
||||
{
|
||||
var updateResult = await _userManager.UpdateAsync(identityUser);
|
||||
if (!updateResult.Succeeded)
|
||||
{
|
||||
var errors = string.Join(" ", updateResult.Errors.Select(e => e.Description));
|
||||
return (false, $"Fehler beim Aktualisieren der Kontaktdaten: {errors}");
|
||||
}
|
||||
}
|
||||
|
||||
return (true, "Profil und Kontaktdaten erfolgreich aktualisiert.");
|
||||
}
|
||||
|
||||
// << ENTFERNT: UpdateMyContactInfoAsync >>
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
// src/Webshop.Application/Services/Customers/ICustomerService.cs
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Customers; // Korrektes Using für DTOs
|
||||
using Webshop.Application.DTOs; // CustomerDto
|
||||
using Webshop.Application.DTOs.Auth; // ChangePasswordRequestDto
|
||||
using Webshop.Application.DTOs.Customers; // UpdateCustomerProfileDto
|
||||
|
||||
namespace Webshop.Application.Services.Customers.Interfaces
|
||||
namespace Webshop.Application.Services.Customers
|
||||
{
|
||||
public interface ICustomerService
|
||||
{
|
||||
Task<CustomerDto?> GetMyProfileAsync(string userId);
|
||||
Task<bool> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto);
|
||||
Task<(bool Success, string ErrorMessage)> ChangePasswordAsync(string userId, ChangePasswordRequestDto request);
|
||||
Task<(bool Success, string ErrorMessage)> UpdateMyProfileAsync(string userId, UpdateCustomerProfileDto profileDto);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user