/// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        public override void Handle(FunctionAuthCacheRefreshEventData eventData)
        {
            if (!_provider.InHttpRequest())
            {
                return;
            }
            IFunctionAuthCache cache = _provider.GetService <IFunctionAuthCache>();

            if (eventData.FunctionIds.Length > 0)
            {
                cache.RemoveFunctionCaches(eventData.FunctionIds);
                foreach (Guid functionId in eventData.FunctionIds)
                {
                    cache.GetFunctionRoles(functionId);
                }
            }
            if (eventData.UserNames.Length > 0)
            {
                cache.RemoveUserCaches(eventData.UserNames);
                foreach (string userName in eventData.UserNames)
                {
                    cache.GetUserFunctions(userName);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 给角色添加模块权限
        /// </summary>
        /// <param name="role">角色信息</param>
        /// <param name="moduleIds">要添加的模块编号集合</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> SetRoleModules(TRole role, params TModuleKey[] moduleIds)
        {
            role.CheckNotNull("role");

            TModuleKey[]  existModuleIds = ModuleRepository.Entities.Where(m => m.Roles.Select(n => n.Id).Contains(role.Id)).Select(m => m.Id).ToArray();
            TModuleKey[]  addModuleIds = moduleIds.Except(existModuleIds).ToArray();
            TModuleKey[]  removeModuleIds = existModuleIds.Except(moduleIds).ToArray();
            List <string> addNames = new List <string>(), removeNames = new List <string>();

            foreach (TModuleKey moduleId in addModuleIds)
            {
                TModule module = await ModuleRepository.GetByKeyAsync(moduleId);

                if (module == null)
                {
                    return(new OperationResult(OperationResultType.QueryNull, "编号为“{0}”的模块信息不存在".FormatWith(moduleId)));
                }
                module.Roles.Add(role);
                addNames.Add(module.Name);
            }
            foreach (TModuleKey moduleId in removeModuleIds)
            {
                TModule module = await ModuleRepository.GetByKeyAsync(moduleId);

                if (module == null)
                {
                    return(new OperationResult(OperationResultType.QueryNull, "编号为“{0}”的模块信息不存在".FormatWith(moduleId)));
                }
                module.Roles.Remove(role);
                removeNames.Add(module.Name);
            }

            //todo:确认更新 role 是否有效
            int count = await RoleRepository.UpdateAsync(role);

            if (count > 0)
            {
                //移除 变更模块涉及到的功能权限缓存,使其更新
                TModuleKey[]   relateModuleIds = addModuleIds.Union(removeModuleIds).Distinct().ToArray();
                TFunctionKey[] functionIds     = ModuleRepository.Entities.Where(m => relateModuleIds.Contains(m.Id))
                                                 .SelectMany(m => m.Functions.Select(n => n.Id)).Distinct().ToArray();
                FunctionAuthCache.RemoveFunctionCaches(functionIds);

                return(new OperationResult(OperationResultType.Success,
                                           "角色“{0}”添加模块“{1}”,移除模块“{2}”操作成功".FormatWith(role.Name, addNames.ExpandAndToString(), removeNames.ExpandAndToString())));
            }
            return(OperationResult.NoChanged);
        }