Beispiel #1
0
        public async Task <MessageModel <string> > Assign([FromBody] AssignView assignView)
        {
            var data = new MessageModel <string>();


            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 &= remove.Any() ? await _roleModulePermissionServices.DeleteByIds(remove.ToArray()) : true;

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


                        roleModulePermission.CreateId = _user.ID;
                        roleModulePermission.CreateBy = _user.Name;

                        data.success &= (await _roleModulePermissionServices.Add(roleModulePermission)) > 0;
                    }
                    else
                    {
                        foreach (var role in rmpitem)
                        {
                            if (!role.ModuleId.Equals(moduleid))
                            {
                                role.ModuleId = moduleid.Value;
                                await _roleModulePermissionServices.Update(role, new List <string> {
                                    "ModuleId"
                                });
                            }
                        }
                    }
                }

                if (data.success)
                {
                    _requirement.Permissions.Clear();
                    data.response = "";
                    data.msg      = "保存成功";
                }
            }


            return(data);
        }
        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,
                            };


                            roleModulePermission.CreateId = _user.ID;
                            roleModulePermission.CreateBy = _user.Name;

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

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

            return(data);
        }
Beispiel #3
0
        public async Task <MessageModel <string> > Post([FromBody] RoleModulePermission request)
        {
            var data = new MessageModel <string>();

            var id = await _roleModulePermissionServices.Add(request);

            data.success = id > 0;

            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg      = "添加成功";
            }

            return(data);
        }
        public async Task <MessageModel <string> > DataMigrateFromOld2New()
        {
            var data = new MessageModel <string>()
            {
                success = true, msg = ""
            };

            if (_env.IsDevelopment())
            {
                try
                {
                    // 获取权限集合数据
                    var rmps = await _roleModulePermissionServices.GetRMPMaps();

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

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

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

                    // 注意信息的完整性,不要重复添加,确保主库没有要添加的数据
                    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;
                            }
                        }

                        // 菜单
                        if (item.Permission != null)
                        {
                            pid = await _permissionServices.Add(item.Permission);

                            Console.WriteLine($"Permission Added:{item.Permission.Name}");
                        }

                        // 接口
                        if (item.Module != null)
                        {
                            mid = await _moduleServices.Add(item.Module);

                            Console.WriteLine($"Module Added:{item.Module.LinkUrl}");
                        }

                        // 关系
                        if (rid > 0 && pid > 0 && mid > 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);
        }
        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);
        }