Skip to content

Commit 6aeb779

Browse files
committed
优化用户权限查询
1 parent 9574b88 commit 6aeb779

3 files changed

Lines changed: 30 additions & 55 deletions

File tree

Admin.Core.Model/Admin/RolePermissionEntity.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,15 @@ public class RolePermissionEntity: EntityAdd
1919
/// </summary>
2020
public long PermissionId { get; set; }
2121

22-
#region 外键 => 导航属性,ManyToMany
2322
/// <summary>
2423
/// 角色
2524
/// </summary>
26-
[Navigate("RoleId")]
2725
public RoleEntity Role { get; set; }
2826

2927
/// <summary>
3028
/// 权限
3129
/// </summary>
32-
[Navigate("PermissionId")]
3330
public PermissionEntity Permission { get; set; }
34-
35-
/// <summary>
36-
/// 角色名称
37-
/// </summary>
38-
[Column(IsIgnore = true)]
39-
public string RoleName { get; set; }
40-
41-
/// <summary>
42-
/// 路由
43-
/// </summary>
44-
[Column(IsIgnore = true)]
45-
public string ApiPath { get; set; }
46-
#endregion
4731
}
4832

4933
}

Admin.Core.Services/Admin/Auth/AuthService.cs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Linq;
33
using System.Threading.Tasks;
4-
using System.Security.Claims;
54
using Admin.Core.Model.Admin;
65
using Admin.Core.Model.Output;
76
using Admin.Core.Repository.Admin;
@@ -19,24 +18,24 @@ public class AuthService : IAuthService
1918
private readonly IUser _user;
2019
private readonly ICache _cache;
2120
private readonly IMapper _mapper;
22-
private readonly IUserToken _userToken;
2321
private readonly IUserRepository _userRepository;
22+
private readonly IPermissionRepository _permissionRepository;
2423
private readonly IRolePermissionRepository _rolePermissionRepository;
2524

2625
public AuthService(
2726
IUser user,
2827
ICache cache,
2928
IMapper mapper,
30-
IUserToken userToken,
3129
IUserRepository userRepository,
30+
IPermissionRepository permissionRepository,
3231
IRolePermissionRepository rolePermissionRepository
3332
)
3433
{
3534
_user = user;
3635
_cache = cache;
3736
_mapper = mapper;
38-
_userToken = userToken;
3937
_userRepository = userRepository;
38+
_permissionRepository = permissionRepository;
4039
_rolePermissionRepository = rolePermissionRepository;
4140
}
4241

@@ -50,11 +49,11 @@ public async Task<IResponseOutput> LoginAsync(AuthLoginInput input)
5049
var verifyCode = await _cache.GetAsync(verifyCodeKey);
5150
if (string.IsNullOrEmpty(verifyCode))
5251
{
53-
return ResponseOutput.NotOk("验证码已过期!",1);
52+
return ResponseOutput.NotOk("验证码已过期!", 1);
5453
}
5554
if (verifyCode.ToLower() != input.VerifyCode.ToLower())
5655
{
57-
return ResponseOutput.NotOk("验证码输入有误!",2);
56+
return ResponseOutput.NotOk("验证码输入有误!", 2);
5857
}
5958
await _cache.DelAsync(verifyCodeKey);
6059
}
@@ -80,22 +79,22 @@ public async Task<IResponseOutput> LoginAsync(AuthLoginInput input)
8079
var secretKey = await _cache.GetAsync(passwordEncryptKey);
8180
if (passwordEncryptKey.IsNull())
8281
{
83-
return ResponseOutput.NotOk("解密失败!",1);
82+
return ResponseOutput.NotOk("解密失败!", 1);
8483
}
8584
input.Password = DesEncrypt.Decrypt(input.Password, secretKey);
8685
await _cache.DelAsync(passwordEncryptKey);
8786
}
8887
else
8988
{
90-
return ResponseOutput.NotOk("解密失败!",1);
89+
return ResponseOutput.NotOk("解密失败!", 1);
9190
}
9291
}
9392
#endregion
9493

9594
var password = MD5Encrypt.Encrypt32(input.Password);
9695
if (user.Password != password)
9796
{
98-
return ResponseOutput.NotOk("密码输入有误!",4);
97+
return ResponseOutput.NotOk("密码输入有误!", 4);
9998
}
10099

101100
var authLoginOutput = _mapper.Map<AuthLoginOutput>(user);
@@ -111,34 +110,36 @@ public async Task<IResponseOutput> GetUserInfoAsync()
111110
}
112111

113112
var user = await _userRepository.Select.WhereDynamic(_user.Id)
114-
.ToOneAsync(m=>new {
113+
.ToOneAsync(m => new {
115114
m.NickName,
116115
m.Name,
117116
m.Avatar
118117
});
119118

120119
//获取菜单
121-
var menus = await _rolePermissionRepository.Select
122-
.InnerJoin<UserRoleEntity>((a, b) => a.RoleId == b.RoleId && b.UserId == _user.Id)
123-
.Include(a => a.Permission.View)
124-
.Where(a => new[] { PermissionType.Group,PermissionType.Menu }.Contains(a.Permission.Type))
125-
//.Distinct()
126-
.OrderBy(a => a.Permission.ParentId)
127-
.OrderBy(a => a.Permission.Sort)
120+
var menus = await _permissionRepository.Select
121+
.Where(a => new[] { PermissionType.Group, PermissionType.Menu }.Contains(a.Type))
122+
.Where(a =>
123+
_rolePermissionRepository.Select
124+
.InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && b.PermissionId == a.Id && c.UserId == _user.Id)
125+
.Any()
126+
)
127+
.OrderBy(a => a.ParentId)
128+
.OrderBy(a => a.Sort)
128129
.ToListAsync(a => new
129130
{
130-
a.Permission.Id,
131-
a.Permission.ParentId,
132-
a.Permission.Path,
133-
ViewPath = a.Permission.View.Path,
134-
a.Permission.Label,
135-
136-
a.Permission.Icon,
137-
a.Permission.Opened,
138-
a.Permission.Closable,
139-
a.Permission.Hidden,
140-
a.Permission.NewWindow,
141-
a.Permission.External
131+
a.Id,
132+
a.ParentId,
133+
a.Path,
134+
ViewPath = a.View.Path,
135+
a.Label,
136+
137+
a.Icon,
138+
a.Opened,
139+
a.Closable,
140+
a.Hidden,
141+
a.NewWindow,
142+
a.External
142143
});
143144

144145
return ResponseOutput.Ok(new { user, menus });

Admin.Core/Admin.Core.Model.xml

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)