/// <summary>
        /// 获取权限信息
        /// </summary>
        /// <param name="page">页</param>
        /// <param name="limit">行数</param>
        /// <returns></returns>
        public async Task <ActionResult> GetPermissionInfo(int page, int limit)
        {
            JsonResponse result = new JsonResponse();

            try
            {
                var permissions = await _permissionServices.Query(c => !c.IsDeleted);

                foreach (var item in permissions)
                {
                    var permission = await _permissionServices.GetSingle(c => c.Id == item.ParentId);

                    if (permission != null)
                    {
                        item.ParentName = permission.Name;
                    }
                }
                result.code  = ResponseCode.Success;
                result.data  = permissions.ToList().Skip((page - 1) * limit).Take(limit).ToList();
                result.count = permissions.Count;
            }
            catch (Exception ex)
            {
                result.code  = ResponseCode.Fail;
                result.msg   = ex.ToString();
                result.count = 0;
                await _logServices.WriteExceptionLog(LoginUser.Id, "获取权限菜单", ex.ToString());
            }
            return(Json(result));
        }
 public async Task<ApiResponseModel<List<Permission>>> GetTreeTable(int f = 0, string key = "")
 {
     var apiList = await _moduleServices.Query(d => d.IsDeleted == false);
     //所有菜单
     var permissionsList = await _permissionServices.Query(d => d.IsDeleted == false);
     //查找所有父级菜单
     var permissions = permissionsList.Where(a => a.Pid == f).OrderBy(a => a.OrderSort).ToList();
     foreach (var item in permissions)
     {
         item.MName = apiList.FirstOrDefault(d => d.Id == item.Mid)?.LinkUrl;
         item.hasChildren = permissionsList.Where(d => d.Pid == item.Id).Any();
     }
     return ApiResponse.Success(permissions);
 }
Beispiel #3
0
        public async Task <MessageModel <string> > Assign([FromBody] AssignView assignView)
        {
            var data = new MessageModel <string>();

            try
            {
                if (assignView.rid > 0)
                {
                    data.success = true;

                    var roleModulePermissions = await _roleModulePermissionServices.Query(d => d.RoleId == assignView.rid);

                    var remove = roleModulePermissions.Where(d => !assignView.pids.Contains(d.PermissionId.ObjToInt())).Select(c => (object)c.Id);
                    data.success |= await _roleModulePermissionServices.DeleteByIds(remove.ToArray());

                    foreach (var item in assignView.pids)
                    {
                        var rmpitem = roleModulePermissions.Where(d => d.PermissionId == item);
                        if (!rmpitem.Any())
                        {
                            var moduleid = (await _permissionServices.Query(p => p.Id == item)).FirstOrDefault()?.Mid;
                            RoleModulePermission roleModulePermission = new RoleModulePermission()
                            {
                                IsDeleted    = false,
                                RoleId       = assignView.rid,
                                ModuleId     = moduleid.ObjToInt(),
                                PermissionId = item,
                            };

                            data.success |= (await _roleModulePermissionServices.Add(roleModulePermission)) > 0;
                        }
                    }

                    if (data.success)
                    {
                        data.response = "";
                        data.msg      = "保存成功";
                    }
                }
            }
            catch (Exception)
            {
                data.success = false;
            }

            return(data);
        }
        public BaseResponse GetNavigationBar(int uid)
        {
            if (uid > 0)
            {
                var roleId = _userRoleServices.Get(d => d.IsDeleted == false && d.UserId == uid)?.RoleId;
                if (roleId > 0)
                {
                    var pids = _roleModulePermissionServices.Query(d => d.IsDeleted == false && d.RoleId == roleId).Select(d => d.PermissionId).Distinct();
                    if (pids.Any())
                    {
                        var rolePermissionMoudles = _permissionServices.Query(d => pids.Contains(d.Id) && d.IsButton == false).OrderBy(c => c.OrderSort);
                        var permissionTrees       = (from child in rolePermissionMoudles
                                                     where child.IsDeleted == false
                                                     orderby child.Id
                                                     select new NavigationBar
                        {
                            id = child.Id,
                            name = child.Name,
                            pid = (int)child.Pid,
                            order = child.OrderSort,
                            path = child.Code,
                            iconCls = child.Icon,
                            key = child.Key,
                            component = child.Component,
                            isbutton = child.IsButton,
                            meta = new NavigationBarMeta
                            {
                                requireAuth = true,
                                title = child.Name,
                                icon = child.Icon
                            }
                        }).ToList();
                        NavigationBar rootRoot = new NavigationBar()
                        {
                            id        = 0,
                            pid       = 0,
                            order     = 0,
                            name      = "根节点",
                            path      = "",
                            iconCls   = "",
                            component = "BasicLayout",
                            key       = "",
                            meta      = new NavigationBarMeta(),
                        };
                        permissionTrees = permissionTrees.OrderBy(d => d.order).ToList();

                        RecursionHelper.LoopNaviBarAppendChildren(permissionTrees, rootRoot);
                        List <NavigationBar> list = new List <NavigationBar>();
                        list.Add(rootRoot);
                        return(new SuccessResponse(list));
                    }
                }
            }
            return(new SuccessResponse());
        }
