95 lines
3.8 KiB
C#
95 lines
3.8 KiB
C#
using Koogle.Domain.Interfaces;
|
||
using Koogle.Infrastrcuture.Services;
|
||
using Koogle.Infrastructure.Data;
|
||
using Koogle.Infrastructure.Identity;
|
||
using Koogle.Infrastructure.Repositories;
|
||
using Koogle.Infrastructure.Security;
|
||
using Koogle.Infrastructure.Services;
|
||
using KoogleApp.Data;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Identity;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
|
||
namespace Koogle.Infrastructure;
|
||
public static class DependencyInjection
|
||
{
|
||
public static IServiceCollection AddInfrastructure(this IServiceCollection services,
|
||
IConfiguration configuration)
|
||
{
|
||
var conn = configuration.GetConnectionString("AppDb") ?? throw new InvalidOperationException("Connection string 'AppDb' not found.");
|
||
|
||
services.AddDatabaseDeveloperPageExceptionFilter();
|
||
|
||
|
||
// Domain DbContext (app schema)
|
||
services.AddDbContext<AppDbContext>(options =>
|
||
options.UseSqlServer(conn, sql =>
|
||
{
|
||
// Eigene Migrations-History für Domain
|
||
sql.MigrationsHistoryTable("__EFMigrationsHistory_App", "app");
|
||
}));
|
||
|
||
// DbContext Factory for scoped contexts in services
|
||
services.AddDbContextFactory<AppDbContext>(options =>
|
||
options.UseSqlServer(conn, sql =>
|
||
{
|
||
sql.MigrationsHistoryTable("__EFMigrationsHistory_App", "app");
|
||
}), ServiceLifetime.Scoped);
|
||
|
||
// Identity DbContext (auth schema)
|
||
services.AddDbContext<AppIdentityDbContext>(options =>
|
||
options.UseSqlServer(conn, sql =>
|
||
{
|
||
// Eigene Migrations-History für Identity
|
||
sql.MigrationsHistoryTable("__EFMigrationsHistory_Auth", "auth");
|
||
}));
|
||
|
||
// Identity Services (UserManager/SignInManager etc.)
|
||
services
|
||
.AddIdentity<ApplicationUser, ApplicationRole>(options =>
|
||
{
|
||
// Optionen (Passwort, Lockout etc.) – Identity lässt sich hier konfigurieren. [2](https://krahgruppe-my.sharepoint.com/personal/c_kauer_krah-gruppe_de1/Documents/Microsoft%20Copilot%20Chat-Dateien/Person.cs)
|
||
})
|
||
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
||
.AddDefaultTokenProviders()
|
||
.AddClaimsPrincipalFactory<CustomClaimsPrincipalFactory>();
|
||
|
||
services.AddAuthentication("Cookies")
|
||
.AddCookie("Cookies", options =>
|
||
{
|
||
options.LoginPath = "/login";
|
||
options.AccessDeniedPath = "/login";
|
||
});
|
||
|
||
services.AddAuthorization(options =>
|
||
{
|
||
options.AddPolicy("ClubViewer", p => p.Requirements.Add(new ClubRoleRequirement("Viewer")));
|
||
options.AddPolicy("ClubEditor", p => p.Requirements.Add(new ClubRoleRequirement("Editor")));
|
||
options.AddPolicy("ClubAdmin", p => p.Requirements.Add(new ClubRoleRequirement("Admin")));
|
||
});
|
||
services.AddSingleton<IAuthorizationHandler, ClubRoleHandler>();
|
||
services.AddSingleton<IAuthorizationHandler, ClubRoleResourceHandler>();
|
||
|
||
// HTTP Context Accessor (für CurrentUserService)
|
||
services.AddHttpContextAccessor();
|
||
|
||
// Current UserProfile Service
|
||
services.AddScoped<ICurrentUserService, CurrentUserService>();
|
||
|
||
// Repositories
|
||
services.AddScoped<IClubRepository, ClubRepository>();
|
||
services.AddScoped<IPersonRepository, PersonRepository>();
|
||
services.AddScoped<IExpenseRepository, ExpenseRepository>();
|
||
services.AddScoped<IDayRepository, DayRepository>();
|
||
services.AddScoped<IPersonExpenseRepository, PersonExpenseRepository>();
|
||
|
||
// Services
|
||
services.AddScoped<IEmailService, StubEmailService>();
|
||
|
||
services.AddCascadingAuthenticationState();
|
||
|
||
return services;
|
||
}
|
||
} |