RbacRoleMenuAppService.cs
3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using FoodLabeling.Application.Contracts.Dtos.RbacRoleMenu;
using FoodLabeling.Application.Contracts.IServices;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Uow;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace FoodLabeling.Application.Services;
/// <summary>
/// 角色-权限关联(食品标签-美国版对外)
/// </summary>
public class RbacRoleMenuAppService : ApplicationService, IRbacRoleMenuAppService
{
private readonly ISqlSugarRepository<RoleAggregateRoot, Guid> _roleRepository;
private readonly ISqlSugarRepository<MenuAggregateRoot, Guid> _menuRepository;
private readonly ISqlSugarRepository<RoleMenuEntity> _roleMenuRepository;
public RbacRoleMenuAppService(
ISqlSugarRepository<RoleAggregateRoot, Guid> roleRepository,
ISqlSugarRepository<MenuAggregateRoot, Guid> menuRepository,
ISqlSugarRepository<RoleMenuEntity> roleMenuRepository)
{
_roleRepository = roleRepository;
_menuRepository = menuRepository;
_roleMenuRepository = roleMenuRepository;
}
/// <inheritdoc />
[UnitOfWork]
public async Task SetAsync([FromBody] RbacRoleMenuSetInputVo input)
{
var role = await _roleRepository.GetSingleAsync(x => x.Id == input.RoleId && x.IsDeleted == false);
if (role is null)
{
throw new UserFriendlyException("角色不存在");
}
var menuIds = input.MenuIds?.Distinct().ToList() ?? new List<Guid>();
if (menuIds.Count == 0)
{
// 覆盖式:传空表示清空
await _roleMenuRepository.DeleteAsync(x => x.RoleId == input.RoleId);
return;
}
// 只允许分配未删除的菜单
var existMenuIds = await _menuRepository._DbQueryable
.Where(x => x.IsDeleted == false)
.Where(x => menuIds.Contains(x.Id))
.Select(x => x.Id)
.ToListAsync();
await _roleMenuRepository.DeleteAsync(x => x.RoleId == input.RoleId);
var entities = existMenuIds.Select(menuId =>
{
var entity = new RoleMenuEntity
{
RoleId = input.RoleId,
MenuId = menuId
};
EntityHelper.TrySetId(entity, () => GuidGenerator.Create());
return entity;
}).ToList();
if (entities.Count > 0)
{
await _roleMenuRepository.InsertRangeAsync(entities);
}
}
/// <inheritdoc />
public async Task<List<Guid>> GetMenuIdsAsync([FromQuery] Guid roleId)
{
return await _roleMenuRepository._DbQueryable
.Where(x => x.RoleId == roleId)
.Select(x => x.MenuId)
.ToListAsync();
}
/// <inheritdoc />
[UnitOfWork]
public async Task RemoveAsync([FromBody] RbacRoleMenuRemoveInputVo input)
{
var role = await _roleRepository.GetSingleAsync(x => x.Id == input.RoleId && x.IsDeleted == false);
if (role is null)
{
throw new UserFriendlyException("角色不存在");
}
var menuIds = input.MenuIds?.Distinct().ToList() ?? new List<Guid>();
if (menuIds.Count == 0)
{
return;
}
await _roleMenuRepository._Db.Deleteable<RoleMenuEntity>()
.Where(x => x.RoleId == input.RoleId)
.WhereIF(menuIds.Count > 0, x => menuIds.Contains(x.MenuId))
.ExecuteCommandAsync();
}
}