예제 #1
0
        public RoleSelectedPermissionModel GetPermission(GetPermissionInput input)
        {
            //选中模块
            List<int> selectedModuleId = new List<int>();

            string[] strSelectedModules = input.SelectedModules.Split(',');
            foreach (var Id in strSelectedModules)
            {
                selectedModuleId.Add(Convert.ToInt32(Id));
            }

            //权限列表
            var model = new RoleSelectedPermissionModel();

            model.HeaderPermissionList = _permissionRepository.GetAll().Where(t => t.IsDeleted == false && t.Enabled == true)
                                                        .OrderBy(t => t.OrderSort)
                                                        .Select(t => new PermissionModel
                                                        {
                                                            PermissionId = t.Id,
                                                            PermissionName = t.Name,
                                                            OrderSort = t.OrderSort
                                                        }).ToList();

            //权限列表 (从选中的菜单获取)
            foreach (var moduleId in selectedModuleId.Distinct())
            {
                var module = _moduleRepository.GetAll().FirstOrDefault(t => t.Id == moduleId);

                var modulePermissionModel = new ModulePermissionModel
                {
                    ModuleId = module.Id,
                    ParentId = module.ParentId,
                    ModuleName = module.Name,
                    Code = module.Code
                };

                //所有权限列表
                foreach (var permission in model.HeaderPermissionList)
                {
                    modulePermissionModel.PermissionDataList.Add(new PermissionModel
                    {
                        PermissionId = permission.PermissionId,
                        PermissionName = permission.PermissionName,
                        OrderSort = permission.OrderSort,
                    });
                }

                //模块包含的按钮
                var modulePermission = _modulePermissionRepository.GetAll().Where(t => t.ModuleId == moduleId && t.IsDeleted == false);
                var selectedModulePermission = _roleModulePermissionRepository.GetAll().Where(t => t.RoleId == input.RoleId && t.ModuleId == moduleId && t.IsDeleted == false);

                if (module.ChildModule.Count > 0 && selectedModulePermission.Count() > 0)
                {
                    modulePermissionModel.Selected = true;
                }

                foreach (var mp in modulePermission)
                {
                    var permission = _permissionRepository.GetAll().FirstOrDefault(t => t.Id == mp.PermissionId);

                    foreach (var p in modulePermissionModel.PermissionDataList)
                    {
                        if (p.PermissionId == permission.Id)
                        {
                            //设置Checkbox可用
                            p.Enabled = true;
                            //设置选中
                            var rmp = _roleModulePermissionRepository.GetAll().FirstOrDefault(t => t.RoleId == input.RoleId && t.ModuleId == moduleId && t.PermissionId == permission.Id && t.IsDeleted == false);
                            if (rmp != null)
                            {
                                //设置父节点选中
                                modulePermissionModel.Selected = true;
                                p.Selected = true;
                            }
                        }
                    }

                }
                model.ModulePermissionDataList.Add(modulePermissionModel);
            }

            //权限按照Code排序
            model.ModulePermissionDataList = model.ModulePermissionDataList.OrderBy(t => t.Code).ToList();
            return model;
        }
예제 #2
0
        public RoleSelectedPermissionModel GetPermission(GetPermissionInput input)
        {
            //选中模块
            List <int> selectedModuleId = new List <int>();

            string[] strSelectedModules = input.SelectedModules.Split(',');
            foreach (var Id in strSelectedModules)
            {
                selectedModuleId.Add(Convert.ToInt32(Id));
            }

            //权限列表
            var model = new RoleSelectedPermissionModel();

            model.HeaderPermissionList = _permissionRepository.GetAll().Where(t => t.IsDeleted == false && t.Enabled == true)
                                         .OrderBy(t => t.OrderSort)
                                         .Select(t => new PermissionModel
            {
                PermissionId   = t.Id,
                PermissionName = t.Name,
                OrderSort      = t.OrderSort
            }).ToList();

            //权限列表 (从选中的菜单获取)
            foreach (var moduleId in selectedModuleId.Distinct())
            {
                var module = _moduleRepository.GetAll().FirstOrDefault(t => t.Id == moduleId);

                var modulePermissionModel = new ModulePermissionModel
                {
                    ModuleId   = module.Id,
                    ParentId   = module.ParentId,
                    ModuleName = module.Name,
                    Code       = module.Code
                };

                //所有权限列表
                foreach (var permission in model.HeaderPermissionList)
                {
                    modulePermissionModel.PermissionDataList.Add(new PermissionModel
                    {
                        PermissionId   = permission.PermissionId,
                        PermissionName = permission.PermissionName,
                        OrderSort      = permission.OrderSort,
                    });
                }

                //模块包含的按钮
                var modulePermission         = _modulePermissionRepository.GetAll().Where(t => t.ModuleId == moduleId && t.IsDeleted == false);
                var selectedModulePermission = _roleModulePermissionRepository.GetAll().Where(t => t.RoleId == input.RoleId && t.ModuleId == moduleId && t.IsDeleted == false);

                if (module.ChildModule.Count > 0 && selectedModulePermission.Count() > 0)
                {
                    modulePermissionModel.Selected = true;
                }

                foreach (var mp in modulePermission)
                {
                    var permission = _permissionRepository.GetAll().FirstOrDefault(t => t.Id == mp.PermissionId);

                    foreach (var p in modulePermissionModel.PermissionDataList)
                    {
                        if (p.PermissionId == permission.Id)
                        {
                            //设置Checkbox可用
                            p.Enabled = true;
                            //设置选中
                            var rmp = _roleModulePermissionRepository.GetAll().FirstOrDefault(t => t.RoleId == input.RoleId && t.ModuleId == moduleId && t.PermissionId == permission.Id && t.IsDeleted == false);
                            if (rmp != null)
                            {
                                //设置父节点选中
                                modulePermissionModel.Selected = true;
                                p.Selected = true;
                            }
                        }
                    }
                }
                model.ModulePermissionDataList.Add(modulePermissionModel);
            }

            //权限按照Code排序
            model.ModulePermissionDataList = model.ModulePermissionDataList.OrderBy(t => t.Code).ToList();
            return(model);
        }