checkout
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
// src/Webshop.Application/Services/Customers/CustomerService.cs
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Resend;
|
||||
using System; // Für Uri
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Webshop.Application.DTOs;
|
||||
using Webshop.Application; // Für ServiceResult
|
||||
using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Customers;
|
||||
using Webshop.Application.Services.Customers.Interfaces; // Namespace korrigiert
|
||||
using Webshop.Application.Services.Public.Interfaces; // WICHTIG: Für IEmailService
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Identity;
|
||||
using Webshop.Domain.Interfaces;
|
||||
|
||||
@@ -18,14 +19,18 @@ namespace Webshop.Application.Services.Customers
|
||||
private readonly ICustomerRepository _customerRepository;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IResend _resend;
|
||||
private readonly IEmailService _emailService; // NEU: Statt IResend
|
||||
|
||||
public CustomerService(ICustomerRepository customerRepository, UserManager<ApplicationUser> userManager, IConfiguration configuration, IResend resend)
|
||||
public CustomerService(
|
||||
ICustomerRepository customerRepository,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IConfiguration configuration,
|
||||
IEmailService emailService) // NEU: Injected
|
||||
{
|
||||
_customerRepository = customerRepository;
|
||||
_userManager = userManager;
|
||||
_configuration = configuration;
|
||||
_resend = resend;
|
||||
_emailService = emailService;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult<CustomerDto>> GetMyProfileAsync(string userId)
|
||||
@@ -60,17 +65,20 @@ namespace Webshop.Application.Services.Customers
|
||||
var identityUser = await _userManager.FindByIdAsync(userId);
|
||||
if (identityUser == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Benutzerkonto nicht gefunden.");
|
||||
|
||||
// Sicherheitscheck: Passwort prüfen
|
||||
if (!await _userManager.CheckPasswordAsync(identityUser, profileDto.CurrentPassword))
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.InvalidInput, "Das zur Bestätigung eingegebene Passwort ist falsch.");
|
||||
}
|
||||
|
||||
// Customer Daten aktualisieren
|
||||
customer.FirstName = profileDto.FirstName;
|
||||
customer.LastName = profileDto.LastName;
|
||||
customer.DefaultShippingAddressId = profileDto.DefaultShippingAddressId;
|
||||
customer.DefaultBillingAddressId = profileDto.DefaultBillingAddressId;
|
||||
await _customerRepository.UpdateAsync(customer);
|
||||
|
||||
// User Daten (Telefon) aktualisieren
|
||||
if (!string.IsNullOrEmpty(profileDto.PhoneNumber) && identityUser.PhoneNumber != profileDto.PhoneNumber)
|
||||
{
|
||||
identityUser.PhoneNumber = profileDto.PhoneNumber;
|
||||
@@ -103,28 +111,29 @@ namespace Webshop.Application.Services.Customers
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Benutzer nicht gefunden.");
|
||||
|
||||
// Passwort prüfen
|
||||
if (!await _userManager.CheckPasswordAsync(user, currentPassword))
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.InvalidInput, "Das zur Bestätigung eingegebene Passwort ist falsch.");
|
||||
}
|
||||
|
||||
// Prüfen, ob neue Email schon existiert (außer es ist die eigene)
|
||||
if (user.Email != newEmail && await _userManager.FindByEmailAsync(newEmail) != null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.Conflict, "Die neue E-Mail-Adresse ist bereits registriert.");
|
||||
}
|
||||
|
||||
// Token generieren
|
||||
var token = await _userManager.GenerateChangeEmailTokenAsync(user, newEmail);
|
||||
var clientUrl = _configuration["App:ClientUrl"]!;
|
||||
var confirmationLink = $"{clientUrl}/confirm-email-change?userId={user.Id}&newEmail={HttpUtility.UrlEncode(newEmail)}&token={HttpUtility.UrlEncode(token)}";
|
||||
var clientUrl = _configuration["App:ClientUrl"]; // Hier stand vorher ! , besser prüfen oder Null-Check
|
||||
|
||||
var message = new EmailMessage();
|
||||
message.From = _configuration["Resend:FromEmail"]!;
|
||||
message.To.Add(newEmail);
|
||||
message.Subject = "Bestätigen Sie Ihre neue E-Mail-Adresse";
|
||||
message.HtmlBody = $"<h1>E-Mail-Änderung Bestätigung</h1><p>Bitte klicken Sie auf den folgenden Link, um Ihre neue E-Mail-Adresse zu bestätigen:</p><p><a href='{confirmationLink}'>Neue E-Mail-Adresse bestätigen</a></p>";
|
||||
await _resend.EmailSendAsync(message);
|
||||
// Link bauen (Uri.EscapeDataString ist sicherer in URLs als HttpUtility)
|
||||
var confirmationLink = $"{clientUrl}/confirm-email-change?userId={user.Id}&newEmail={Uri.EscapeDataString(newEmail)}&token={Uri.EscapeDataString(token)}";
|
||||
|
||||
// Die Erfolgsmeldung wird hier als Teil des "Ok"-Results übermittelt
|
||||
// --- REFACTORED: Nutzung des EmailService ---
|
||||
await _emailService.SendEmailChangeConfirmationAsync(newEmail, confirmationLink);
|
||||
|
||||
// Wir geben die Erfolgsmeldung im Result zurück (passend zu deinem Controller)
|
||||
return ServiceResult.Ok("Bestätigungs-E-Mail wurde an die neue Adresse gesendet. Bitte prüfen Sie Ihr Postfach.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user