Beispiel #5
0
        public async Task <MessageModel <PageModel <PermissionDto> > > GetPageList(int page, int size, string conditions, string sorts)
        {
            var data = await _permissionServices.GetPageList(page, size, conditions, sorts);

            var moduleList = await _moduleServices.Query(d => d.IsDeleted == false);

            var permissionsList = await _permissionServices.Query(d => d.IsDeleted == false);

            foreach (var item in data.data)
            {
                item.Link        = moduleList.FirstOrDefault(t => t.Id == item.ParentId)?.LinkUrl;
                item.hasChildren = permissionsList.Where(t => t.ParentId == item.Id).Any();
            }
            return(new MessageModel <PageModel <PermissionDto> >()
            {
                msg = "获取成功",
                success = true,
                data = data
            });
        }
Beispiel #6
0
        /// <summary>
        /// 查询权限
        /// </summary>
        /// <param name="loginNo"></param>
        /// <returns></returns>
        public async Task <ActionResult> QueryPermissionInfoById(int roleId)
        {
            JsonResponse result = new JsonResponse();

            try
            {
                List <TreeEntity> tree = new List <TreeEntity>();
                var Permissions        = await _permissionServices.Query(c => !c.IsDeleted);

                var userPermission = await _rolePermissionServices.QueryByRoleId(roleId);

                foreach (var item in userPermission)
                {
                    foreach (var per in Permissions)
                    {
                        if ((item.PermissionId == per.Id && per.ParentId != null) || (item.PermissionId == per.Id && Permissions.Where(c => c.ParentId == per.Id).Count() == 0))
                        {
                            per.IsChecked = true;
                        }
                    }
                }
                foreach (var parent in Permissions.Where(a => a.ParentId == null))
                {
                    tree.Add(new TreeEntity
                    {
                        id       = parent.Id,
                        @checked = parent.IsChecked,
                        title    = parent.Name
                    });
                    foreach (var child in Permissions.Where(a => a.ParentId == parent.Id))
                    {
                        tree.Where(c => c.id == parent.Id).First().children.Add(new TreeEntity()
                        {
                            id       = child.Id,
                            @checked = child.IsChecked,
                            title    = child.Name
                        });
                    }
                }
                result.data = tree;
            }
            catch (Exception ex)
            {
                result.code = ResponseCode.Fail;
                result.msg  = ex.ToString();
            }
            return(Json(result));
        }
Beispiel #7
0
        public async Task <MessageModel <string> > SaveData2TsvAsync()
        {
            var data = new MessageModel <string>()
            {
                success = true, msg = ""
            };

            if (_env.IsDevelopment())
            {
                JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                };

                // 取出数据,序列化,自己可以处理判空
                var rolesJson = JsonConvert.SerializeObject(await _roleServices.Query(d => d.IsDeleted == false), microsoftDateFormatSettings);
                FileHelper.WriteFile(Path.Combine(_env.WebRootPath, "BlogCore.Data.json", "Role_New.tsv"), rolesJson, Encoding.UTF8);


                var permissionsJson = JsonConvert.SerializeObject(await _permissionServices.Query(d => d.IsDeleted == false), microsoftDateFormatSettings);
                FileHelper.WriteFile(Path.Combine(_env.WebRootPath, "BlogCore.Data.json", "Permission_New.tsv"), permissionsJson, Encoding.UTF8);


                var modulesJson = JsonConvert.SerializeObject(await _moduleServices.Query(d => d.IsDeleted == false), microsoftDateFormatSettings);
                FileHelper.WriteFile(Path.Combine(_env.WebRootPath, "BlogCore.Data.json", "Modules_New.tsv"), modulesJson, Encoding.UTF8);


                var rmpsJson = JsonConvert.SerializeObject(await _roleModulePermissionServices.Query(d => d.IsDeleted == false), microsoftDateFormatSettings);
                FileHelper.WriteFile(Path.Combine(_env.WebRootPath, "BlogCore.Data.json", "RoleModulePermission_New.tsv"), rmpsJson, Encoding.UTF8);



                data.success = true;
                data.msg     = "生成成功!";
            }
            else
            {
                data.success = false;
                data.msg     = "当前不处于开发模式,代码生成不可用!";
            }

            return(data);
        }
