mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-22 12:09:19 +08:00
增加撤销与启动,详见:#I3ILBG
调整工程结构,采用模块化机制
This commit is contained in:
182
OpenAuth.App/ModuleManager/ModuleManagerApp.cs
Normal file
182
OpenAuth.App/ModuleManager/ModuleManagerApp.cs
Normal file
@@ -0,0 +1,182 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.Request;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public class ModuleManagerApp : BaseTreeApp<Module,OpenAuthDBContext>
|
||||
{
|
||||
private RevelanceManagerApp _revelanceApp;
|
||||
|
||||
public void Add(Module model)
|
||||
{
|
||||
var loginContext = _auth.GetCurrentUser();
|
||||
if (loginContext == null)
|
||||
{
|
||||
throw new CommonException("登录已过期", Define.INVALID_TOKEN);
|
||||
}
|
||||
|
||||
CaculateCascade(model);
|
||||
|
||||
Repository.Add(model);
|
||||
|
||||
AddDefaultMenus(model);
|
||||
//当前登录用户的所有角色自动分配模块
|
||||
loginContext.Roles.ForEach(u =>
|
||||
{
|
||||
_revelanceApp.Assign(new AssignReq
|
||||
{
|
||||
type = Define.ROLEMODULE,
|
||||
firstId = u.Id,
|
||||
secIds = new[] {model.Id}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public void Update(Module obj)
|
||||
{
|
||||
UpdateTreeObj(obj);
|
||||
}
|
||||
|
||||
|
||||
#region 用户/角色分配模块
|
||||
|
||||
/// <summary>
|
||||
/// 加载特定角色的模块
|
||||
/// </summary>
|
||||
/// <param name="roleId">The role unique identifier.</param>
|
||||
public IEnumerable<Module> LoadForRole(string roleId)
|
||||
{
|
||||
var moduleIds = UnitWork.Find<Relevance>(u => u.FirstId == roleId && u.Key == Define.ROLEMODULE)
|
||||
.Select(u => u.SecondId);
|
||||
return UnitWork.Find<Module>(u => moduleIds.Contains(u.Id)).OrderBy(u => u.SortNo);
|
||||
}
|
||||
|
||||
//获取角色可访问的模块字段
|
||||
public IEnumerable<string> LoadPropertiesForRole(string roleId, string moduleCode)
|
||||
{
|
||||
return _revelanceApp.Get(Define.ROLEDATAPROPERTY, roleId, moduleCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据某角色ID获取可访问某模块的菜单项
|
||||
/// </summary>
|
||||
public IEnumerable<ModuleElement> LoadMenusForRole(string moduleId, string roleId)
|
||||
{
|
||||
var elementIds = _revelanceApp.Get(Define.ROLEELEMENT, true, roleId);
|
||||
var query = UnitWork.Find<ModuleElement>(u => elementIds.Contains(u.Id));
|
||||
if (!string.IsNullOrEmpty(moduleId))
|
||||
{
|
||||
query = query.Where(u => u.ModuleId == moduleId);
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
#endregion 用户/角色分配模块
|
||||
|
||||
|
||||
#region 菜单操作
|
||||
|
||||
/// <summary>
|
||||
/// 删除指定的菜单
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
public void DelMenu(string[] ids)
|
||||
{
|
||||
UnitWork.Delete<ModuleElement>(u => ids.Contains(u.Id));
|
||||
UnitWork.Save();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 新增菜单
|
||||
/// <para>当前登录用户的所有角色会自动分配菜单</para>
|
||||
/// </summary>
|
||||
public void AddMenu(ModuleElement model)
|
||||
{
|
||||
var loginContext = _auth.GetCurrentUser();
|
||||
if (loginContext == null)
|
||||
{
|
||||
throw new CommonException("登录已过期", Define.INVALID_TOKEN);
|
||||
}
|
||||
|
||||
UnitWork.ExecuteWithTransaction(() =>
|
||||
{
|
||||
UnitWork.Add(model);
|
||||
|
||||
//当前登录用户的所有角色自动分配菜单
|
||||
loginContext.Roles.ForEach(u =>
|
||||
{
|
||||
_revelanceApp.Assign(new AssignReq
|
||||
{
|
||||
type = Define.ROLEELEMENT,
|
||||
firstId = u.Id,
|
||||
secIds = new[] {model.Id}
|
||||
});
|
||||
});
|
||||
UnitWork.Save();
|
||||
});
|
||||
}
|
||||
|
||||
public void UpdateMenu(ModuleElement model)
|
||||
{
|
||||
UnitWork.Update<ModuleElement>(model);
|
||||
UnitWork.Save();
|
||||
}
|
||||
|
||||
//添加默认按钮
|
||||
private void AddDefaultMenus(Module module)
|
||||
{
|
||||
AddMenu(new ModuleElement
|
||||
{
|
||||
ModuleId = module.Id,
|
||||
DomId = "btnAdd",
|
||||
Script = "add()",
|
||||
Name = "添加",
|
||||
Sort = 1,
|
||||
Icon = "xinzeng",
|
||||
Class = "success",
|
||||
Remark = "新增" + module.Name
|
||||
});
|
||||
AddMenu(new ModuleElement
|
||||
{
|
||||
ModuleId = module.Id,
|
||||
DomId = "btnEdit",
|
||||
Script = "edit()",
|
||||
Name = "编辑",
|
||||
Sort = 2,
|
||||
Icon = "bianji-copy",
|
||||
Class = "primary",
|
||||
Remark = "修改" + module.Name
|
||||
});
|
||||
AddMenu(new ModuleElement
|
||||
{
|
||||
ModuleId = module.Id,
|
||||
DomId = "btnDel",
|
||||
Script = "del()",
|
||||
Name = "删除",
|
||||
Sort = 3,
|
||||
Icon = "shanchu",
|
||||
Class = "danger",
|
||||
Remark = "删除" + module.Name
|
||||
});
|
||||
|
||||
//todo:可以自己添加更多默认按钮
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public ModuleManagerApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<Module,OpenAuthDBContext> repository
|
||||
, RevelanceManagerApp app, IAuth auth) : base(unitWork, repository, auth)
|
||||
{
|
||||
_revelanceApp = app;
|
||||
}
|
||||
}
|
||||
}
|
49
OpenAuth.App/ModuleManager/Response/ModuleElementVM.cs
Normal file
49
OpenAuth.App/ModuleManager/Response/ModuleElementVM.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
namespace OpenAuth.App.Response
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户ID
|
||||
/// </summary>
|
||||
public class ModuleElementVM
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户ID
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// DOM ID
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string DomId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 组织名称
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string Name { get; set; }
|
||||
|
||||
//模块ID
|
||||
public int ModuleId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所属模块名称
|
||||
/// </summary>
|
||||
public string ModuleName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 授权状态
|
||||
/// </summary>
|
||||
public bool Accessed { get; set; }
|
||||
|
||||
public ModuleElementVM()
|
||||
{
|
||||
this.Id = 0;
|
||||
this.DomId = string.Empty;
|
||||
this.Name = string.Empty;
|
||||
this.ModuleId = 0;
|
||||
this.ModuleName = string.Empty;
|
||||
this.Accessed = false;
|
||||
}
|
||||
}
|
||||
}
|
83
OpenAuth.App/ModuleManager/Response/ModuleView.cs
Normal file
83
OpenAuth.App/ModuleManager/Response/ModuleView.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System.Collections.Generic;
|
||||
using Infrastructure;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
||||
namespace OpenAuth.App.Response
|
||||
{
|
||||
public class ModuleView
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点语义ID
|
||||
/// </summary>
|
||||
public string CascadeId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主页面URL
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 父节点流水号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string ParentId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 父节点流水号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string ParentName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点图标文件名称
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string IconName { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态,0:正常,-1:隐藏,不在导航列表中显示
|
||||
/// </summary>
|
||||
public int Status { get; set; }
|
||||
|
||||
|
||||
public bool Checked { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序号
|
||||
/// </summary>
|
||||
public int SortNo { get; set; }
|
||||
|
||||
public string Code { get; set; }
|
||||
|
||||
public bool IsSys { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 模块中的元素
|
||||
/// </summary>
|
||||
public List<ModuleElement> Elements { get; set; }
|
||||
|
||||
public static implicit operator ModuleView(Module module)
|
||||
{
|
||||
return module.MapTo<ModuleView>();
|
||||
}
|
||||
|
||||
public static implicit operator Module(ModuleView view)
|
||||
{
|
||||
return view.MapTo<Module>();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user