Пример #1
0
        /// <summary>
        /// 设置指定数据权限的缓存
        /// </summary>
        /// <param name="item">数据权限缓存项</param>
        public void SetCache(DataAuthCacheItem item)
        {
            string key = $"Security_EntityRole_{item.RoleName}_{item.EntityTypeFullName}";

            _cache.Set(key, item.FilterGroup);
            _logger.LogDebug($"创建角色“{item.RoleName}”和实体“{item.EntityTypeFullName}”的数据权限规则缓存");
        }
Пример #2
0
        /// <summary>
        /// 设置指定数据权限的缓存
        /// </summary>
        /// <param name="item">数据权限缓存项</param>
        public virtual void SetCache(DataAuthCacheItem item)
        {
            string key  = GetKey(item.RoleName, item.EntityTypeFullName, item.Operation);
            string name = GetName(item.RoleName, item.EntityTypeFullName, item.Operation);

            _cache.Set(key, item.FilterGroup);
            _logger.LogDebug($"创建{name}的数据权限规则缓存");
        }
Пример #3
0
        /// <summary>
        /// 移除指定角色名与实体类型的缓存项
        /// </summary>
        /// <param name="item">要移除的数据权限缓存项信息</param>
        public virtual void RemoveCache(DataAuthCacheItem item)
        {
            string key  = GetKey(item.RoleName, item.EntityTypeFullName, item.Operation);
            string name = GetName(item.RoleName, item.EntityTypeFullName, item.Operation);

            _cache.Remove(key);
            _logger.LogDebug($"移除{name}的数据权限规则缓存");
        }
Пример #4
0
        /// <summary>
        /// 更新实体角色信息
        /// </summary>
        /// <param name="dtos">包含更新信息的实体角色信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> UpdateEntityRoles(params TEntityRoleInputDto[] dtos)
        {
            Check.Validate <TEntityRoleInputDto, Guid>(dtos, nameof(dtos));

            DataAuthCacheRefreshEventData eventData = new DataAuthCacheRefreshEventData();
            OperationResult result = await _entityRoleRepository.UpdateAsync(dtos,
                                                                             async (dto, entity) =>
            {
                TRole role = await _roleRepository.GetAsync(dto.RoleId);
                if (role == null)
                {
                    throw new OsharpException($"编号为“{dto.RoleId}”的角色信息不存在");
                }
                TEntityInfo entityInfo = await _entityInfoRepository.GetAsync(dto.EntityId);
                if (entityInfo == null)
                {
                    throw new OsharpException($"编号为“{dto.EntityId}”的数据实体信息不存在");
                }
                if (await CheckEntityRoleExists(m => m.RoleId.Equals(dto.RoleId) && m.EntityId == dto.EntityId && m.Operation == dto.Operation, dto.Id))
                {
                    throw new OsharpException($"角色“{role.Name}”和实体“{entityInfo.Name}”和操作“{dto.Operation}”的数据权限规则已存在,不能重复添加");
                }
                OperationResult checkResult = CheckFilterGroup(dto.FilterGroup, entityInfo);
                if (!checkResult.Succeeded)
                {
                    throw new OsharpException($"数据规则验证失败:{checkResult.Message}");
                }
                DataAuthCacheItem cacheItem = new DataAuthCacheItem()
                {
                    RoleName           = role.Name,
                    EntityTypeFullName = entityInfo.TypeName,
                    Operation          = dto.Operation,
                    FilterGroup        = dto.FilterGroup
                };
                if (dto.IsLocked)
                {
                    eventData.RemoveItems.Add(cacheItem);
                }
                else
                {
                    eventData.SetItems.Add(cacheItem);
                }
            });

            if (result.Succeeded && eventData.HasData())
            {
                _eventBus.Publish(eventData);
            }
            return(result);
        }