Skip to content

Commit 54d0189

Browse files
committed
修复递归获取所有子角色可能无限循环的问题
1 parent b5846bd commit 54d0189

File tree

6 files changed

+77
-21
lines changed

6 files changed

+77
-21
lines changed

Vue.Net/VOL.Core/UserManager/RoleContext.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using VOL.Core.DBManager;
66
using VOL.Core.Extensions.AutofacManager;
77
using VOL.Core.ManageUser;
8+
using VOL.Core.Services;
89
using VOL.Entity.DomainModels;
910

1011
namespace VOL.Core.UserManager
@@ -63,8 +64,9 @@ public static List<RoleNodes> GetAllChildren(int roleId)
6364
if (roleId <= 0) return null;
6465
var roles = GetAllRoleId();
6566
if (UserContext.IsRoleIdSuperAdmin(roleId)) return roles;
67+
Dictionary<int, bool> completedRoles = new Dictionary<int, bool>();
6668
List<RoleNodes> rolesChildren = new List<RoleNodes>();
67-
return GetChildren(roles, rolesChildren, roleId);
69+
return GetChildren(roles, rolesChildren, roleId, completedRoles);
6870
}
6971
public static List<int> GetAllChildrenIds(int roleId)
7072
{
@@ -74,19 +76,28 @@ public static List<int> GetAllChildrenIds(int roleId)
7476
/// 递归获取所有子节点权限
7577
/// </summary>
7678
/// <param name="roleId"></param>
77-
private static List<RoleNodes> GetChildren(List<RoleNodes> roles, List<RoleNodes> rolesChildren, int roleId)
79+
private static List<RoleNodes> GetChildren(List<RoleNodes> roles, List<RoleNodes> rolesChildren, int roleId, Dictionary<int, bool> completedRoles)
7880
{
7981
roles.ForEach(x =>
8082
{
8183
if (x.ParentId == roleId)
8284
{
85+
if (completedRoles.TryGetValue(roleId, out bool isWrite))
86+
{
87+
if (!isWrite)
88+
{
89+
Logger.Error($"获取子角色异常RoleContext,角色id:{roleId}");
90+
completedRoles[roleId] = true;
91+
}
92+
return;
93+
}
8394
rolesChildren.Add(x);
84-
GetChildren(roles, rolesChildren, x.Id);
95+
completedRoles.Add(x.Id, false);
96+
GetChildren(roles, rolesChildren, x.Id, completedRoles);
8597
}
8698
});
8799
return rolesChildren;
88100
}
89-
90101
/// <summary>
91102
/// 获取当前角色下的所有用户
92103
/// </summary>

Vue.Net/VOL.System/Services/System/Partial/Sys_RoleService.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,38 @@ public List<int> GetAllChildrenRoleId(int roleId)
159159
private List<RoleNodes> GetAllChildrenNodes(int roleId)
160160
{
161161
if (UserContext.IsRoleIdSuperAdmin(roleId)) return roles;
162-
rolesChildren = GetChildren(roleId);
162+
Dictionary<int, bool> completedRoles = new Dictionary<int, bool>();
163+
rolesChildren = GetChildren(roleId, completedRoles);
163164
return rolesChildren;
164165
}
165166
/// <summary>
166167
/// 递归获取所有子节点权限
167168
/// </summary>
168169
/// <param name="roleId"></param>
169-
private List<RoleNodes> GetChildren(int roleId)
170+
private List<RoleNodes> GetChildren(int roleId,Dictionary<int, bool> completedRoles)
170171
{
171172
roles.ForEach(x =>
172173
{
173174
if (x.ParentId == roleId)
174175
{
176+
if (completedRoles.TryGetValue(roleId, out bool isWrite))
177+
{
178+
if (!isWrite)
179+
{
180+
completedRoles[roleId] = true;
181+
Logger.Error($"获取子角色异常Sys_RoleService,角色id:{roleId}");
182+
}
183+
return;
184+
}
175185
rolesChildren.Add(x);
176-
GetChildren(x.Id);
186+
187+
completedRoles[x.Id] = false;
188+
189+
190+
if (x.Id != x.ParentId)
191+
{
192+
GetChildren(x.Id, completedRoles);
193+
}
177194
}
178195
});
179196
return rolesChildren;

Vue.Net/VOL.System/Services/System/Partial/Sys_UserService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task<WebResponseContent> Login(LoginInfo loginInfo, bool verificati
4343
Sys_User user = await repository.FindAsIQueryable(x => x.UserName == loginInfo.UserName)
4444
.FirstOrDefaultAsync();
4545

46-
if (user == null || loginInfo.PassWord.Trim() != (user.UserPwd ?? "").DecryptDES(AppSetting.Secret.User))
46+
if (user == null || loginInfo.Password.Trim() != (user.UserPwd ?? "").DecryptDES(AppSetting.Secret.User))
4747
return responseContent.Error(ResponseType.LoginError);
4848

4949
string token = JwtHelper.IssueJwt(new UserInfo()
@@ -57,7 +57,7 @@ public async Task<WebResponseContent> Login(LoginInfo loginInfo, bool verificati
5757
repository.Update(user, x => x.Token, true);
5858
UserContext.Current.LogOut(user.User_Id);
5959

60-
loginInfo.PassWord = string.Empty;
60+
loginInfo.Password = string.Empty;
6161

6262
return responseContent.OK(ResponseType.LoginSuccess);
6363
}

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/UserManager/RoleContext.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using VOL.Core.DBManager;
66
using VOL.Core.Extensions.AutofacManager;
77
using VOL.Core.ManageUser;
8+
using VOL.Core.Services;
89
using VOL.Entity.DomainModels;
910

1011
namespace VOL.Core.UserManager
@@ -63,8 +64,9 @@ public static List<RoleNodes> GetAllChildren(int roleId)
6364
if (roleId <= 0) return null;
6465
var roles = GetAllRoleId();
6566
if (UserContext.IsRoleIdSuperAdmin(roleId)) return roles;
67+
Dictionary<int, bool> completedRoles = new Dictionary<int, bool>();
6668
List<RoleNodes> rolesChildren = new List<RoleNodes>();
67-
return GetChildren(roles, rolesChildren, roleId);
69+
return GetChildren(roles, rolesChildren, roleId, completedRoles);
6870
}
6971
public static List<int> GetAllChildrenIds(int roleId)
7072
{
@@ -74,19 +76,28 @@ public static List<int> GetAllChildrenIds(int roleId)
7476
/// 递归获取所有子节点权限
7577
/// </summary>
7678
/// <param name="roleId"></param>
77-
private static List<RoleNodes> GetChildren(List<RoleNodes> roles, List<RoleNodes> rolesChildren, int roleId)
79+
private static List<RoleNodes> GetChildren(List<RoleNodes> roles, List<RoleNodes> rolesChildren, int roleId, Dictionary<int, bool> completedRoles)
7880
{
7981
roles.ForEach(x =>
8082
{
8183
if (x.ParentId == roleId)
8284
{
85+
if (completedRoles.TryGetValue(roleId, out bool isWrite))
86+
{
87+
if (!isWrite)
88+
{
89+
Logger.Error($"获取子角色异常RoleContext,角色id:{roleId}");
90+
completedRoles[roleId] = true;
91+
}
92+
return;
93+
}
8394
rolesChildren.Add(x);
84-
GetChildren(roles, rolesChildren, x.Id);
95+
completedRoles.Add(x.Id, false);
96+
GetChildren(roles, rolesChildren, x.Id, completedRoles);
8597
}
8698
});
8799
return rolesChildren;
88100
}
89-
90101
/// <summary>
91102
/// 获取当前角色下的所有用户
92103
/// </summary>

开发版dev/Vue.NetCore/Vue.Net/VOL.System/Services/System/Partial/Sys_RoleService.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task<WebResponseContent> GetCurrentUserTreePermission()
4646
/// <param name="roleId"></param>
4747
/// <returns></returns>
4848
public async Task<WebResponseContent> GetUserTreePermission(int roleId)
49-
{
49+
{
5050
WebResponseContent webResponse = new WebResponseContent();
5151
if (!UserContext.IsRoleIdSuperAdmin(roleId) && UserContext.Current.RoleId != roleId)
5252
{
@@ -159,21 +159,38 @@ public List<int> GetAllChildrenRoleId(int roleId)
159159
private List<RoleNodes> GetAllChildrenNodes(int roleId)
160160
{
161161
if (UserContext.IsRoleIdSuperAdmin(roleId)) return roles;
162-
rolesChildren = GetChildren(roleId);
162+
Dictionary<int, bool> completedRoles = new Dictionary<int, bool>();
163+
rolesChildren = GetChildren(roleId, completedRoles);
163164
return rolesChildren;
164165
}
165166
/// <summary>
166167
/// 递归获取所有子节点权限
167168
/// </summary>
168169
/// <param name="roleId"></param>
169-
private List<RoleNodes> GetChildren(int roleId)
170+
private List<RoleNodes> GetChildren(int roleId, Dictionary<int, bool> completedRoles)
170171
{
171172
roles.ForEach(x =>
172173
{
173174
if (x.ParentId == roleId)
174175
{
176+
if (completedRoles.TryGetValue(roleId, out bool isWrite))
177+
{
178+
if (!isWrite)
179+
{
180+
completedRoles[roleId] = true;
181+
Logger.Error($"获取子角色异常Sys_RoleService,角色id:{roleId}");
182+
}
183+
return;
184+
}
175185
rolesChildren.Add(x);
176-
GetChildren(x.Id);
186+
187+
completedRoles[x.Id] = false;
188+
189+
190+
if (x.Id != x.ParentId)
191+
{
192+
GetChildren(x.Id, completedRoles);
193+
}
177194
}
178195
});
179196
return rolesChildren;
@@ -322,13 +339,13 @@ public override WebResponseContent Update(SaveModel saveModel)
322339
{
323340
return WebResponseContent.Instance.Error($"上级角色不能选择自己");
324341
}
325-
if (role.Role_Id==UserContext.Current.RoleId)
342+
if (role.Role_Id == UserContext.Current.RoleId)
326343
{
327344
return WebResponseContent.Instance.Error($"不能修改自己的角色");
328345
}
329346

330347
if (GetAllChildren(role.ParentId).Any(x => x.ParentId == role.Role_Id)
331-
||repository.Exists(x=>x.ParentId== role.Role_Id))
348+
|| repository.Exists(x => x.ParentId == role.Role_Id))
332349
{
333350
return WebResponseContent.Instance.Error($"不能选择此上级角色,选择的上级角色与当前角色形成依赖关系");
334351
}

开发版dev/Vue.NetCore/Vue.Net/VOL.System/Services/System/Partial/Sys_UserService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task<WebResponseContent> Login(LoginInfo loginInfo, bool verificati
4343
Sys_User user = await repository.FindAsIQueryable(x => x.UserName == loginInfo.UserName)
4444
.FirstOrDefaultAsync();
4545

46-
if (user == null || loginInfo.PassWord.Trim() != (user.UserPwd ?? "").DecryptDES(AppSetting.Secret.User))
46+
if (user == null || loginInfo.Password.Trim() != (user.UserPwd ?? "").DecryptDES(AppSetting.Secret.User))
4747
return responseContent.Error(ResponseType.LoginError);
4848

4949
string token = JwtHelper.IssueJwt(new UserInfo()
@@ -57,7 +57,7 @@ public async Task<WebResponseContent> Login(LoginInfo loginInfo, bool verificati
5757
repository.Update(user, x => x.Token, true);
5858
UserContext.Current.LogOut(user.User_Id);
5959

60-
loginInfo.PassWord = string.Empty;
60+
loginInfo.Password = string.Empty;
6161

6262
return responseContent.OK(ResponseType.LoginSuccess);
6363
}

0 commit comments

Comments
 (0)