diff --git a/Webshop.Api/Controllers/Admin/AdminUsersController.cs b/Webshop.Api/Controllers/Admin/AdminUsersController.cs index 5c708fd..73b3385 100644 --- a/Webshop.Api/Controllers/Admin/AdminUsersController.cs +++ b/Webshop.Api/Controllers/Admin/AdminUsersController.cs @@ -15,9 +15,9 @@ namespace Webshop.Api.Controllers.Admin [Authorize(Roles = "Admin")] public class AdminUsersController : ControllerBase { - private readonly AdminUserService _adminUserService; + private readonly IAdminUserService _adminUserService; - public AdminUsersController(AdminUserService adminUserService) + public AdminUsersController(IAdminUserService adminUserService) { _adminUserService = adminUserService; } diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index a6d5e34..9339b08 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -13,6 +13,7 @@ using Microsoft.AspNetCore.HttpOverrides; // For UseForwardedHeaders using Microsoft.Extensions.Logging; // For ILogger using Microsoft.OpenApi.Models; // For Swagger OpenAPI models using Webshop.Api.SwaggerFilters; // For AuthorizeOperationFilter +using Webshop.Domain.Entities; var builder = WebApplication.CreateBuilder(args); @@ -24,9 +25,12 @@ builder.Services.AddDbContext(options => ); // 2. ASP.NET Core Identity für Benutzerverwaltung registrieren -builder.Services.AddIdentity() - .AddEntityFrameworkStores() - .AddDefaultTokenProviders(); +builder.Services.AddIdentity(options => +{ + options.SignIn.RequireConfirmedAccount = true; +}) +.AddEntityFrameworkStores() // Stellen Sie sicher, dass Ihr DbContext-Name hier korrekt ist +.AddDefaultTokenProviders(); // Optional: Passe die Anforderungen für Passwörter für die Entwicklung an builder.Services.Configure(options => diff --git a/Webshop.Application/Services/Admin/AdminUserService.cs b/Webshop.Application/Services/Admin/AdminUserService.cs index a54416b..8161440 100644 --- a/Webshop.Application/Services/Admin/AdminUserService.cs +++ b/Webshop.Application/Services/Admin/AdminUserService.cs @@ -1,26 +1,65 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Webshop.Application.DTOs.Users; +// src/Webshop.Application/Services/Admin/AdminUserService.cs using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; - +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Webshop.Application.DTOs.Users; +using Webshop.Domain.Entities; // Wichtiges using für ApplicationUser namespace Webshop.Application.Services.Admin { public class AdminUserService : IAdminUserService { - private readonly UserManager _userManager; + // Wir verwenden nun den UserManager mit unserer neuen ApplicationUser-Klasse + private readonly UserManager _userManager; - public AdminUserService(UserManager userManager) + public AdminUserService(UserManager userManager) { _userManager = userManager; } - // HIER KOMMT DER VORHERIGE ADMINUSERSERVICE CODE HIN (GetAllUsersAsync, GetUserByIdAsync etc.) - // Hier sind Platzhalter-Implementierungen, die Sie durch den vollständigen Code ersetzen müssen: - public async Task> GetAllUsersAsync() { return new List(); } - public async Task GetUserByIdAsync(string userId) { return null; } + public async Task> GetAllUsersAsync() + { + // Alle Benutzer aus der Datenbank laden + var users = await _userManager.Users.ToListAsync(); + var userDtos = new List(); + + // Für jeden Benutzer ein DTO erstellen und die Daten mappen + foreach (var user in users) + { + userDtos.Add(new UserDto + { + Id = user.Id, + Email = user.Email ?? string.Empty, + UserName = user.UserName ?? string.Empty, + CreatedDate = user.CreatedDate, // Dieses Feld ist jetzt verfügbar! + EmailConfirmed = user.EmailConfirmed, + Roles = (await _userManager.GetRolesAsync(user)).ToList() // Rollen des Benutzers abrufen + }); + } + + return userDtos; + } + + public async Task GetUserByIdAsync(string userId) + { + var user = await _userManager.FindByIdAsync(userId); + if (user == null) + { + return null; + } + + // Den gefundenen Benutzer in ein DTO umwandeln + return new UserDto + { + Id = user.Id, + Email = user.Email ?? string.Empty, + UserName = user.UserName ?? string.Empty, + CreatedDate = user.CreatedDate, + EmailConfirmed = user.EmailConfirmed, + Roles = (await _userManager.GetRolesAsync(user)).ToList() + }; + } } -} +} \ No newline at end of file diff --git a/Webshop.Domain/Entities/ApplicationUser.cs b/Webshop.Domain/Entities/ApplicationUser.cs new file mode 100644 index 0000000..711d688 --- /dev/null +++ b/Webshop.Domain/Entities/ApplicationUser.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Identity; +using System; + +namespace Webshop.Domain.Entities +{ + /// + /// Erweitert die Standard-Identity-Klasse, um anwendungsspezifische + /// Eigenschaften für einen Benutzer zu speichern. + /// + public class ApplicationUser : IdentityUser + { + // Hinzugefügtes Feld, das in der Standard-IdentityUser-Klasse fehlt. + // Wird benötigt, um die Anforderung Ihrer UserDto zu erfüllen. + public DateTimeOffset CreatedDate { get; set; } + + // BEISPIELE FÜR WEITERE NÜTZLICHE FELDER (können bei Bedarf einkommentiert werden): + // public string? FirstName { get; set; } + // public string? LastName { get; set; } + // public byte[]? ProfilePicture { get; set; } + // public DateTimeOffset? LastLoginDate { get; set; } + } +} \ No newline at end of file diff --git a/Webshop.Domain/Webshop.Domain.csproj b/Webshop.Domain/Webshop.Domain.csproj index fa71b7a..997858e 100644 --- a/Webshop.Domain/Webshop.Domain.csproj +++ b/Webshop.Domain/Webshop.Domain.csproj @@ -6,4 +6,9 @@ enable + + + + +