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.
 
 
 
 
 

94 lines
3.3 KiB

using AutoMapper;
using Easy.DDD.Application;
using Easy.DDD.Domain.Repositories;
using Easy.Result;
using IdentityServer.DDD.Contracts.Models;
using IdentityServer.DDD.Domain.Entites;
using IdentityServer.DDD.Shared.IServices;
using IdentityServer.DDD.Shared.ValueObjects;
using Microsoft.EntityFrameworkCore;
namespace IdentityServer.DDD.Application;
public class IdentityUserAppService : ApiService
{
protected IRepository<IdentityUser> TenantRepository { get; }
protected IRepository<IdentityRole> IdentityRoleRepository { get; }
protected IRepository<IdentityUserRole> IdentityUserRoleRepository { get; }
protected IPermissionGrantManager PermissionGrantManager { get; }
protected IMapper Mapper { get; }
public IdentityUserAppService(
IRepository<IdentityUser> tenantRepository,
IRepository<IdentityRole> identityRoleRepository,
IRepository<IdentityUserRole> identityUserRoleRepository,
IMapper mapper)
{
TenantRepository = tenantRepository;
IdentityRoleRepository = identityRoleRepository;
IdentityUserRoleRepository = identityUserRoleRepository;
Mapper = mapper;
}
/// <summary>
/// 绑定角色
/// </summary>
public virtual async Task<ApiResult> CreateBindRoleAsync(Guid id, Guid roleId, Guid tenantId)
{
var identityUserRole = await IdentityUserRoleRepository
.FirstOrDefaultAsync(o => o.UserId == id && o.RoleId == roleId && o.TenantId == tenantId);
if (identityUserRole == null)
{
var userRole = new IdentityUserRole(id, roleId, tenantId);
await IdentityUserRoleRepository.AddAsync(userRole, true);
}
return ApiResult.Success();
}
/// <summary>
/// 获得所有角色
/// </summary>
public virtual async Task<ApiResultValues<IdentityUserRoleModel>> GetRolesAsync(Guid id, Guid tenantId)
{
var identityRoleIds = await IdentityUserRoleRepository
.Where(o => o.UserId == id && o.TenantId == tenantId)
.Select(o => o.RoleId).ToListAsync();
if (identityRoleIds.Count != 0)
{
var userRoleModels = await IdentityRoleRepository
.Where(o => identityRoleIds.Contains(o.Id) && o.TenantId == tenantId)
.Select(o => new IdentityUserRoleModel()
{
RoleId = o.Id,
RoleName = o.RoleName
}).ToListAsync();
return ApiResult.ValuesSuccess(userRoleModels);
}
return ApiResult.ValuesSuccess<IdentityUserRoleModel>(new());
}
/// <summary>
/// 获得许可信息
/// </summary>
public virtual async Task<ApiResultValues<PermissionGroupValueObject>> GetPermissionGrantsAsync(Guid id, Guid tenantId)
{
var identityRoleIds = await IdentityUserRoleRepository
.Where(o => o.UserId == id && o.TenantId == tenantId)
.Select(o => o.RoleId)
.ToListAsync();
var input = identityRoleIds.Select(o => ("R", o)).ToList();
input.Add(("U", id));
var permissionNames = await PermissionGrantManager.GetPermissionNamesAsync(input.ToArray());
var values = PermissionGrantManager.GetPermissionGroups(permissionNames);
return ApiResult.Values(values);
}
}