NormalAuthStrategy.cs
5.18 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// ***********************************************************************
// <summary>
// 普通用户授权策略
// </summary>
// ***********************************************************************
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.Service.Repository;
using System.Collections.Generic;
using System.Linq;
namespace Hh.Mes.Service.SystemAuth
{
/// <summary>
/// 普通用户授权策略
/// </summary>
public class NormalAuthStrategy : RepositorySqlSugar<SysUser>
{
protected SysUser _user;
//用户角色
private List<int> _userRoleIds;
public List<SysModuleView> Modules
{
get
{
//根据用户来获取模块ID
var moduleIds = Context.Queryable<SysRelevance>()
.Where(t => (t.FirstId == _user.Id && t.RelKey == Define.USERMODULE) ||
(t.RelKey == Define.ROLEMODULE && _userRoleIds.Contains(t.FirstId.Value)))
.Select(t => t.SecondId)
.ToList();
//根据用户的模块ID来获取具体模块信息
var moduleList = Context.Queryable<SysModule>().Where(t => moduleIds.Contains(t.Id)).ToList();
//组合成页面展示需要的SysModuleView格式
List<SysModuleView> modules = new List<SysModuleView>();
foreach (var item in moduleList.OrderBy(u => u.SortNo))
{
SysModuleView sysModuleView = new SysModuleView();
sysModuleView.Id = item.Id;
sysModuleView.Code = item.Code;
sysModuleView.Name = item.Name;
sysModuleView.CascadeId = item.CascadeId;
sysModuleView.IconName = item.IconName;
sysModuleView.Url = item.Url;
sysModuleView.ParentId = item.ParentId;
sysModuleView.ParentName = item.ParentName;
sysModuleView.SortNo = item.SortNo;
sysModuleView.IsShow = item.IsShow.Value;
sysModuleView.CreateBy = item.CreateBy;
sysModuleView.CreateTime = item.CreateTime;
sysModuleView.UpdateBy = item.UpdateBy;
sysModuleView.UpdateTime = item.UpdateTime;
modules.Add(sysModuleView);
}
//根据用户来获取模块内元素ID
var elementIds = Context.Queryable<SysRelevance>()
.Where(t => (t.FirstId == _user.Id && t.RelKey == Define.USERELEMENT) ||
(t.RelKey == Define.ROLEELEMENT && _userRoleIds.Contains(t.FirstId.Value)))
.Select(t => t.SecondId)
.ToList();
//根据模块内元素ID
var usermoduleelements = Context.Queryable<SysModuleElement>().Where(t => elementIds.Contains(t.Id)).ToList();
foreach (var module in modules)
{
module.Elements = usermoduleelements.Where(u => u.ModuleId == module.Id).OrderBy(u => u.Sort).ToList();
}
return modules;
}
}
public List<SysRole> Roles
{
get
{
return Context.Queryable<SysRole>().Where(t => _userRoleIds.Contains(t.Id)).ToList();
}
}
public List<SysDept> Orgs
{
get
{
var orgids = Context.Queryable<SysRelevance>()
.Where(u => (u.FirstId == _user.Id && u.RelKey == Define.USERORG) ||
(u.RelKey == Define.ROLEORG && _userRoleIds.Contains(u.FirstId.Value)))
.Select(u => u.SecondId)
.ToList();
var deptList = Context.Queryable<SysDept>().Where(t => true).ToList();
//用组织
var userDeptList = deptList.Where(t => orgids.Contains(t.Id)).ToList();
// 用户所有模块,包含组织下属的所有组织
var userAllDeptList = new List<SysDept>();
foreach (var item in userDeptList)
{
//找出当前组织的下属组织,并且不在userAllDeptList中的
var depts = deptList.Where(t => t.CascadeId.StartsWith(item.CascadeId) && !userAllDeptList.Contains(t));
userAllDeptList.AddRange(depts);
}
return userAllDeptList;
}
}
public SysUser User
{
get
{
return _user;
}
set
{
_user = value;
_userRoleIds = Context.Queryable<SysRelevance>().Where(t => t.FirstId == _user.Id && t.RelKey == Define.USERROLE).Select(u => u.SecondId.Value).ToList();
}
}
}
}