sozsoft-platform/api/src/Sozsoft.Platform.HttpApi.Host/Extensions/PlatformApplicationConfigurationContributor.cs

54 lines
2.6 KiB
C#
Raw Normal View History

2026-02-24 20:44:16 +00:00
using System;
using System.Linq;
2026-02-24 20:44:16 +00:00
using System.Threading.Tasks;
using Sozsoft.Platform.Entities;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sozsoft.Platform.Identity;
2026-02-24 20:44:16 +00:00
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Users;
2026-02-24 20:44:16 +00:00
namespace Sozsoft.Platform.Extensions;
public class PlatformApplicationConfigurationContributor : IApplicationConfigurationContributor, ITransientDependency
{
public async Task ContributeAsync(ApplicationConfigurationContributorContext context)
{
var hostEnvironment = context.ServiceProvider.GetRequiredService<IHostEnvironment>();
context.ApplicationConfiguration.SetProperty("environment", hostEnvironment.EnvironmentName);
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
context.ApplicationConfiguration.SetProperty("version", configuration.GetValue<string>("App:Version"));
// GetListAsync boş liste döner, null dönmez — bu yüzden null kontrolü yok.
// Veritabanı erişilemezse çağrı exception atar; bu durum istek boru hattında,
// buraya gelinmeden 503 ile karşılanır (bkz. PlatformHttpApiHostModule).
2026-02-24 20:44:16 +00:00
var routeRepository = context.ServiceProvider.GetRequiredService<IRepository<Route, Guid>>();
context.ApplicationConfiguration.SetProperty("routes", await routeRepository.GetListAsync());
2026-02-24 20:44:16 +00:00
var customComponentRepository = context.ServiceProvider.GetRequiredService<IRepository<CustomComponent, Guid>>();
context.ApplicationConfiguration.SetProperty(
"customComponents", await customComponentRepository.GetListAsync());
var currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();
if (currentUser.Id.HasValue)
{
var sessionRepository = context.ServiceProvider.GetRequiredService<IIdentitySessionRepository>();
var sessions = await sessionRepository.GetListAsync(userId: currentUser.Id.Value);
var claimSessionId = currentUser.FindClaim(PlatformClaimTypes.SessionId.Key)?.Value;
var sessionId = sessions.Exists(session => session.SessionId == claimSessionId)
? claimSessionId
: sessions.OrderByDescending(session => session.SignedIn).FirstOrDefault()?.SessionId;
context.ApplicationConfiguration.CurrentUser.SessionId = sessionId;
context.ApplicationConfiguration.SetProperty("currentUserSessionId", sessionId);
}
2026-02-24 20:44:16 +00:00
}
}