怎樣優雅地增刪查改(二):擴充套件身份管理模組

2023-07-08 21:00:56

@


身份管理模組(Identity模組)為通用查詢介面的按組織架構查詢和按戶關係查詢提供查詢依據。

身份管理模組的領域層依賴Volo.Abp.Identity.Domain

Abp為我們實現了一套身份管理模組,此模組包含使用者管理、角色管理、組織管理、許可權管理等功能。詳細請參考身份管理模組

我們將基於Volo.Abp.Identity模組按需求擴充套件。將為其擴充套件組織管理功能的介面,以及人員關係(Relation)功能。

使用者關係管理

Relation是人員之間的關係,比如:簽約、關注,或者朋友關係等。人員之間的關係是單項的,也就是說可以A是B的好友,但B不一定是A的好友。

關係型別由Type來定義

正向關係:User -> RelatedUser,由查詢GetRelatedToUsersAsync實現;

反向關係:RelatedUser -> User,由查詢GetRelatedFromUsersAsync實現。

新增Relation實體:

public class Relation : FullAuditedAggregateRoot<long>
{
    public Guid? TenantId { get; set; }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public override long Id { get; protected set; }

    public Guid UserId { get; set; }

    [ForeignKey("UserId")]
    public IdentityUser User { get; set; }

    public Guid RelatedUserId { get; set; }

    [ForeignKey("RelatedUserId")]
    public IdentityUser RelatedUser { get; set; }

    public string Type { get; set; }

}

在模組設定中新增

public class IdentityEntityFrameworkCoreModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<IdentityDbContext>(options =>
        {
            options.AddRepository<IdentityUserOrganizationUnit, EfCoreRepository<IdentityDbContext, IdentityUserOrganizationUnit>>();
            options.AddRepository<Relation.Relation, EfCoreRepository<IdentityDbContext, Relation.Relation>>();
        });
    }
}

建立RelationManager,實現人員關係的正向和反向查詢

public async Task<List<Relation>> GetRelatedToUsersAsync(Guid userId, string type)
{
    var query = (await Repository.GetQueryableAsync())
        .WhereIf(userId != null, c => userId == c.UserId)
        .WhereIf(!string.IsNullOrEmpty(type), c => c.Type == type);
    var items = query.ToList();
    return items;

}

public async Task<List<Relation>> GetRelatedFromUsersAsync(Guid userId, string type)
{
    var query = (await Repository.GetQueryableAsync())
        .Where(c => userId == c.RelatedUserId)
        .WhereIf(!string.IsNullOrEmpty(type), c => c.Type == type);
    var items = query.ToList();
    return items;
}

擴充套件組織管理功能

組織(OrganizationUnit)是身份管理模組的核心概念,組織是樹形結構,組織之間存在父子關係。

我們對功能模組的介面進行擴充套件:

  1. 增加OrganizationUnit的增刪查改介面;

  2. 增加OrganizationUnit的移動介面;

  3. 增加人員與組織架構管理介面,如新增/刪除人員到組織架構,查詢組織架構下的人員,查詢未分配組織的人員等;

  4. 增加查詢根組織(GetRootOrganizationUnit)介面。

完整的應用層介面如下:

public interface IOrganizationUnitAppService : IBasicCurdAppService<OrganizationUnitDto, Guid, CreateOrganizationUnitInput, UpdateOrganizationUnitInput>, IApplicationService
{
    Task AddToOrganizationUnitAsync(UserToOrganizationUnitInput input);
    Task<List<OrganizationUnitDto>> GetCurrentOrganizationUnitsAsync();
    Task<PagedResultDto<IdentityUserDto>> GetOrganizationUnitUsersByPageAsync(GetOrganizationUnitUsersInput input);
    Task<List<IdentityUserDto>> GetOrganizationUnitUsersAsync(GetOrganizationUnitUsersInput input);
    Task<OrganizationUnitDto> GetRootOrganizationUnitAsync(Guid id);
    Task<List<OrganizationUnitDto>> GetRootOrganizationUnitsAsync(IEnumerable<Guid> ids);
    Task<OrganizationUnitDto> GetRootOrganizationUnitByDisplayNameAsync(GetRootOrganizationUnitByDisplayName input);
    Task<List<OrganizationUnitDto>> GetRootOrganizationUnitsByParentAsync(GetRootOrganizationUnitsByParentInput input);
    Task<bool> IsInOrganizationUnitAsync(UserToOrganizationUnitInput input);
    Task MoveOrganizationUnitAsync(MoveOrganizationUnitInput input);
    Task RemoveUserFromOrganizationUnitAsync(UserToOrganizationUnitInput input);
    Task<List<IdentityUserDto>> GetUsersWithoutOrganizationAsync(GetUserWithoutOrganizationInput input);
    Task<PagedResultDto<IdentityUserDto>> GetUsersWithoutOrganizationByPageAsync(GetUserWithoutOrganizationInput input);
}

