SysModuleApp.cs
3.86 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
using System;
using System.Collections.Generic;
using System.Linq;
using WebRepository;
namespace WebApp
{
public class SysModuleApp : WebApp<SysModule>
{
private SysRevelanceApp _revelanceApp;
public SysModuleApp(IUnitWork unitWork, IRepository<SysModule> repository
, SysRevelanceApp app) : base(unitWork, repository)
{
_revelanceApp = app;
}
public SysModuleApp SetLoginInfo(LoginInfo loginInfo)
{
Repository._loginInfo = loginInfo;
return this;
}
public void Add(SysModule model)
{
ChangeModuleCascade(model);
Repository.Add(model);
}
public void Update(SysModule model)
{
ChangeModuleCascade(model);
Repository.Update(model);
}
#region 用户/角色分配模块
/// <summary>
/// 加载特定用户的模块
/// TODO:这里会加载用户及用户角色的所有模块,“为用户分配模块”功能会给人一种混乱的感觉,但可以接受
/// </summary>
/// <param name="userId">The user unique identifier.</param>
public IEnumerable<SysModule> LoadForUser(int userId)
{
var roleIds = _revelanceApp.Get(Define.USERROLE, true, userId);
var moduleIds = UnitWork.Find<SysRelevance>(
u =>
(u.FirstId == userId && u.RelKey == Define.USERMODULE) ||
(u.RelKey == Define.ROLEMODULE && roleIds.Contains(u.FirstId.Value))).Select(u => u.SecondId);
return UnitWork.Find<SysModule>(u => moduleIds.Contains(u.Id)).OrderBy(u => u.SortNo);
}
/// <summary>
/// 根据某用户ID获取可访问某模块的菜单项
/// </summary>
/// <param name="moduleId"></param>
/// <param name="userId"></param>
/// <returns></returns>
public IEnumerable<SysModuleElement> LoadMenusForUser(int moduleId, int userId)
{
var elementIds = _revelanceApp.Get(Define.USERELEMENT, true, userId);
return UnitWork.Find<SysModuleElement>(u => elementIds.Contains(u.Id.Value) && u.ModuleId == moduleId);
}
/// <summary>
/// 加载特定角色的模块
/// </summary>
/// <param name="roleId">The role unique identifier.</param>
public IEnumerable<SysModule> LoadForRole(int roleId)
{
var moduleIds = UnitWork.Find<SysRelevance>(u => u.FirstId == roleId && u.RelKey == Define.ROLEMODULE)
.Select(u => u.SecondId);
return UnitWork.Find<SysModule>(u => moduleIds.Contains(u.Id)).OrderBy(u => u.SortNo);
}
public IEnumerable<SysModuleElement> LoadMenusForRole(int moduleId, int roleId)
{
var elementIds = _revelanceApp.Get(Define.ROLEELEMENT, true, roleId);
return UnitWork.Find<SysModuleElement>(u => elementIds.Contains(u.Id.Value) && u.ModuleId == moduleId);
}
#endregion 用户/角色分配模块
#region 菜单操作
/// <summary>
/// 删除指定的菜单
/// </summary>
/// <param name="ids"></param>
public void DelMenu(int[] ids)
{
UnitWork.Delete<SysModuleElement>(u => ids.Contains(u.Id.Value));
UnitWork.Save();
}
public void AddMenu(SysModuleElement model)
{
model.CreateBy = Repository._loginInfo.Account;
model.CreateTime = DateTime.Now;
UnitWork.Add(model);
UnitWork.Save();
}
#endregion
public void UpdateMenu(SysModuleElement model)
{
model.UpdateBy = Repository._loginInfo.Account;
model.UpdateTime = DateTime.Now;
UnitWork.Update<SysModuleElement>(model);
UnitWork.Save();
}
}
}