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

95 lines
4.5 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 Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sozsoft.Platform.Entities;
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>();
2026-08-12 21:26:36 +00:00
context.ApplicationConfiguration.SetProperty("apiVersion", configuration.GetValue<string>("App:Version"));
// Uygulamanın çalışan sürümü: istemcinin tek sürüm kaynağı (ayrı bir
// version.json isteği yoktur). Yalnızca EN SON sürüm taşınır; tam liste
2026-08-17 08:51:46 +00:00
// ChangeLog ekranının kendi endpoint'inden (/api/app/change-log) gelir,
2026-08-12 21:26:36 +00:00
// aksi halde her config isteğinde bütün changelog gönderilirdi.
2026-08-17 08:51:46 +00:00
var changeLogRepository = context.ServiceProvider.GetRequiredService<IRepository<ChangeLog, Guid>>();
var changeLogs = await changeLogRepository.GetListAsync(x => x.IsPublished);
2026-08-12 21:26:36 +00:00
2026-08-17 08:51:46 +00:00
var latest = changeLogs
2026-08-12 21:26:36 +00:00
.OrderByDescending(note => note.ReleaseDate)
.ThenByDescending(note => note.Version)
.FirstOrDefault();
context.ApplicationConfiguration.SetProperty("changeLogs", new
{
uiVersion = latest?.Version,
tags = latest?.GetChangeLog() ?? []
});
2026-02-24 20:44:16 +00:00
// 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());
// Public site menusu: ziyaretci menu ucunu cagiramaz ([Authorize]), bu yuzden
// rotalar ve custom component'ler gibi anonim erisilen konfigurasyonla tasinir.
var menuRepository = context.ServiceProvider.GetRequiredService<IRepository<Menu, Guid>>();
var publicMenus = await menuRepository.GetListAsync(
menu => menu.ModuleId == PlatformConsts.PublicSite.ModuleId && !menu.IsDisabled);
context.ApplicationConfiguration.SetProperty("publicMenus", publicMenus
.OrderBy(menu => menu.Order)
.Select(menu => new
{
code = menu.Code,
displayName = menu.DisplayName,
url = menu.Url,
icon = menu.Icon,
order = menu.Order,
parentCode = menu.ParentCode,
cssClass = menu.CssClass,
target = menu.Target,
requiredPermissionName = menu.RequiredPermissionName
})
.ToList());
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
}
2026-08-12 21:26:36 +00:00
2026-02-24 20:44:16 +00:00
}