Beispiel #8
0
        public async Task <JsonResponse> DeleteOperation(int id)
        {
            var operation = await _operationServices.QueryById(id);

            if (operation == null)
            {
                throw new UserOperationException("该功能不存在");
            }
            _unitOfWork.Begin();

            var delOperaResult = await _operationServices.DeleteByIdAsync(id);

            var permisOperation = (await _permissionOperationServices.Query(po => po.OperationId == operation.Id)).SingleOrDefault();

            if (permisOperation != null)
            {
                if ((await _permissionOperationServices.DeleteByIdAsync(permisOperation.Id)) <= 0)
                {
                    _unitOfWork.Rollback();
                    throw new UserOperationException("删除失败");
                }
                var permission = (await _permissionServices.Query(p => p.Id == permisOperation.PermissionId)).SingleOrDefault();
                if (permission != null)
                {
                    if ((await _permissionServices.DeleteByIdAsync(permission.Id)) <= 0)
                    {
                        _unitOfWork.Rollback();
                        throw new UserOperationException("删除失败");
                    }
                }
            }
            if (delOperaResult <= 0)
            {
                _unitOfWork.Rollback();
                throw new UserOperationException("删除失败");
            }
            _unitOfWork.Commit();
            return(new JsonResponse(true));
        }
        public async Task <MessageModel <PageModel <Permission> > > Get(int page = 1, string key = "")
        {
            PageModel <Permission> permissions = new PageModel <Permission>();
            int intPageSize = 50;

            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            #region  弃
            //var permissions = await _permissionServices.Query(a => a.IsDeleted != true);
            //if (!string.IsNullOrEmpty(key))
            //{
            //    permissions = permissions.Where(t => (t.Name != null && t.Name.Contains(key))).ToList();
            //}
            ////筛选后的数据总数
            //totalCount = permissions.Count;
            ////筛选后的总页数
            //pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intTotalCount.ObjToDecimal())).ObjToInt();
            //permissions = permissions.OrderByDescending(d => d.Id).Skip((page - 1) * intTotalCount).Take(intTotalCount).ToList();
            #endregion



            permissions = await _permissionServices.QueryPage(a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key)), page, intPageSize, " Id desc ");


            #region 单独处理

            var apis = await _moduleServices.Query(d => d.IsDeleted == false);

            var permissionsView = permissions.data;

            var permissionAll = await _permissionServices.Query(d => d.IsDeleted != true);

            foreach (var item in permissionsView)
            {
                List <int> pidarr = new List <int>
                {
                    item.Pid
                };
                if (item.Pid > 0)
                {
                    pidarr.Add(0);
                }
                var parent = permissionAll.FirstOrDefault(d => d.Id == item.Pid);

                while (parent != null)
                {
                    pidarr.Add(parent.Id);
                    parent = permissionAll.FirstOrDefault(d => d.Id == parent.Pid);
                }


                item.PidArr = pidarr.OrderBy(d => d).Distinct().ToList();
                foreach (var pid in item.PidArr)
                {
                    var per = permissionAll.FirstOrDefault(d => d.Id == pid);
                    item.PnameArr.Add((per != null ? per.Name : "根节点") + "/");
                    //var par = Permissions.Where(d => d.Pid == item.Id ).ToList();
                    //item.PCodeArr.Add((per != null ? $"/{per.Code}/{item.Code}" : ""));
                    //if (par.Count == 0 && item.Pid == 0)
                    //{
                    //    item.PCodeArr.Add($"/{item.Code}");
                    //}
                }

                item.MName = apis.FirstOrDefault(d => d.Id == item.Mid)?.LinkUrl;
            }

            permissions.data = permissionsView;

            #endregion


            return(new MessageModel <PageModel <Permission> >()
            {
                msg = "获取成功",
                success = permissions.dataCount >= 0,
                response = permissions
            });
        }
        public async Task <MessageModel <PageModel <Permission> > > Get(int page = 1, string key = "")
        {
            var data          = new MessageModel <PageModel <Permission> >();
            int intTotalCount = 50;
            int totalCount    = 0;
            int pageCount     = 1;

            var permissions = await _permissionServices.Query(a => a.IsDeleted != true);

            if (!string.IsNullOrEmpty(key))
            {
                permissions = permissions.Where(t => (t.Name != null && t.Name.Contains(key))).ToList();
            }


            //筛选后的数据总数
            totalCount = permissions.Count;
            //筛选后的总页数
            pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intTotalCount.ObjToDecimal())).ObjToInt();

            permissions = permissions.OrderByDescending(d => d.Id).Skip((page - 1) * intTotalCount).Take(intTotalCount).ToList();
            var apis = await _moduleServices.Query(d => d.IsDeleted == false);

            foreach (var item in permissions)
            {
                List <int> pidarr = new List <int>();
                pidarr.Add(item.Pid);
                if (item.Pid > 0)
                {
                    pidarr.Add(0);
                }
                var parent = permissions.FirstOrDefault(d => d.Id == item.Pid);

                while (parent != null)
                {
                    pidarr.Add(parent.Id);
                    parent = permissions.FirstOrDefault(d => d.Id == parent.Pid);
                }


                item.PidArr = pidarr.OrderBy(d => d).Distinct().ToList();
                foreach (var pid in item.PidArr)
                {
                    var per = permissions.FirstOrDefault(d => d.Id == pid);
                    item.PnameArr.Add((per != null ? per.Name : "根节点") + "/");
                    //var par = Permissions.Where(d => d.Pid == item.Id ).ToList();
                    //item.PCodeArr.Add((per != null ? $"/{per.Code}/{item.Code}" : ""));
                    //if (par.Count == 0 && item.Pid == 0)
                    //{
                    //    item.PCodeArr.Add($"/{item.Code}");
                    //}
                }

                item.MName = apis.FirstOrDefault(d => d.Id == item.Mid)?.LinkUrl;
            }

            return(new MessageModel <PageModel <Permission> >()
            {
                msg = "获取成功",
                success = totalCount >= 0,
                response = new PageModel <Permission>()
                {
                    page = page,
                    pageCount = pageCount,
                    dataCount = totalCount,
                    data = permissions,
                }
            });
        }
        public async Task <MessageModel <string> > DataMigrateFromOld2New()
        {
            var data = new MessageModel <string>()
            {
                success = true, msg = ""
            };
            var filterPermissionId = 122;

            if (_env.IsDevelopment())
            {
                try
                {
                    var apiList = await _moduleServices.Query(d => d.IsDeleted == false);

                    var permissionsAllList = await _permissionServices.Query(d => d.IsDeleted == false);

                    var permissions = permissionsAllList.Where(d => d.Pid == 0).ToList();
                    var rmps        = await _roleModulePermissionServices.GetRMPMaps();

                    List <PM> pms = new();

                    // 当然,你可以做个where查询
                    rmps = rmps.Where(d => d.PermissionId >= filterPermissionId).ToList();

                    InitPermissionTree(permissions, permissionsAllList, apiList);

                    var        actionPermissionIds = permissionsAllList.Where(d => d.Id >= filterPermissionId).Select(d => d.Id).ToList();
                    List <int> filterPermissionIds = new();
                    FilterPermissionTree(permissionsAllList, actionPermissionIds, filterPermissionIds);
                    permissions = permissions.Where(d => filterPermissionIds.Contains(d.Id)).ToList();

                    // 开启事务,保证数据一致性
                    _unitOfWork.BeginTran();

                    // 注意信息的完整性,不要重复添加,确保主库没有要添加的数据

                    // 1、保持菜单和接口
                    await SavePermissionTreeAsync(permissions, pms);

                    var rid   = 0;
                    var pid   = 0;
                    var mid   = 0;
                    var rpmid = 0;

                    // 2、保存关系表
                    foreach (var item in rmps)
                    {
                        // 角色信息,防止重复添加,做了判断
                        if (item.Role != null)
                        {
                            var isExit = (await _roleServices.Query(d => d.Name == item.Role.Name && d.IsDeleted == false)).FirstOrDefault();
                            if (isExit == null)
                            {
                                rid = await _roleServices.Add(item.Role);

                                Console.WriteLine($"Role Added:{item.Role.Name}");
                            }
                            else
                            {
                                rid = isExit.Id;
                            }
                        }

                        pid = (pms.FirstOrDefault(d => d.PidOld == item.PermissionId)?.PidNew).ObjToInt();
                        mid = (pms.FirstOrDefault(d => d.MidOld == item.ModuleId)?.MidNew).ObjToInt();
                        // 关系
                        if (rid > 0 && pid > 0)
                        {
                            rpmid = await _roleModulePermissionServices.Add(new RoleModulePermission()
                            {
                                IsDeleted    = false,
                                CreateTime   = DateTime.Now,
                                ModifyTime   = DateTime.Now,
                                ModuleId     = mid,
                                PermissionId = pid,
                                RoleId       = rid,
                            });

                            Console.WriteLine($"RMP Added:{rpmid}");
                        }
                    }


                    _unitOfWork.CommitTran();

                    data.success = true;
                    data.msg     = "导入成功!";
                }
                catch (Exception)
                {
                    _unitOfWork.RollbackTran();
                }
            }
            else
            {
                data.success = false;
                data.msg     = "当前不处于开发模式,代码生成不可用!";
            }

            return(data);
        }