You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.2 KiB

using Easy.Authorization.Realization;
using Easy.DDD.Application;
using Easy.DDD.Domain.Repositories;
using Easy.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using PermissionManagement.Api.DDD.Contracts.Dtos;
using PermissionManagement.Api.DDD.Contracts.Inputs;
using PermissionManagement.Api.DDD.Domain;
using PermissionManagement.Api.DDD.Domain.Entites;
namespace PermissionManagement.Api.DDD.Application;
[Authorize]
public class PermissionAppService : ApiService
{
private IPermissionManager PermissionManager { get; }
private IRepository<PermissionGrant> PermissionGrantRepository { get; }
protected Easy.Authorization.Abstractions.IPermissionDefinitionManager PermissionDefinitionManager { get; }
public PermissionAppService(
IPermissionManager permissionManager,
IRepository<PermissionGrant> permissionGrantRepository,
Easy.Authorization.Abstractions.IPermissionDefinitionManager permissionDefinitionManager)
{
PermissionManager = permissionManager;
PermissionGrantRepository = permissionGrantRepository;
PermissionDefinitionManager = permissionDefinitionManager;
}
public virtual async Task<ApiResultValue<GetPermissionListResultDto>> GetAsync(string providerName, Guid providerKey)
{
var result = new GetPermissionListResultDto()
{
ProviderName = providerName,
Groups = new List<PermissionGroupDto>()
};
var permissionNames = await PermissionGrantRepository.Where(o => o.ProviderKey == providerKey)
.Select(o => o.PermissionName).ToListAsync();
foreach (PermissionDefinitionGroup group in PermissionDefinitionManager.GetGroups())
{
var groupDto = new PermissionGroupDto
{
GroupName = group.GroupName,
Description = group.Description,
DisplayName = group.DisplayName,
Permissions = new List<PermissionGrantInfoDto>()
};
var permissionDefinitions = group.Permissions
.Where(o => permissionNames.Contains(o.PermissionName) && o.IsEnabled);
foreach (PermissionDefinition permission in permissionDefinitions)
{
var permissionGrantInfoDto = new PermissionGrantInfoDto()
{
DisplayName = permission.Description,
IsGranted = permission.IsEnabled,
Name = permission.PermissionName,
ParentName = permission.Parent?.PermissionName
};
groupDto.Permissions.Add(permissionGrantInfoDto);
}
if (groupDto.Permissions.Any())
{
result.Groups.Add(groupDto);
}
}
return ApiResult.ValueSuccess(result);
}
public async Task<ApiResult> UpdateAsync(UpdatePermissionsInput input)
{
foreach (var permission in input.Permissions)
{
await PermissionManager.SetAsync(permission.PermissionName, input.ProviderName, input.ProviderKey, permission.IsGranted);
}
return ApiResult.Success();
}
}