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.
 
 
 
 
 

108 lines
3.6 KiB

using AutoMapper;
using Easy;
using Easy.DDD.Application;
using Easy.DDD.Domain.Repositories;
using Easy.Extensions;
using Easy.Result;
using IdentityServer.DDD.Contracts.Inputs;
using IdentityServer.DDD.Contracts.Models;
using IdentityServer.DDD.Domain.Entites;
using IdentityServer.DDD.Shared.IServices;
using IdentityServer.DDD.Shared.ValueObjects;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
namespace IdentityServer.DDD.Application;
public class IdentityRoleAppService : ApiService
{
protected IRepository<IdentityRole> IdentityRoleRepository { get; }
protected IRepository<PermissionGrant> PermissionGrantRepository { get; }
protected IPermissionGrantManager PermissionGrantManager { get; }
protected IMapper Mapper { get; }
protected IIdentityRoleManager IdentityRoleManager { get; }
public IdentityRoleAppService(
IRepository<IdentityRole> tenantRepository,
IIdentityRoleManager identityRoleManager,
IPermissionGrantManager permissionGrantManager,
IMapper mapper)
{
IdentityRoleRepository = tenantRepository;
IdentityRoleManager = identityRoleManager;
PermissionGrantManager = permissionGrantManager;
Mapper = mapper;
}
public virtual async Task<ApiResultValue<IdentityRoleModel>> CreateAsync(IdentityRoleCreateInput input)
{
var identityRole = await IdentityRoleManager.CreateAsync(input.RoleName, input.TenantId);
Mapper.Map(input, identityRole);
await IdentityRoleRepository.AddAsync(identityRole, true);
var value = Mapper.Map<IdentityRoleModel>(identityRole);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResult> DeleteAsync(Guid id)
{
var tenant = await IdentityRoleRepository.FirstOrDefaultAsync(o => o.Id == id);
if (tenant != null)
{
await IdentityRoleRepository.RemoveAsync(tenant, true);
}
return ApiResult.Success();
}
public virtual async Task<ApiResultValue<IdentityRoleModel>> UpdateAsync(Guid id, IdentityRoleUpdateInput input)
{
var tenant = await IdentityRoleRepository.FirstOrDefaultAsync(o => o.Id == id);
When.Is(tenant == null, "此租户不存在");
await IdentityRoleManager.ChangeNameAsync(tenant, input.RoleName);
Mapper.Map(input, tenant);
await IdentityRoleRepository.UpdateAsync(tenant, true);
var value = Mapper.Map<IdentityRoleModel>(tenant);
return ApiResult.ValueSuccess(value);
}
/// <summary>
/// 获得角色
/// </summary>
public virtual async Task<ApiResultValue<IdentityRoleModel>> GetAsync(Guid id)
{
var tenant = await IdentityRoleRepository.FirstOrDefaultAsync(o => o.Id == id);
var value = Mapper.Map<IdentityRoleModel>(tenant);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResultPaged<IdentityRoleModel>> GetListAsync(GetIdentityRolesInput input)
{
var count = await IdentityRoleRepository.LongCountAsync();
var tenants = await IdentityRoleRepository.Set.ApplyPaged(input).ToListAsync();
var pageCount = AppData.PageCount(count, input.PageSize);
var valuePaged = Mapper.Map<List<IdentityRoleModel>>(tenants);
return ApiResult.ValuesPagedSuccess(pageCount, valuePaged);
}
/// <summary>
/// 获得许可信息
/// </summary>
public virtual async Task<ApiResultValues<PermissionGroupValueObject>> GetPermissionGrantsAsync(Guid id)
{
return ApiResult.Values(await PermissionGrantManager.GetPermissionGrantsAsync("R", id));
}
}