建立可查詢倉儲

通用查詢介面過濾條件需要對IQueryable進行拼接,由於Volo.Abp.Identity.IIdentityUserRepository繼承自IBasicRepository,我們需要重新編寫一個IdentityUser的可查詢倉儲:QueryableIdentityUserRepository

其實現介面IQueryableIdentityUserRepository的定義如下:

public interface IQueryableIdentityUserRepository : IIdentityUserRepository
{
    Task<IQueryable<OrganizationUnit>> GetOrganizationUnitsQueryableAsync(Guid id, bool includeDetails = false);
    Task<IQueryable<IdentityUser>> GetOrganizationUnitUsersAsync(
        Guid id, string keyword, string[] type,
        bool includeDetails = false);
    Task<IQueryable<IdentityUser>> GetUsersWithoutOrganizationAsync(string keyword, string[] type);
}

實現控制器

為OrganizationUnitAppService 以及 RelationAppService 建立MVC控制器

完整的 OrganizationUnitController 程式碼如下:

namespace Matoapp.Identity.OrganizationUnit
{
    [Area(IdentityRemoteServiceConsts.ModuleName)]
    [RemoteService(Name = IdentityRemoteServiceConsts.RemoteServiceName)]
    [Route("api/identity/organizationUnit")]
    public class OrganizationUnitController : IdentityController, IOrganizationUnitAppService
    {
        private readonly IOrganizationUnitAppService _organizationUnitAppService;

        public OrganizationUnitController(IOrganizationUnitAppService organizationUnitAppService)
        {
            _organizationUnitAppService = organizationUnitAppService;
        }

        [HttpPost]
        [Route("AddToOrganizationUnit")]
        
        public async Task AddToOrganizationUnitAsync(UserToOrganizationUnitInput input)
        {
            await _organizationUnitAppService.AddToOrganizationUnitAsync(input);
        }

        [HttpPost]
        [Route("Create")]
        
        public async Task<OrganizationUnitDto> CreateAsync(CreateOrganizationUnitInput input)
        {
            return await _organizationUnitAppService.CreateAsync(input);
        }

        [HttpDelete]
        [Route("Delete")]
        
        public async Task DeleteAsync(Guid id)
        {
            await _organizationUnitAppService.DeleteAsync(id);
        }


        [HttpGet]
        [Route("Get")]
        
        public async Task<OrganizationUnitDto> GetAsync(Guid id)
        {
            return await _organizationUnitAppService.GetAsync(id);

        }

        [HttpGet]
        [Route("GetCurrentOrganizationUnits")]

        
        public async Task<List<OrganizationUnitDto>> GetCurrentOrganizationUnitsAsync()
        {
            return await _organizationUnitAppService.GetCurrentOrganizationUnitsAsync();
        }


        [HttpGet]
        [Route("GetOrganizationUnitUsers")]
        
        public async Task<List<IdentityUserDto>> GetOrganizationUnitUsersAsync(GetOrganizationUnitUsersInput input)
        {
            return await _organizationUnitAppService.GetOrganizationUnitUsersAsync(input);
        }

        [HttpGet]
        [Route("GetOrganizationUnitUsersByPage")]
        
        public async Task<PagedResultDto<IdentityUserDto>> GetOrganizationUnitUsersByPageAsync(GetOrganizationUnitUsersInput input)
        {
            return await _organizationUnitAppService.GetOrganizationUnitUsersByPageAsync(input);
        }

        [HttpGet]
        [Route("GetRootOrganizationUnit")]
        
        public async Task<OrganizationUnitDto> GetRootOrganizationUnitAsync(Guid id)
        {
            return await _organizationUnitAppService.GetRootOrganizationUnitAsync(id);
        }

        [HttpGet]
        [Route("GetRootOrganizationUnits")]
        
        public async Task<List<OrganizationUnitDto>> GetRootOrganizationUnitsAsync(IEnumerable<Guid> ids)
        {
            return await _organizationUnitAppService.GetRootOrganizationUnitsAsync(ids);
        }

        [HttpGet]
        [Route("GetRootOrganizationUnitByDisplayName")]
        
        public async Task<OrganizationUnitDto> GetRootOrganizationUnitByDisplayNameAsync(GetRootOrganizationUnitByDisplayName input)
        {
            return await _organizationUnitAppService.GetRootOrganizationUnitByDisplayNameAsync(input);
        }

        [HttpGet]
        [Route("GetRootOrganizationUnitsByParent")]
        
        public async Task<List<OrganizationUnitDto>> GetRootOrganizationUnitsByParentAsync(GetRootOrganizationUnitsByParentInput input)
        {
            return await _organizationUnitAppService.GetRootOrganizationUnitsByParentAsync(input);
        }

        [HttpGet]
        [Route("GetUsersWithoutOrganization")]
        
