怎样优雅地增删查改(二):扩展身份管理模块

2023-07-08 20:34:31

@

目录用户关系管理扩展组织管理功能创建可查询仓储实现控制器测试接口身份管理模块(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{    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(options =>        {            options.AddRepository>();            options.AddRepository>();        });    }}

创建RelationManager,实现人员关系的正向和反向查询

public async Task> 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> 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)是身份管理模块的核心概念,组织是树形结构,组织之间存在父子关系。

我们对功能模块的接口进行扩展:

增加OrganizationUnit的增删查改接口;

增加OrganizationUnit的移动接口;

增加人员与组织架构管理接口,如添加/删除人员到组织架构,查询组织架构下的人员,查询未分配组织的人员等;

增加查询根组织(GetRootOrganizationUnit)接口。

完整的应用层接口如下:

public interface IOrganizationUnitAppService : IBasicCurdAppService, IApplicationService{    Task AddToOrganizationUnitAsync(UserToOrganizationUnitInput input);    Task> GetCurrentOrganizationUnitsAsync();    Task> GetOrganizationUnitUsersByPageAsync(GetOrganizationUnitUsersInput input);    Task> GetOrganizationUnitUsersAsync(GetOrganizationUnitUsersInput input);    Task GetRootOrganizationUnitAsync(Guid id);    Task> GetRootOrganizationUnitsAsync(IEnumerable ids);    Task GetRootOrganizationUnitByDisplayNameAsync(GetRootOrganizationUnitByDisplayName input);    Task> GetRootOrganizationUnitsByParentAsync(GetRootOrganizationUnitsByParentInput input);    Task IsInOrganizationUnitAsync(UserToOrganizationUnitInput input);    Task MoveOrganizationUnitAsync(MoveOrganizationUnitInput input);    Task RemoveUserFromOrganizationUnitAsync(UserToOrganizationUnitInput input);    Task> GetUsersWithoutOrganizationAsync(GetUserWithoutOrganizationInput input);    Task> GetUsersWithoutOrganizationByPageAsync(GetUserWithoutOrganizationInput input);}
创建可查询仓储

通用查询接口过滤条件需要对IQueryable进行拼接,由于Volo.Abp.Identity.IIdentityUserRepository继承自IBasicRepository,我们需要重新编写一个IdentityUser的可查询仓储:QueryableIdentityUserRepository

其实现接口IQueryableIdentityUserRepository的定义如下:

public interface IQueryableIdentityUserRepository : IIdentityUserRepository{    Task> GetOrganizationUnitsQueryableAsync(Guid id, bool includeDetails = false);    Task> GetOrganizationUnitUsersAsync(        Guid id, string keyword, string[] type,        bool includeDetails = false);    Task> 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 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 GetAsync(Guid id)        {            return await _organizationUnitAppService.GetAsync(id);        }        [HttpGet]        [Route("GetCurrentOrganizationUnits")]                public async Task> GetCurrentOrganizationUnitsAsync()        {            return await _organizationUnitAppService.GetCurrentOrganizationUnitsAsync();        }        [HttpGet]        [Route("GetOrganizationUnitUsers")]                public async Task> GetOrganizationUnitUsersAsync(GetOrganizationUnitUsersInput input)        {            return await _organizationUnitAppService.GetOrganizationUnitUsersAsync(input);        }        [HttpGet]        [Route("GetOrganizationUnitUsersByPage")]                public async Task> GetOrganizationUnitUsersByPageAsync(GetOrganizationUnitUsersInput input)        {            return await _organizationUnitAppService.GetOrganizationUnitUsersByPageAsync(input);        }        [HttpGet]        [Route("GetRootOrganizationUnit")]                public async Task GetRootOrganizationUnitAsync(Guid id)        {            return await _organizationUnitAppService.GetRootOrganizationUnitAsync(id);        }        [HttpGet]        [Route("GetRootOrganizationUnits")]                public async Task> GetRootOrganizationUnitsAsync(IEnumerable ids)        {            return await _organizationUnitAppService.GetRootOrganizationUnitsAsync(ids);        }        [HttpGet]        [Route("GetRootOrganizationUnitByDisplayName")]                public async Task GetRootOrganizationUnitByDisplayNameAsync(GetRootOrganizationUnitByDisplayName input)        {            return await _organizationUnitAppService.GetRootOrganizationUnitByDisplayNameAsync(input);        }        [HttpGet]        [Route("GetRootOrganizationUnitsByParent")]                public async Task> GetRootOrganizationUnitsByParentAsync(GetRootOrganizationUnitsByParentInput input)        {            return await _organizationUnitAppService.GetRootOrganizationUnitsByParentAsync(input);        }        [HttpGet]        [Route("GetUsersWithoutOrganization")]                public async Task> GetUsersWithoutOrganizationAsync(GetUserWithoutOrganizationInput input)        {            return await _organizationUnitAppService.GetUsersWithoutOrganizationAsync(input);        }        [HttpGet]        [Route("GetUsersWithoutOrganizationByPage")]                public async Task> GetUsersWithoutOrganizationByPageAsync(GetUserWithoutOrganizationInput input)        {            return await _organizationUnitAppService.GetUsersWithoutOrganizationByPageAsync(input);        }        [HttpGet]        [Route("IsInOrganizationUnit")]                public async Task 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 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 CreateAsync(ModifyRelationInput input)        {            return await _relationAppService.CreateAsync(input);        }        [HttpDelete]        [Route("Delete")]        public async Task DeleteAsync(EntityDto input)        {            await _relationAppService.DeleteAsync(input);        }        [HttpDelete]        [Route("DeleteByUserId")]        public async Task DeleteByUserIdAsync(ModifyRelationInput input)        {            await _relationAppService.DeleteByUserIdAsync(input);        }        [HttpGet]        [Route("GetRelatedFromUsers")]        public async Task> GetRelatedFromUsersAsync(GetRelatedUsersInput input)        {            return await _relationAppService.GetRelatedFromUsersAsync(input);        }        [HttpGet]        [Route("GetRelatedToUsers")]        public async Task> GetRelatedToUsersAsync(GetRelatedUsersInput input)        {            return await _relationAppService.GetRelatedToUsersAsync(input);        }        [HttpGet]        [Route("GetRelatedToUserIds")]        public async Task> GetRelatedToUserIdsAsync(GetRelatedUsersInput input)        {            return await _relationAppService.GetRelatedToUserIdsAsync(input);        }        [HttpGet]        [Route("GetRelatedFromUserIds")]        public async Task> 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模块为用户的查询提供组织架构和用户关系的条件过滤。

关键词:

银行更多»

收藏