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.
97 lines
3.2 KiB
97 lines
3.2 KiB
3 years ago
|
using AutoMapper;
|
||
|
using Easy.DDD.Application;
|
||
|
using Easy.DDD.Domain.Repositories;
|
||
|
using Easy.Result;
|
||
|
using Identity.Api.Clean.Contracts.Models;
|
||
|
using Identity.Api.Clean.Domain.Entites;
|
||
|
using Identity.Api.Clean.Shared.IServices;
|
||
|
using Identity.Api.Clean.Shared.ValueObjects;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
||
|
namespace Identity.Api.Clean.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)
|
||
|
{
|
||
|
var identityRoleIds = await IdentityUserRoleRepository
|
||
|
.Where(o => o.UserId == id)
|
||
|
.Select(o => o.RoleId).ToListAsync();
|
||
|
|
||
|
if (identityRoleIds.Count != 0)
|
||
|
{
|
||
|
var userRoleModels = await IdentityRoleRepository
|
||
|
.Where(o => identityRoleIds.Contains(o.Id))
|
||
|
.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);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|