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.
 
 
 
 
 

47 lines
1.7 KiB

using Easy;
using Easy.Authorization.Abstractions;
using Easy.DDD.Domain;
using Easy.DDD.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using PermissionManagement.Api.DDD.Domain.Entites;
namespace PermissionManagement.Api.DDD.Domain;
public class PermissionManager : DomainService, IPermissionManager
{
protected IRepository<PermissionGrant> PermissionGrantRepository { get; }
protected IPermissionDefinitionManager PermissionDefinitionManager { get; }
public PermissionManager(IRepository<PermissionGrant> permissionGrantRepository,
IPermissionDefinitionManager permissionDefinitionManager)
{
PermissionGrantRepository = permissionGrantRepository;
PermissionDefinitionManager = permissionDefinitionManager;
}
public async Task SetAsync(string permissionName, string providerName, Guid providerKey, bool isGranted)
{
var permission = PermissionDefinitionManager.Get(permissionName);
When.Is(!permission.IsEnabled, $"这个权限权限 '{permissionName}' 被禁用!");
if (isGranted)
{
var permissionGrant = new PermissionGrant(GuidGenerator.Create(), permission.PermissionName, providerName, providerKey);
await PermissionGrantRepository.AddAsync(permissionGrant, true);
}
else
{
var permissionGrant = await PermissionGrantRepository
.Where(o => o.PermissionName == permissionName)
.Where(o => o.ProviderKey == providerKey)
.FirstOrDefaultAsync();
if (permissionGrant == null)
{
return;
}
await PermissionGrantRepository.RemoveAsync(permissionGrant, true);
}
}
}