erp-platform/api/src/Kurs.Platform.Application/Menu/MenuAppService.cs
2025-10-12 21:45:03 +03:00

210 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kurs.Languages.Entities;
using Kurs.Platform.Entities;
using Kurs.Platform.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
using Volo.Abp.PermissionManagement;
using Volo.Abp.TenantManagement;
using static Kurs.Platform.Data.Seeds.SeedConsts;
namespace Kurs.Platform.Menus;
[Authorize]
public class MenuAppService : CrudAppService<
Menu,
MenuDto,
Guid,
PagedAndSortedResultRequestDto>
{
private readonly IRepository<Menu, Guid> _menuRepository;
private readonly IRepository<LanguageKey, Guid> _repositoryKey;
private readonly ITenantRepository _tenantRepository;
private readonly IPermissionDefinitionRecordRepository _permissionRepository;
public MenuAppService(
IRepository<Menu, Guid> menuRepository,
IRepository<LanguageKey, Guid> languageKeyRepository,
ITenantRepository tenantRepository,
IPermissionDefinitionRecordRepository permissionRepository
) : base(menuRepository)
{
_menuRepository = menuRepository;
_repositoryKey = languageKeyRepository;
_tenantRepository = tenantRepository;
_permissionRepository = permissionRepository;
CreatePolicyName = $"{AppCodes.Menus.Menu}.Create";
UpdatePolicyName = $"{AppCodes.Menus.Menu}.Update";
DeletePolicyName = $"{AppCodes.Menus.Menu}.Delete";
}
public override async Task<PagedResultDto<MenuDto>> GetListAsync(PagedAndSortedResultRequestDto input)
{
await CheckGetListPolicyAsync();
var query = await CreateFilteredQueryAsync(input);
query = query.Where(a => !a.IsDisabled);
//Tenant üzerinden MenuGrup bilgisi alınıp sadece o menüler listelenecek
if (CurrentTenant.IsAvailable)
{
var tenant = await _tenantRepository.FindAsync(CurrentTenant.Id.Value);
if (tenant != null)
{
var tenantMenuGroup = tenant.GetMenuGroup();
if (!tenantMenuGroup.IsNullOrWhiteSpace())
{
var permissionRecords = await _permissionRepository.GetListAsync();
var allowedPermissionNames = permissionRecords
.Where(p => p.GetMenuGroup().Contains(tenantMenuGroup))
.Select(p => p.Name)
.Distinct()
.ToList();
query = query.Where(menu =>
string.IsNullOrEmpty(menu.RequiredPermissionName) ||
allowedPermissionNames.Contains(menu.RequiredPermissionName));
}
}
}
var totalCount = await AsyncExecuter.CountAsync(query);
var entities = new List<Menu>();
var entityDtos = new List<MenuDto>();
if (totalCount > 0)
{
query = ApplySorting(query, input);
query = ApplyPaging(query, input);
var items = await AsyncExecuter.ToListAsync(query);
foreach (var item in items)
{
if (item.RequiredPermissionName.IsNullOrWhiteSpace())
{
entities.Add(item);
}
else
{
try
{
var result = await AuthorizationService.IsGrantedAsync(item.RequiredPermissionName);
if (result == true)
{
entities.Add(item);
}
}
catch (Exception ex)
{
Logger.LogError(ex, ex.Message);
}
}
}
entityDtos = await base.MapToGetListOutputDtosAsync(entities);
}
return new PagedResultDto<MenuDto>(
totalCount,
entityDtos
);
}
public override async Task<MenuDto> CreateAsync(MenuDto input)
{
await CheckCreatePolicyAsync();
var keyExists = await _repositoryKey.AnyAsync(
a => a.Key == input.DisplayName &&
a.ResourceName == PlatformConsts.AppName);
if (!keyExists)
{
await _repositoryKey.InsertAsync(new LanguageKey
{
Key = input.DisplayName,
ResourceName = PlatformConsts.AppName
});
}
return await base.CreateAsync(input);
}
public override async Task<MenuDto> UpdateAsync(Guid id, MenuDto input)
{
await CheckUpdatePolicyAsync();
var key = await _repositoryKey.FirstOrDefaultAsync(
a => a.Key == input.DisplayName &&
a.ResourceName == PlatformConsts.AppName);
if (key is null)
{
// Yeni Key'i olustur
await _repositoryKey.InsertAsync(new LanguageKey
{
Key = input.DisplayName,
ResourceName = PlatformConsts.AppName
});
}
return await base.UpdateAsync(id, input);
}
public async Task<List<MenuDto>> UpdateAllAsync(List<MenuDto> inputs)
{
await CheckUpdatePolicyAsync();
var result = new List<MenuDto>();
foreach (var input in inputs)
{
if (input.Id == Guid.Empty)
{
throw new ArgumentException("MenuDto içinde geçerli bir Id bulunmalıdır.");
}
var key = await _repositoryKey.FirstOrDefaultAsync(
a => a.Key == input.DisplayName &&
a.ResourceName == PlatformConsts.AppName);
if (key is null)
{
await _repositoryKey.InsertAsync(new LanguageKey
{
Key = input.DisplayName,
ResourceName = PlatformConsts.AppName
});
}
var updated = await base.UpdateAsync(input.Id, input);
result.Add(updated);
}
return result;
}
public override async Task DeleteAsync(Guid id)
{
await CheckDeletePolicyAsync();
var menu = await _menuRepository.GetAsync(id);
if (menu != null)
{
await _repositoryKey.DeleteAsync(
a => a.Key == menu.DisplayName &&
a.ResourceName == PlatformConsts.AppName);
}
await base.DeleteAsync(id);
}
}