        public async Task<List<IdentityUserDto>> GetUsersWithoutOrganizationAsync(GetUserWithoutOrganizationInput input)
        {
            return await _organizationUnitAppService.GetUsersWithoutOrganizationAsync(input);
        }

        [HttpGet]
        [Route("GetUsersWithoutOrganizationByPage")]
        
        public async Task<PagedResultDto<IdentityUserDto>> GetUsersWithoutOrganizationByPageAsync(GetUserWithoutOrganizationInput input)
        {
            return await _organizationUnitAppService.GetUsersWithoutOrganizationByPageAsync(input);
        }

        [HttpGet]
        [Route("IsInOrganizationUnit")]
        
        public async Task<bool> IsInOrganizationUnitAsync(UserToOrganizationUnitInput input)
        {
            return await _organizationUnitAppService.IsInOrganizationUnitAsync(input);
        }

        [HttpPost]
        [Route("MoveOrganizationUnit")]
        
        public async Task MoveOrganizationUnitAsync(MoveOrganizationUnitInput input)
        {
            await _organizationUnitAppService.MoveOrganizationUnitAsync(input);
        }

        [HttpPost]
        [Route("RemoveUserFromOrganizationUnit")]
        
        public async Task RemoveUserFromOrganizationUnitAsync(UserToOrganizationUnitInput input)
        {
            await _organizationUnitAppService.RemoveUserFromOrganizationUnitAsync(input);
        }

        [HttpPut]
        [Route("Update")]
        
        public async Task<OrganizationUnitDto> UpdateAsync(UpdateOrganizationUnitInput input)
        {
            return await _organizationUnitAppService.UpdateAsync(input);
        }

    }

完整的 RelationController 程式碼如下:

    [Area(IdentityRemoteServiceConsts.ModuleName)]
    [RemoteService(Name = IdentityRemoteServiceConsts.RemoteServiceName)]
    [Route("api/identity/relation")]
    public class RelationController : IdentityController, IRelationAppService
    {
        private readonly IRelationAppService _relationAppService;

        public RelationController(IRelationAppService relationAppService)
        {
            _relationAppService = relationAppService;
        }

        [HttpDelete]
        [Route("ClearAllRelatedFromUsers")]

        public async Task ClearAllRelatedFromUsersAsync(GetRelatedUsersInput input)
        {
            await _relationAppService.ClearAllRelatedFromUsersAsync(input);
        }

        [HttpDelete]
        [Route("ClearAllRelatedToUsers")]

        public async Task ClearAllRelatedToUsersAsync(GetRelatedUsersInput input)
        {
            await _relationAppService.ClearAllRelatedToUsersAsync(input);
        }

        [HttpPost]
        [Route("Create")]

        public async Task<RelationDto> CreateAsync(ModifyRelationInput input)
        {
            return await _relationAppService.CreateAsync(input);
        }

        [HttpDelete]
        [Route("Delete")]

        public async Task DeleteAsync(EntityDto<long> input)
        {
            await _relationAppService.DeleteAsync(input);
        }

        [HttpDelete]
        [Route("DeleteByUserId")]

        public async Task DeleteByUserIdAsync(ModifyRelationInput input)
        {
            await _relationAppService.DeleteByUserIdAsync(input);
        }

        [HttpGet]
        [Route("GetRelatedFromUsers")]

        public async Task<List<IdentityUserDto>> GetRelatedFromUsersAsync(GetRelatedUsersInput input)
        {
            return await _relationAppService.GetRelatedFromUsersAsync(input);
        }

        [HttpGet]
        [Route("GetRelatedToUsers")]

        public async Task<List<IdentityUserDto>> GetRelatedToUsersAsync(GetRelatedUsersInput input)
        {
            return await _relationAppService.GetRelatedToUsersAsync(input);
        }

        [HttpGet]
        [Route("GetRelatedToUserIds")]
        public async Task<List<Guid>> GetRelatedToUserIdsAsync(GetRelatedUsersInput input)
        {
            return await _relationAppService.GetRelatedToUserIdsAsync(input);
        }


        [HttpGet]
        [Route("GetRelatedFromUserIds")]
        public async Task<List<Guid>> GetRelatedFromUserIdsAsync(GetRelatedUsersInput input)
        {
            return await _relationAppService.GetRelatedFromUserIdsAsync(input);
        }


    }

測試介面

上一章節我們已經將三個模組的依賴新增到MatoappHttpApiModule中,直接啟動HttpApi.Host就可以存取介面了。

[DependsOn(
    ...
    typeof(CommonHttpApiModule),
    typeof(HealthHttpApiModule),
    typeof(IdentityHttpApiModule)
    )]
public class MatoappHttpApiModule : AbpModule

Relation相關介面:

OrganizationUnit相關介面:

下一章節將介紹如何利用Identity模組為使用者的查詢提供組織架構和使用者關係的條件過濾。