示例#1
0
        /// <summary>
        /// 添加模块信息信息
        /// </summary>
        /// <param name="dto">要添加的模块信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> CreateModule(TModuleInputDto dto)
        {
            dto.CheckNotNull("dto");
            if (await ModuleRepository.CheckExistsAsync(m => m.Name == dto.Name))
            {
                return(new OperationResult(OperationResultType.Error, "名称为“{0}”的模块已存在,不能重复添加".FormatWith(dto.Name)));
            }
            TModule module = dto.MapTo <TModule>();

            if (dto.ParentId.HasValue)
            {
                TModule parent = await ModuleRepository.GetByKeyAsync(dto.ParentId.Value);

                if (parent == null)
                {
                    return(new OperationResult(OperationResultType.Error, "编号为“{0}”的父模块信息不存在".FormatWith(dto.ParentId.Value)));
                }
                module.Parent = parent;
            }
            else
            {
                module.Parent = default(TModule);
            }
            module.TreePathString = module.GetTreePath();
            await ModuleRepository.InsertAsync(module);

            return(OperationResult.Success);
        }
示例#2
0
        /// <summary>
        /// 添加模块信息信息
        /// </summary>
        /// <param name="dto">要添加的模块信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> CreateModule(TModuleInputDto dto)
        {
            dto.CheckNotNull("dto");
            var exist = ModuleRepository.Entities.Where(m => m.Name == dto.Name && m.Parent != null && m.Parent.Id.Equals(dto.ParentId))
                        .Select(m => new { ParentName = m.Parent.Name }).FirstOrDefault();

            if (exist != null)
            {
                return(new OperationResult(OperationResultType.Error, "模块“{0}”中已存在名称为“{1}”的子模块,不能重复添加".FormatWith(exist.ParentName, dto.Name)));
            }

            TModule entity = dto.MapTo <TModule>();
            //排序码,不存在为1,否则同级最大+1
            var peerModules = ModuleRepository.Entities.Where(m => m.Parent.Id.Equals(dto.ParentId)).Select(m => new { m.OrderCode }).ToArray();

            if (peerModules.Length == 0)
            {
                entity.OrderCode = 1;
            }
            else
            {
                double maxCode = peerModules.Max(m => m.OrderCode);
                entity.OrderCode = maxCode + 1;
            }
            //父模块
            if (!dto.ParentId.Equals(default(TModuleKey)))
            {
                TModule parent = await ModuleRepository.GetByKeyAsync(dto.ParentId);

                if (parent == null)
                {
                    return(new OperationResult(OperationResultType.Error, "编号为“{0}”的父模块信息不存在".FormatWith(dto.ParentId)));
                }
                entity.Parent = parent;
            }
            else
            {
                entity.Parent = default(TModule);
            }
            ModuleRepository.UnitOfWork.BeginTransaction();
            int count = await ModuleRepository.InsertAsync(entity);

            entity.TreePathString = entity.GetTreePath();
            await ModuleRepository.UpdateAsync(entity);

            return(count > 0
                ? new OperationResult(OperationResultType.Success, "模块“{0}”创建成功".FormatWith(dto.Name))
                : OperationResult.NoChanged);
        }
示例#3
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);
        }
示例#4
0
        /// <summary>
        /// 删除模块信息信息
        /// </summary>
        /// <param name="id">要删除的模块信息编号</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> DeleteModule(TModuleKey id)
        {
            TModule module = await ModuleRepository.GetByKeyAsync(id);

            if (module == null)
            {
                return(OperationResult.Success);
            }
            if (module.SubModules.Count > 0)
            {
                return(new OperationResult(OperationResultType.Error, "模块“{0}”的子模块不为空,不能删除".FormatWith(module.Name)));
            }
            module.Functions.Clear();
            await ModuleRepository.DeleteAsync(module);

            return(OperationResult.Success);
        }
示例#5
0
        /// <summary>
        /// 删除模块信息信息
        /// </summary>
        /// <param name="id">要删除的模块信息编号</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> DeleteModule(TModuleKey id)
        {
            TModule entity = await ModuleRepository.GetByKeyAsync(id);

            if (entity == null)
            {
                return(OperationResult.Success);
            }
            if (entity.SubModules.Count > 0)
            {
                return(new OperationResult(OperationResultType.Error, "模块“{0}”的子模块不为空,不能删除".FormatWith(entity.Name)));
            }
            entity.Functions.Clear();
            return(await ModuleRepository.DeleteAsync(entity) > 0
                ? new OperationResult(OperationResultType.Success, "模块“{0}”删除成功".FormatWith(entity.Name))
                : OperationResult.NoChanged);
        }
