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

news/2024/11/24 3:00:09/

文章目录

    • 用户关系管理
    • 扩展组织管理功能
      • 创建可查询仓储
    • 实现控制器
    • 测试接口

身份管理模块(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模块为用户的查询提供组织架构和用户关系的条件过滤。


http://www.ppmy.cn/news/779591.html

相关文章

【Java】Date类型获取年月日时分秒的两种方法(12小时制、24小时制)

Java的Date类型是&#xff0c;提供用来描述日期时间的类&#xff0c;它可以存储时间的年月日、时分秒的信息。但是如何从Date的实例中获取这些信息呢&#xff1f; 以前Date提供了一系列的get方法来获取&#xff0c;但是这些方法现在都被弃用了&#xff1a; 既然这些方法不能使…

java 毫秒转分钟和秒_毫秒转换为天、小时、分、秒

将毫秒数或两个日期类型数转换为*天*小时*分*秒的方法&#xff0c;在进行时间段计算时应该经常用到。 记得有一道ACM题就是从一个计时方法A转换为另一个计时方法B&#xff0c;思路如下&#xff1a;总时间不会变&#xff0c;1s就是1s&#xff0c;只不过小时、分钟、天等包含的秒…

C#基础-编程思想实现107653秒是几天几小时几分钟几秒

编程思想实现107653秒是几天几小时几分钟几秒&#xff1f; 第一种方法&#xff1a; int x 107653; int day x / (24 * 60 * 60);//获取天数 int hour x % (24 * 60 * 60) / 3600;//天数之后还可能剩余秒数,在模小时 int min x % (24 * 60 * 60) % 3600 / 60;//小时之后还可…

js将时间秒转换成天小时分钟秒的字符串

js将时间秒转换成天小时分钟秒的字符串 场景代码 场景 有的时候&#xff0c;后台会返回 毫秒 或者 秒 的时间&#xff0c;前端需要转换为 “xx天xx小时xx分钟” 的格式来显示。 代码 function getFormatDuration(duration) {let time parseInt(duration);let minute 0;// 分…

java格式化日期24小时_Java如何格式化24小时格式的时间?

在Java中&#xff0c;如何格式化24小时格式的时间&#xff1f;&#xff1f; 此示例使用SimpleDateFormat类的sdf.format(date)方法将时间格式化为24小时格式(00:00-24:00)。 package com.yiibai; import java.text.SimpleDateFormat; import java.util.*; public class FormatT…

java计算两个日期间相差多少天多少小时多少分多少秒

1、参数为日期类型参数 /** * @Description: TODO(计算两个日期【日期类型】之间的时间距离) * @param @param sdate * @param @param bdate * @param @return 设定文件 * @throws */ public static Map<String,Long> timesBetween(Date sdate,Date bdate) { Dat…

python输入秒数输出分钟小时_Python函数将秒到分钟,小时,天问题,怎么解决

慕盖茨4494581 为了美化日志输出程序执行的总时间,同时人们能够快速获取所需要的信息,需要把输出的秒数转换成 228 days, 22 hour, 9 min,39.0 sec 这样的格式。因为考虑到判断的重复型,这个函数运用递归的思维方式编写的。[python] view plain copy#coding:utf8 import t…

Java工具类 计算某个时间距离当前时间相差多少天、多少小时、多少分、多少秒

/*** 计算传入时间距离当前时间多久** param date* return*/ public static String getTimeDiff(String date) {if (ObjectUtils.isEmpty(date)) {return "";}StringBuilder sb new StringBuilder();try {Date parse mDateFormat.parse(date);Date now new Date()…