Beispiel #1
0
        /// <summary>
        /// 移除用户授权
        /// </summary>
        /// <param name="userId">用户编号</param>
        public static void RemoveUserAuthorize(long userId)
        {
            var cacheKey = CacheUtil.GetUserAuthOperationCacheKey(userId.ToString());

            CacheManager.Keys.Delete(new DeleteOption()
            {
                Keys = new List <CacheKey>()
                {
                    cacheKey
                }
            });
        }
Beispiel #2
0
        /// <summary>
        /// 授权验证
        /// </summary>
        /// <param name="operation">授权操作</param>
        /// <returns></returns>
        public static async Task <bool> AuthorizationAsync(AuthenticationUser <long> user, AuthorityOperationCmdDto operation)
        {
            if (operation == null || user == null)
            {
                return(false);
            }
            if (user.IsAdmin)
            {
                return(true);
            }

            operation.ControllerCode = operation.ControllerCode?.ToUpper() ?? string.Empty;
            operation.ActionCode     = operation.ActionCode?.ToUpper() ?? string.Empty;

            #region 授权操作判断

            string operationValue    = $"{operation.ControllerCode}/{operation.ActionCode}";
            var    operationCacheKey = CacheUtil.GetOperationCacheKey(operationValue);
            var    nowOperation      = CacheManager.GetData <AuthorityOperationDto>(operationCacheKey);
            if (nowOperation == null || nowOperation.Status == AuthorityOperationStatus.关闭)
            {
                return(false);
            }
            if (nowOperation.AuthorizeType == AuthorityOperationAuthorizeType.无限制)
            {
                return(true);
            }

            #endregion

            #region 授权操作分组判断

            var groupKey = nowOperation.Group?.SysNo.ToString() ?? string.Empty;
            if (groupKey.IsNullOrEmpty())
            {
                return(false);
            }
            var groupCacheKey = CacheUtil.GetOperationGroupCacheKey(groupKey);
            var nowGroup      = CacheManager.GetData <AuthorityOperationGroupDto>(groupCacheKey);
            if (nowGroup == null || nowGroup.Status == AuthorityOperationGroupStatus.关闭)
            {
                return(false);
            }
            while (nowGroup.Level > 1)
            {
                var parentGroupKey = nowGroup.Parent?.SysNo.ToString() ?? string.Empty;
                if (parentGroupKey.IsNullOrEmpty())
                {
                    return(false);
                }
                var parentGroupCacheKey = CacheUtil.GetOperationGroupCacheKey(parentGroupKey);
                var nowParentGroup      = CacheManager.GetData <AuthorityOperationGroupDto>(parentGroupCacheKey);
                nowGroup = nowParentGroup;
                if (nowGroup == null || nowGroup.Status == AuthorityOperationGroupStatus.关闭)
                {
                    return(false);
                }
            }

            #endregion

            var cacheKey    = CacheUtil.GetUserAuthOperationCacheKey(user.Id.ToString());
            var existResult = CacheManager.Set.Contains(new SetContainsOption()
            {
                Key   = cacheKey,
                Value = operationValue
            })?.Responses ?? new List <SetContainsResponse>(0);
            var hasOperation = existResult.IsNullOrEmpty() ? false : (existResult.FirstOrDefault()?.ContainsValue ?? false);
            if (!hasOperation)
            {
                return(false);
            }
            return(await Task.FromResult(true));
        }
        /// <summary>
        /// 刷新用户登录信息
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="superAdmin">超级用户</param>
        public static void RefreshLoginUser(long userId, bool superAdmin = false)
        {
            if (userId < 1 || superAdmin)
            {
                return;
            }

            #region 判断登录信息

            var userCacheKey = CacheUtil.GetUserCacheKey(userId.ToString());
            var userData     = CacheManager.GetData <UserDto>(userCacheKey);
            if (userData == null)
            {
                CacheManager.Set.Remove(new SetRemoveOption()
                {
                    Key          = CacheUtil.AllLoginUserCacheKey,
                    RemoveValues = new List <string>(1)
                    {
                        userId.ToString()
                    }
                });
                return;
            }

            #endregion

            #region 刷新授权信息

            UserOperationFilterDto operationFilter = new UserOperationFilterDto()
            {
                UserFilter = new UserFilterDto()
                {
                    SysNos = new List <long>(1)
                    {
                        userId
                    }
                }
            };
            var operations = AuthAppService.GetAuthorityOperationList(operationFilter);
            if (operations.IsNullOrEmpty())
            {
                return;
            }
            CacheKey userAuthKey = CacheUtil.GetUserAuthOperationCacheKey(userId.ToString());
            CacheManager.Keys.Delete(new DeleteOption()
            {
                Keys = new List <CacheKey>()
                {
                    userAuthKey
                }
            });
            operations.ForEach(c =>
            {
                CacheManager.Set.Add(new SetAddOption()
                {
                    Key   = userAuthKey,
                    Value = $"{c.ControllerCode}/{c.ActionCode}"
                });
            });

            #endregion
        }