示例#6
0
        /// <summary>
        /// 给用户添加模块权限
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <param name="moduleIds">要添加的模块编号集合</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> SetUserModules(TUser user, params TModuleKey[] moduleIds)
        {
            user.CheckNotNull("user");

            TModuleKey[]  existModuleIds = ModuleRepository.Entities.Where(m => m.Users.Select(n => n.Id).Contains(user.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>();

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

                if (module == null)
                {
                    return(new OperationResult(OperationResultType.QueryNull, "编号为“{0}”的模块信息不存在".FormatWith(moduleId)));
                }
                module.Users.Add(user);
                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.Users.Remove(user);
                removeNames.Add(module.Name);
            }
            //todo:确认更新 user 是否有效
            int count = await UserRepository.UpdateAsync(user);

            if (count > 0)
            {
                //移除 当前用户的功能权限缓存,使其更新
                FunctionAuthCache.RemoveUserCaches(new[] { user.UserName });

                return(new OperationResult(OperationResultType.Success,
                                           "用户“{0}”添加模块“{1}”,移除模块“{2}”操作成功".FormatWith(user.UserName, addNames.ExpandAndToString(), removeNames.ExpandAndToString())));
            }
            return(OperationResult.NoChanged);
        }
示例#7
0
        /// <summary>
        /// 给用户添加模块权限
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <param name="moduleIds">要添加的模块编号集合</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> SetUserModules(TUser user, params TModuleKey[] moduleIds)
        {
            user.CheckNotNull("user");
            moduleIds.CheckNotNullOrEmpty("moduleIds");

            TModuleKey[]  existModuleIds = ModuleRepository.Entities.Where(m => m.Users.Select(n => n.Id).Contains(user.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>();

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

                if (module == null)
                {
                    return(new OperationResult(OperationResultType.QueryNull, "编号为“{0}”的模块信息不存在".FormatWith(moduleId)));
                }
                module.Users.Add(user);
                await ModuleRepository.UpdateAsync(module);

                addNames.Add(module.Name);
            }
            foreach (TModuleKey moduleId in removeModuleIds)
            {
                TModule module = await ModuleRepository.GetByKeyAsync(moduleId);

                if (module == null)
                {
                    return(new OperationResult(OperationResultType.QueryNull, "编号为“{}”的模块信息不存在".FormatWith(moduleId)));
                }
                module.Users.Remove(user);
                await ModuleRepository.UpdateAsync(module);

                removeNames.Add(module.Name);
            }

            return(await ModuleRepository.UnitOfWork.SaveChangesAsync() > 0
                ? new OperationResult(OperationResultType.Success,
                                      "用户“{0}”添加模块“{1}”,移除模块“{2}”操作成功".FormatWith(user.UserName, addNames.ExpandAndToString(), removeNames.ExpandAndToString()))
                : OperationResult.NoChanged);
        }
示例#8
0
        /// <summary>
        /// 更新模块信息信息
        /// </summary>
        /// <param name="dto">包含更新信息的模块信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public virtual async Task <OperationResult> UpdateModule(TModuleInputDto dto)
        {
            dto.CheckNotNull("dto");
            var exist = ModuleRepository.Entities.Where(m => m.Name == dto.Name && m.Parent != null && m.Parent.Id.Equals(dto.ParentId))
                        .Select(m => new { m.Id, ParentName = m.Parent.Name }).FirstOrDefault();

            if (exist != null && !exist.Id.Equals(dto.Id))
            {
                return(new OperationResult(OperationResultType.Error, "模块“{0}”中已存在名称为“{1}”的子模块,不能重复添加".FormatWith(exist.ParentName, dto.Name)));
            }
            TModule entity = await ModuleRepository.GetByKeyAsync(dto.Id);

            if (entity == null)
            {
                return(new OperationResult(OperationResultType.Error, "编号为“{0}”的模块信息不存在".FormatWith(dto.Id)));
            }
            entity = dto.MapTo(entity);
            if (!dto.ParentId.Equals(default(TModuleKey)))
            {
                if (entity.Parent == null || !entity.Parent.Id.Equals(dto.ParentId))
                {
                    TModule parent = await ModuleRepository.GetByKeyAsync(dto.ParentId);

                    if (parent == null)
                    {
                        return(new OperationResult(OperationResultType.Error, "编号为“{0}”的父模块信息不存在".FormatWith(dto.ParentId)));
                    }
                    entity.Parent = parent;
                }
            }
            else
            {
                entity.Parent = default(TModule);
            }
            entity.TreePathString = entity.GetTreePath();
            return(await ModuleRepository.UpdateAsync(entity) > 0
                ? new OperationResult(OperationResultType.Success, "模块“{0}”更新成功".FormatWith(dto.Name))
                : OperationResult.NoChanged);
        }