admin user
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<ApplicationDbContext>(options =>
|
||||
);
|
||||
|
||||
// 2. ASP.NET Core Identity f<>r Benutzerverwaltung registrieren
|
||||
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
|
||||
{
|
||||
options.SignIn.RequireConfirmedAccount = true;
|
||||
})
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>() // Stellen Sie sicher, dass Ihr DbContext-Name hier korrekt ist
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
// Optional: Passe die Anforderungen f<>r Passw<73>rter f<>r die Entwicklung an
|
||||
builder.Services.Configure<IdentityOptions>(options =>
|
||||
|
||||
@@ -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<IdentityUser> _userManager;
|
||||
// Wir verwenden nun den UserManager mit unserer neuen ApplicationUser-Klasse
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public AdminUserService(UserManager<IdentityUser> userManager)
|
||||
public AdminUserService(UserManager<ApplicationUser> 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<IEnumerable<UserDto>> GetAllUsersAsync() { return new List<UserDto>(); }
|
||||
public async Task<UserDto?> GetUserByIdAsync(string userId) { return null; }
|
||||
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
|
||||
{
|
||||
// Alle Benutzer aus der Datenbank laden
|
||||
var users = await _userManager.Users.ToListAsync();
|
||||
var userDtos = new List<UserDto>();
|
||||
|
||||
// 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<UserDto?> 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()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Webshop.Domain/Entities/ApplicationUser.cs
Normal file
22
Webshop.Domain/Entities/ApplicationUser.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System;
|
||||
|
||||
namespace Webshop.Domain.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Erweitert die Standard-Identity-Klasse, um anwendungsspezifische
|
||||
/// Eigenschaften für einen Benutzer zu speichern.
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,9 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.4" />
|
||||
<!-- Die Version kann leicht abweichen -->
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user