/// <summary> /// 取得角色概要 /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页面大小</param> /// <param name="searchText">搜索内容</param> public PagedData <RoleOutline> GetRole(int pageIndex, int pageSize, string searchText) { // 生成表达式组 var tupleList = new List <(string, object, ExpressionType)> { ("Name", searchText, ExpressionType.StringContain), ("Describe", searchText, ExpressionType.StringContain) }; // 表达式组 var group = new ExpressionGroup <BaseIdentityRole>( tupleList: tupleList, expressionCombineType: ExpressionCombineType.Or, expressionGroupsList: null); var filter = group.GetLambda(); // 分页取得用户 var skip = pageSize * (pageIndex - 1); var roles = _roleManager.Roles.Where(filter).Skip(skip).Take(pageSize); // 生成结果 return(new PagedData <RoleOutline> { Total = _roleManager.Roles.Where(filter).Count(), Data = _mapper.Map <List <RoleOutline> >(roles) }); }
/// <summary> /// 取得规则组的表达式 /// </summary> /// <param name="ruleGroupId">规则组ID</param> /// <returns></returns> private string GetExpressionStr(Guid ruleGroupId, string userName, string groupId) { // 规则信息 var ruleGroup = _context.Set <RuleGroup>().Find(ruleGroupId); var rules = _context.Set <Rule>().AsNoTracking().Where(rule => rule.RuleGroupId == ruleGroupId); var ruleConditions = _context.Set <RuleCondition>().AsNoTracking().Where(condition => condition.RuleGroupId == ruleGroupId); // 生成表达式 var topRule = rules.Where(rule => rule.UpRuleId.Equals(new Guid())).FirstOrDefault(); var entityType = EntityTypeFinder.FindEntityType(ruleGroup.DbContext, ruleGroup.Entity); var expressionGroup = new ExpressionGroup(entityType); var keyValuePairs = new Dictionary <string, string> { }; keyValuePairs.Add("UserName", userName); keyValuePairs.Add("GroupId", groupId); MakeExpressionGroup(topRule, rules, ruleConditions, entityType, keyValuePairs, ref expressionGroup); // 生成过滤表达式 Expression lambda = expressionGroup.GetLambda(); // 表达式序列化 var serializer = new ExpressionSerializer(new JsonSerializer()) { AutoAddKnownTypesAsListTypes = true }; serializer.AddKnownType(typeof(Core.Expressions.ExpressionType)); return(serializer.SerializeText(lambda)); }
/// <summary> /// 取得用户概要数据 /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页面大小</param> /// <returns>用户数据</returns> public async Task <PagedData <UserOutline> > GetUserOutlinesAsync(int pageIndex, int pageSize, string searchText) { // 生成表达式组 var tupleList = new List <(string, object, ExpressionType)> { ("UserName", searchText, ExpressionType.StringContain), ("PhoneNumber", searchText, ExpressionType.StringContain), ("Email", searchText, ExpressionType.StringContain), }; // 表达式组 var group = new ExpressionGroup <BaseIdentityUser>( tupleList: tupleList, expressionCombineType: ExpressionCombineType.Or, expressionGroupsList: null); var filter = group.GetLambda(); // 分页取得用户 var skip = pageSize * (pageIndex - 1); var users = _userManager.Users.Where(filter).Skip(skip).Take(pageSize).ToList(); // 结果数据 var userOutlines = _mapper.Map <List <UserOutline> >(users); // 设定组织名称 var groups = _groupRepository.GetAllNoTracking().ToList(); foreach (var user in users) { if (!string.IsNullOrEmpty(user.UserGroupId)) { userOutlines.FirstOrDefault(u => u.UserName == user.UserName).GroupName = groups.FirstOrDefault(g => g.Id == Guid.Parse(user.UserGroupId))?.GroupName; } userOutlines.FirstOrDefault(u => u.UserName == user.UserName).Roles = (await _userManager.GetRolesAsync(user)).ToArray(); } // 生成结果 return(new PagedData <UserOutline> { Total = _userManager.Users.Where(filter).Count(), Data = userOutlines }); }
/// <summary> /// 取得用户概要数据 /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页面大小</param> /// <returns>用户数据</returns> public async Task <PagedData <UserOutline> > GetUserOutlinesAsync(int pageIndex, int pageSize, string searchText) { // 生成表达式组 var tupleList = new List <(string, object, ExpressionType)> { ("UserName", searchText, ExpressionType.StringContain), ("PhoneNumber", searchText, ExpressionType.StringContain), ("Email", searchText, ExpressionType.StringContain), }; // 表达式组 var group = new ExpressionGroup <BaseIdentityUser>( tupleList: tupleList, expressionCombineType: ExpressionCombineType.Or, expressionGroupsList: null); var filter = group.GetLambda(); // 分页取得用户 var skip = pageSize * (pageIndex - 1); var users = await _userManager.Users.Where(filter).OrderByDescending(u => u.CreateTime).Skip(skip).Take(pageSize).ToListAsync(); users.ForEach(user => { user.GroupName = string.IsNullOrEmpty(user.UserGroupId) ? string.Empty : _groupService.GetGroupNameById(Guid.Parse(user.UserGroupId)); user.PositionName = string.IsNullOrEmpty(user.PositionId) ? string.Empty : _positionService.GetPositionNameById(Guid.Parse(user.PositionId)); }); // 结果数据 var userOutlines = _mapper.Map <List <UserOutline> >(users); // 生成结果 return(new PagedData <UserOutline> { Total = _userManager.Users.Where(filter).Count(), Data = userOutlines }); }
/// <summary> /// 修改规则组 /// </summary> /// <param name="rules">规则</param> /// <param name="ruleConditions">条件</param> /// <param name="ruleGroup">规则组</param> public async Task <bool> AddOrUpdateRuleAsync(IEnumerable <RuleEntityResult> rules, IEnumerable <RuleConditionResult> ruleConditions, RuleGroup ruleGroup) { // 因为有动态参数的存在,表达式暂时不持久化到数据库,暂时删除group的lambda字段 //// 生成表达式保存到数据库 //// 获取实体类型 var topRule = rules.Where(rule => string.IsNullOrEmpty(rule.UpRuleId)).FirstOrDefault(); var entityType = EntityTypeFinder.FindEntityType(ruleGroup.DbContext, ruleGroup.Entity); var expressionGroup = new ExpressionGroup(entityType); var keyValuePairs = new Dictionary <string, string> { }; keyValuePairs.Add("UserName", _httpContextAccessor.HttpContext.User.GetClaimValue(CustomClaimTypes.UserName)); keyValuePairs.Add("GroupId", _httpContextAccessor.HttpContext.User.GetClaimValue(CustomClaimTypes.Group)); MakeExpressionGroup(topRule, rules, ruleConditions, entityType, keyValuePairs, ref expressionGroup); // 用当前用户数据检查表达式是否正确 try { expressionGroup.GetLambda(); } catch { return(false); } // 开始更新数据 var group = _ruleGroupRepository.GetByWhereNoTracking(rg => rg.Id == ruleGroup.Id).FirstOrDefault(); // 已经存在时先删除再插入 if (group != null) { // 先删除再插入 _ruleRepository.DeleteRange(r => r.RuleGroupId == group.Id); _ruleConditionRepository.DeleteRange(r => r.RuleGroupId == group.Id); } // 修改规则组Id var groupId = group != null ? group.Id : GuidUtil.NewSquentialGuid(); ruleGroup.Id = groupId; // 修改每项规则的Id和Upid foreach (var rule in rules) { var oldId = rule.Id; // 替换前端生成的ID var ruleId = GuidUtil.NewSquentialGuid().ToString(); rule.Id = ruleId; rule.RuleGroupId = groupId.ToString(); foreach (var r in rules) { if (r.UpRuleId == oldId) { r.UpRuleId = ruleId; } } ; foreach (var c in ruleConditions) { if (c.RuleId == oldId) { c.RuleId = ruleId; c.RuleGroupId = groupId.ToString(); } } ; } // 生成新的Id ruleConditions.ToList().ForEach(condition => condition.Id = GuidUtil.NewSquentialGuid().ToString()); // 保存全部数据 await _ruleRepository.InsertRangeAsync(_mapper.Map <IEnumerable <RuleEntity> >(rules)); await _ruleConditionRepository.InsertRangeAsync(_mapper.Map <IEnumerable <RuleCondition> >(ruleConditions)); // 更新或添加规则组 if (group == null) { await _ruleGroupRepository.InsertAsync(ruleGroup); } else { _ruleGroupRepository.Update(ruleGroup); } // 提交事务 await _unitOfWork.CommitAsync(); return(true); }