diff --git a/OpenAuth.App/ModuleElementManagerApp.cs b/OpenAuth.App/ModuleElementManagerApp.cs index bea1ff3f..77ed2bad 100644 --- a/OpenAuth.App/ModuleElementManagerApp.cs +++ b/OpenAuth.App/ModuleElementManagerApp.cs @@ -68,20 +68,7 @@ namespace OpenAuth.App public List LoadWithAccess(string accessType, int firstId, int moduleId) { //TODO:多个Repository使用的是不同的Context不能进行联表查询,要用UnitOfWork处理 - //var results = from element in _repository.Find(u => u.ModuleId == moduleId) - // join module in _moduleRepository.Find(null) on element.ModuleId equals module.Id - // join relev in _relevanceRepository.Find(u => u.Key == accessType && u.FirstId == firstId) - // on element.Id equals relev.SecondId into temp - // from t in temp.DefaultIfEmpty() - // select new ModuleElementVM - // { - // DomId = element.DomId, - // Id = element.Id, - // ModuleId = element.ModuleId, - // ModuleName = module.Name, - // Name = element.Name, - // Accessed = t != null - // }; + var listVms = new List(); if (moduleId == 0) return listVms; string modulename = _moduleRepository.FindSingle(u => u.Id == moduleId).Name; diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj index 85d85cc9..51bcc9fc 100644 --- a/OpenAuth.App/OpenAuth.App.csproj +++ b/OpenAuth.App/OpenAuth.App.csproj @@ -55,6 +55,7 @@ + diff --git a/OpenAuth.App/ResourceManagerApp.cs b/OpenAuth.App/ResourceManagerApp.cs index 70c5b934..4cbef49b 100644 --- a/OpenAuth.App/ResourceManagerApp.cs +++ b/OpenAuth.App/ResourceManagerApp.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using Infrastructure; +using OpenAuth.App.ViewModel; namespace OpenAuth.App { @@ -12,12 +13,15 @@ namespace OpenAuth.App { private IResourceRepository _repository; private readonly ICategoryRepository _categoryRepository; + private IRelevanceRepository _relevanceRepository; public ResourceManagerApp(IResourceRepository repository, - ICategoryRepository categoryRepository) + ICategoryRepository categoryRepository, + IRelevanceRepository relevanceRepository) { _repository = repository; _categoryRepository = categoryRepository; + _relevanceRepository = relevanceRepository; } public int GetResourceCntInOrg(int orgId) @@ -102,6 +106,57 @@ namespace OpenAuth.App } - + /// + /// 获取带有授权状态的菜单列表 + /// + /// 授权类型,当前有RoleResource/UserResource + /// + /// 当为RoleResource时,表示RoleId + /// 当为UserResource时,表示UserId + /// + /// 分类ID + public List LoadWithAccess(string accessType, int firstId, int cId) + { + var listVms = new List(); + if (cId == 0) return listVms; + + foreach (var element in _repository.LoadInOrgs(cId)) + { + var accessed = _relevanceRepository.FindSingle(u => u.Key == accessType + && u.FirstId == firstId && u.SecondId == element.Id); + listVms.Add(new ResourceVM + { + Id = element.Id, + Name = element.Name, + IsBelongUser = accessed != null, + Description = element.Description, + Key = element.Key, + Status = element.Status + }); + } + return listVms; + } + + /// + /// 为用户分配资源 + /// + /// 用户ID + /// 资源ID数组 + public void AssignResForUser(int userId, int[] resIds) + { + _relevanceRepository.DeleteBy("UserResource", resIds); + _relevanceRepository.AddRelevance("UserResource", resIds.ToLookup(u => userId)); + } + + /// + /// 为角色分配资源 + /// + /// 角色ID + /// 资源ID数组 + public void AssignResForRole(int roleId, int[] resIds) + { + _relevanceRepository.DeleteBy("RoleResource", resIds); + _relevanceRepository.AddRelevance("RoleResource", resIds.ToLookup(u => roleId)); + } } } \ No newline at end of file diff --git a/OpenAuth.App/ViewModel/ResourceVM.cs b/OpenAuth.App/ViewModel/ResourceVM.cs new file mode 100644 index 00000000..d5b36832 --- /dev/null +++ b/OpenAuth.App/ViewModel/ResourceVM.cs @@ -0,0 +1,39 @@ +namespace OpenAuth.App.ViewModel +{ + public class ResourceVM + { + /// + /// 资源表ID + /// + /// + public int Id { get; set; } + + + /// + /// + /// + /// + public string Key { get; set; } + + /// + /// 组织名称 + /// + /// + public string Name { get; set; } + + /// + /// 资源分类标识 + /// + /// + public int Status { get; set; } + + + /// + /// 描述 + /// + /// + public string Description { get; set; } + + public bool IsBelongUser { get; set; } + } +} \ No newline at end of file diff --git a/OpenAuth.App/ViewModel/RoleVM.cs b/OpenAuth.App/ViewModel/RoleVM.cs index 69168606..9787d096 100644 --- a/OpenAuth.App/ViewModel/RoleVM.cs +++ b/OpenAuth.App/ViewModel/RoleVM.cs @@ -57,7 +57,7 @@ namespace OpenAuth.App.ViewModel public static implicit operator Role(RoleVM rolevm) { - return rolevm.MapTo(); + return rolevm.MapTo(); } } diff --git a/OpenAuth.Mvc/Controllers/OrgManagerController.cs b/OpenAuth.Mvc/Controllers/OrgManagerController.cs index 3e05bb26..d46855a8 100644 --- a/OpenAuth.Mvc/Controllers/OrgManagerController.cs +++ b/OpenAuth.Mvc/Controllers/OrgManagerController.cs @@ -32,11 +32,6 @@ namespace OpenAuth.Mvc.Controllers return View(); } - public ActionResult LookupMulti() - { - return View(); - } - public ActionResult AddOrg() { return View(); diff --git a/OpenAuth.Mvc/Controllers/ResourceManagerController.cs b/OpenAuth.Mvc/Controllers/ResourceManagerController.cs index b521f6a2..ded0facb 100644 --- a/OpenAuth.Mvc/Controllers/ResourceManagerController.cs +++ b/OpenAuth.Mvc/Controllers/ResourceManagerController.cs @@ -1,13 +1,13 @@ - -using System; -using System.Web.Mvc; using Infrastructure; using OpenAuth.App; using OpenAuth.Domain; +using System; +using System.Linq; +using System.Web.Mvc; namespace OpenAuth.Mvc.Controllers { - public class ResourceManagerController : BaseController + public class ResourceManagerController : BaseController { private ResourceManagerApp _app; @@ -35,7 +35,6 @@ namespace OpenAuth.Mvc.Controllers try { _app.AddOrUpdate(model); - } catch (Exception ex) { @@ -52,20 +51,20 @@ namespace OpenAuth.Mvc.Controllers { return JsonHelper.Instance.Serialize(_app.Load(categoryId, pageCurrent, pageSize)); } - - public string LoadForTree() - { - var models = _app.LoadAll(); - //添加根节点 - models.Add(new Resource - { - Id = 0, - ParentId = -1, - Name = "根结点", - CascadeId = "0" - }); - return JsonHelper.Instance.Serialize(models); - } + + public string LoadForTree() + { + var models = _app.LoadAll(); + //添加根节点 + models.Add(new Resource + { + Id = 0, + ParentId = -1, + Name = "根结点", + CascadeId = "0" + }); + return JsonHelper.Instance.Serialize(models); + } public string Delete(int Id) { @@ -82,6 +81,63 @@ namespace OpenAuth.Mvc.Controllers return JsonHelper.Instance.Serialize(BjuiResponse); } - + #region 为用户分配资源 + + public ActionResult LookupMultiForUser(int userId) + { + ViewBag.UserId = userId; + return View(); + } + + public string LoadWithUserAccess(int cId, int userId) + { + return JsonHelper.Instance.Serialize(_app.LoadWithAccess("UserResource",userId, cId)); + } + + public string AccessForUser(int userId, string ids) + { + try + { + var resIds = ids.Split(',').Select(id => int.Parse(id)).ToArray(); + _app.AssignResForUser(userId, resIds); + } + catch (Exception e) + { + BjuiResponse.message = e.Message; + BjuiResponse.statusCode = "300"; + } + + return JsonHelper.Instance.Serialize(BjuiResponse); + } + #endregion 为用户分配资源 + + #region 为角色分配资源 + public ActionResult LookupMultiForRole(int roleId) + { + ViewBag.RoleId = roleId; + return View(); + } + + public string LoadWithRoleAccess(int cId, int roleId) + { + return JsonHelper.Instance.Serialize(_app.LoadWithAccess("RoleResource", roleId, cId)); + } + + public string AccessForRole(int roleId, string ids) + { + try + { + var resIds = ids.Split(',').Select(id => int.Parse(id)).ToArray(); + _app.AssignResForRole(roleId, resIds); + } + catch (Exception e) + { + BjuiResponse.message = e.Message; + BjuiResponse.statusCode = "300"; + } + + return JsonHelper.Instance.Serialize(BjuiResponse); + } + #endregion } } \ No newline at end of file diff --git a/OpenAuth.Mvc/OpenAuth.Mvc.csproj b/OpenAuth.Mvc/OpenAuth.Mvc.csproj index 4807e3a9..6c7a66e8 100644 --- a/OpenAuth.Mvc/OpenAuth.Mvc.csproj +++ b/OpenAuth.Mvc/OpenAuth.Mvc.csproj @@ -575,6 +575,8 @@ + + diff --git a/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml b/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml new file mode 100644 index 00000000..fa9b7a18 --- /dev/null +++ b/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml @@ -0,0 +1,126 @@ +@{ + string _prefix = "assignResForRole"; + var _treeId = _prefix + "Tree"; + var _gridId = _prefix + "Grid"; + var _treeDetail = _prefix + "Detail"; +} +
+ +
+ +
+
+
+
    +
    + +
    +
    +
    +
    + + \ No newline at end of file diff --git a/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForUser.cshtml b/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForUser.cshtml new file mode 100644 index 00000000..6e276783 --- /dev/null +++ b/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForUser.cshtml @@ -0,0 +1,126 @@ +@{ + string _prefix = "assignResForUser"; + var _treeId = _prefix + "Tree"; + var _gridId = _prefix + "Grid"; + var _treeDetail = _prefix + "Detail"; +} +
    + +
    + +
    +
    +
    +
      +
      + +
      +
      +
      +
      + + \ No newline at end of file diff --git a/OpenAuth.Mvc/Views/RoleManager/Index.cshtml b/OpenAuth.Mvc/Views/RoleManager/Index.cshtml index a8c58d9e..1e46cfb3 100644 --- a/OpenAuth.Mvc/Views/RoleManager/Index.cshtml +++ b/OpenAuth.Mvc/Views/RoleManager/Index.cshtml @@ -169,6 +169,23 @@ }); } + //为角色分配资源 + function openRoleReourceAccess(obj) { + var selected = getSelected('#@_gridId', 2); + if (selected == null) return; + + $(obj).dialog({ + id: 'accessUserRole', + url: '/ResourceManager/LookupMultiForRole', + title: '为角色分配资源', + width: 600, + height: 380, + data: { + roleId: selected + } + }); + } + //为角色分配菜单 function assignRoleElement(obj) { var selected = getSelected('#@_gridId', 2); diff --git a/OpenAuth.Mvc/Views/UserManager/Index.cshtml b/OpenAuth.Mvc/Views/UserManager/Index.cshtml index 502519c9..e3a7e0d2 100644 --- a/OpenAuth.Mvc/Views/UserManager/Index.cshtml +++ b/OpenAuth.Mvc/Views/UserManager/Index.cshtml @@ -190,6 +190,23 @@ }); } + //为用户分配资源 + function openUserReourceAccess(obj) { + var selected = getSelected('#@_gridId', 2); + if (selected == null) return; + + $(obj).dialog({ + id: 'accessUserRole', + url: '/ResourceManager/LookupMultiForUser', + title: '为用户分配资源', + width: 600, + height: 380, + data: { + userId: selected + } + }); + } + //分配菜单 function openAssignUserElement(obj) { var selected = getSelected('#@_gridId', 2); diff --git a/建表&初始化数据.sql b/建表&初始化数据.sql index 96dc88c6..c221e0e8 100644 Binary files a/建表&初始化数据.sql and b/建表&初始化数据.sql differ