bklLiudl
2024-08-23 7fc23d00b91d52c4c0b84fc4a4c2211607ca634d
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.ModelDto.SysDto;
using WMS.Entity;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL;
using WMS.IDAL.ISysInterface;
using Model.ModelVm.SysVm;
using WMS.Entity.Context;
using Utility;
using Talk.Extensions;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    /// RBAC服务实践
    /// </summary>
    public class RBACServer : IRBACServer
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        public IRBACRepository _rbac { get; set; }
        private readonly IMenuRepository _menu;
        private readonly IRoleRightRepository _roleright;
        private static readonly SqlSugarScope Db = DataContext.Db;
        private readonly UserManager _userManager;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rbac"></param>
        /// <param name="menu"></param>
        /// <param name="roleright"></param>
        public RBACServer(IRBACRepository rbac, IMenuRepository menu, IRoleRightRepository roleright, UserManager userManager)
        {
            _rbac = rbac;
            _menu = menu;
            _roleright = roleright;
            _userManager = userManager;
        }
 
        /// <summary>
        /// 获取rbac信息列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<RBAC>> GetRBACLists(int roleid)
        {
            List<RBAC> rbaclist = await _rbac.GetRBACLists(roleid);
            return rbaclist;
        }
 
        /// <summary>
        /// 根据角色id获取对应菜单权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<RoleRightDto>> GetRoleRightList(int id)
        {
            return await Db.Queryable<SysRoleRight>()
                           .LeftJoin<SysRoles>((a, b) => a.RoleNo == b.RoleNo)
                           .LeftJoin<SysFunctionMenu>((a, b, c) => a.MenuNo == c.MenuNo)
                           .Where((a, b, c) => a.Id == id && a.IsDel == "0")
                            .OrderBy((a, b, c) => SqlFunc.ToInt32(a.MenuNo)).OrderBy((a, b, c) => c.Ord)
                           .Select<RoleRightDto>()
                           .ToListAsync();
        }
 
        /// <summary>
        /// 根据角色Id获取对应的菜单选中
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<FunctionMenuDto>> GetMenuByroleIdNew(int id)
        {
            List<RoleRightDto> rolerightlist = await Db.Queryable<SysRoleRight>().LeftJoin<SysRoles>((a, b) => a.RoleNo == b.RoleNo)
                                                                           .LeftJoin<SysFunctionMenu>((a, b, c) => a.MenuNo == c.MenuNo)
                                                                           .Where((a, b, c) => a.Id == id && a.IsDel == "0")
                                                                           .Select<RoleRightDto>()
                                                                           .ToListAsync();
            var listMenuNo = rolerightlist.Select(s => s.MenuNo).Distinct().ToList();
            List<FunctionMenuDto> listMenu = await Db.Queryable<SysFunctionMenu>().OrderBy(m => m.MenuNo).Select(m => new FunctionMenuDto()
            {
                //Id = m.Id,
                MenuNo = m.MenuNo,
                ParentId = m.ParentNo,
                title = m.MenuName,
                Expand = listMenuNo.Contains(m.MenuNo),
                @checked = listMenuNo.Contains(m.MenuNo)
            }).ToListAsync();
            var listParent = listMenu.Where(s => s.ParentId == "").ToList();
            var listChild = listMenu.Where(s => s.ParentId != "").ToList();
            foreach (var item in listParent)
            {
                FunNew(listChild, item);
            }
            return listParent;
 
        }
        public void FunNew(List<FunctionMenuDto> all, FunctionMenuDto curItem)
        {
            List<FunctionMenuDto> childItems = all.Where(m => m.ParentId == curItem.MenuNo).ToList(); //得到子节点
            curItem.children = childItems; //将子节点加入
 
            //遍历子节点,进行递归,寻找子节点的子节点
            foreach (var subItem in childItems)
            {
                FunNew(all, subItem);
            }
        }
 
 
 
        /// <summary>
        /// 分配权限显示
        /// </summary>
        /// <param name="MenuNo"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public async Task<List<FunctionMenuDto>> GetRoleRightRBAC(string MenuNo, string UserId)
        {
            var modUser = await Db.Queryable<SysUserInfor>().FirstAsync(s => s.Id == _userManager.UserId);
            return await Db.Queryable<SysFunctionMenu>()
                            .Where(s => s.IsDel == "0")
                            .WhereIF(!string.IsNullOrEmpty(MenuNo), s => s.ParentNo == MenuNo)
                            .WhereIF(modUser.UserName.ToUpper() != "ADMIN", s => SqlFunc.Subqueryable<SysRoleRight>().Where(c => c.RoleNo == modUser.RoleNo && s.MenuNo == c.MenuNo).Any())
                            .OrderBy(s => s.Ord, OrderByType.Asc)
                            .Select<FunctionMenuDto>()
                            .ToListAsync();
        }
    }
}