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.
 
 
 
 
 

35 lines
1.2 KiB

using Easy.AuthorityManagement.Clean.Shared.IServices;
using Easy.DDD.Domain;
using Easy.Snowflakes;
namespace Easy.AuthorityManagement.Clean.Domain.Services;
public class IdentityRoleManager : DomainService, IIdentityRoleManager
{
protected IRepository<IdentityRole> IdentityRoleRepository => LazyServiceProvider.LazyGetService<IRepository<IdentityRole>>();
private ISnowflakeIdGenerator SnowflakeIdGenerator => LazyServiceProvider.LazyGetService<ISnowflakeIdGenerator>();
protected virtual async Task ValidateNameAsync(string name, long? expectedId = null)
{
When.Is(string.IsNullOrEmpty(name), "角色名称不能为空");
var role = await IdentityRoleRepository.Set.FirstOrDefaultAsync(o => o.RoleName == name);
When.Is(role != null && role.Id != expectedId, "重复的角色名称: " + name);
}
public async Task ChangeNameAsync(IdentityRole role, string roleName)
{
await ValidateNameAsync(roleName, role.Id);
role.SetName(roleName);
}
public async Task<IdentityRole> CreateAsync(string roleName, long tenantId)
{
await ValidateNameAsync(roleName);
return new IdentityRole(SnowflakeIdGenerator.Create(), roleName, tenantId);
}
}