From 40ffa5f1e82c653cd322abb29fe7a8807aa83bbe Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sun, 17 Apr 2016 23:16:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=B5=84=E6=BA=90=E5=88=86?= =?UTF-8?q?=E9=85=8D=E5=89=8D=E7=AB=AF=E6=A8=A1=E5=9D=97=E5=8C=96=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/OpenAuth.App.csproj | 1 + OpenAuth.App/ResourceManagerApp.cs | 19 +-- OpenAuth.App/RevelanceManagerApp.cs | 44 ++++++ OpenAuth.Mvc/AutofacExt.cs | 5 +- OpenAuth.Mvc/BllScripts/assignRes.js | 113 ++++++++++++++++ OpenAuth.Mvc/BllScripts/roleManager.js | 5 +- OpenAuth.Mvc/BllScripts/usermanager.js | 5 +- .../Controllers/OrgManagerController.cs | 9 -- .../Controllers/RelevanceManagerController.cs | 55 ++++++++ .../Controllers/ResourceManagerController.cs | 72 +++------- OpenAuth.Mvc/OpenAuth.Mvc.csproj | 6 +- .../Views/OrgManager/LookupParent.cshtml | 62 --------- .../Views/ResourceManager/AssignRes.cshtml | 31 +++++ .../ResourceManager/LookupMultiForRole.cshtml | 126 ------------------ .../ResourceManager/LookupMultiForUser.cshtml | 126 ------------------ OpenAuth.Repository/RoleRepository.cs | 4 +- 16 files changed, 285 insertions(+), 398 deletions(-) create mode 100644 OpenAuth.App/RevelanceManagerApp.cs create mode 100644 OpenAuth.Mvc/BllScripts/assignRes.js create mode 100644 OpenAuth.Mvc/Controllers/RelevanceManagerController.cs delete mode 100644 OpenAuth.Mvc/Views/OrgManager/LookupParent.cshtml create mode 100644 OpenAuth.Mvc/Views/ResourceManager/AssignRes.cshtml delete mode 100644 OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml delete mode 100644 OpenAuth.Mvc/Views/ResourceManager/LookupMultiForUser.cshtml diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj index 035fde15..0e0bc6cd 100644 --- a/OpenAuth.App/OpenAuth.App.csproj +++ b/OpenAuth.App/OpenAuth.App.csproj @@ -48,6 +48,7 @@ + diff --git a/OpenAuth.App/ResourceManagerApp.cs b/OpenAuth.App/ResourceManagerApp.cs index 131378d3..95d1a38e 100644 --- a/OpenAuth.App/ResourceManagerApp.cs +++ b/OpenAuth.App/ResourceManagerApp.cs @@ -118,7 +118,7 @@ namespace OpenAuth.App public List LoadWithAccess(string accessType, int firstId, int cId) { var listVms = new List(); - if (cId == 0) return listVms; + // if (cId == 0) return listVms; foreach (var element in _repository.LoadInOrgs(cId)) { @@ -137,17 +137,6 @@ namespace OpenAuth.App return listVms; } - /// - /// 为用户分配资源 - /// - /// 用户ID - /// 资源ID数组 - public void AssignResForUser(int userId, int[] resIds) - { - _relevanceRepository.DeleteBy("UserResource", resIds); - _relevanceRepository.AddRelevance("UserResource", resIds.ToLookup(u => userId)); - } - /// /// 为角色分配资源 /// @@ -155,8 +144,12 @@ namespace OpenAuth.App /// 资源ID数组 public void AssignResForRole(int roleId, int[] resIds) { - _relevanceRepository.DeleteBy("RoleResource", resIds); _relevanceRepository.AddRelevance("RoleResource", resIds.ToLookup(u => roleId)); } + + public void DelResForRole(int roleId, int[] resIds) + { + _relevanceRepository.DeleteBy("RoleResource", resIds.ToLookup(u =>roleId)); + } } } \ No newline at end of file diff --git a/OpenAuth.App/RevelanceManagerApp.cs b/OpenAuth.App/RevelanceManagerApp.cs new file mode 100644 index 00000000..7e717771 --- /dev/null +++ b/OpenAuth.App/RevelanceManagerApp.cs @@ -0,0 +1,44 @@ + +using OpenAuth.Domain; +using OpenAuth.Domain.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using Infrastructure; + +using OpenAuth.App.ViewModel; + +namespace OpenAuth.App +{ + public class RevelanceManagerApp + { + + private readonly IRelevanceRepository _relevanceRepository; + + public RevelanceManagerApp(IRelevanceRepository relevanceRepository) + { + _relevanceRepository = relevanceRepository; + } + + /// + /// 添加关联 + /// 比如给用户分配资源,那么firstId就是用户ID,secIds就是资源ID列表 + /// + /// 关联的类型,如"UserResource" + public void Assign(string type, int firstId, int[] secIds) + { + _relevanceRepository.AddRelevance(type, secIds.ToLookup(u => firstId)); + } + + /// + /// 取消关联 + /// + /// 关联的类型,如"UserResource" + /// The first identifier. + /// The sec ids. + public void UnAssign(string type, int firstId, int[] secIds) + { + _relevanceRepository.DeleteBy(type, secIds.ToLookup(u =>firstId)); + } + } +} \ No newline at end of file diff --git a/OpenAuth.Mvc/AutofacExt.cs b/OpenAuth.Mvc/AutofacExt.cs index 4cd356d7..ce45392c 100644 --- a/OpenAuth.Mvc/AutofacExt.cs +++ b/OpenAuth.Mvc/AutofacExt.cs @@ -42,9 +42,10 @@ namespace OpenAuth.Mvc builder.RegisterType(); builder.RegisterType(); builder.RegisterType(); - builder.RegisterType(); + builder.RegisterType(); + builder.RegisterType(); - // Register your MVC controllers. + // Register your MVC controllers. builder.RegisterControllers(typeof(MvcApplication).Assembly); // OPTIONAL: Register model binders that require DI. diff --git a/OpenAuth.Mvc/BllScripts/assignRes.js b/OpenAuth.Mvc/BllScripts/assignRes.js new file mode 100644 index 00000000..e8ac400c --- /dev/null +++ b/OpenAuth.Mvc/BllScripts/assignRes.js @@ -0,0 +1,113 @@ +// *********************************************************************** +// Assembly : OpenAuth.Mvc +// Author : yubaolee +// Created : 04-16-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 04-16-2016 +// *********************************************************************** +// +// 版权所有(C) 2015 +// +// 分配资源模块处理 +// *********************************************************************** + + +$(document).ready(function () { + $.CurrentDialog.find("#btnAccess").on("click", function () { + var ids = dlgList.getSelectedProperties('Id'); + if (ids == null) return; + + $.post('/RelevanceManager/Assign', { + type: $("#moduleType").val(), + firstId: $('#firstId').val(), + secIds: ids + }, function (json) { + dlgList.reload(); + }); + }); + $.CurrentDialog.find("#btnDelAccess").on("click", function () { + var ids = dlgList.getSelectedProperties('Id'); + if (ids == null) return; + + $.post('/RelevanceManager/UnAssign', { + type: $("#moduleType").val(), + firstId: $('#firstId').val(), + secIds: ids + }, function (json) { + dlgList.reload(); + }); + }); +}); + +//grid列表模块 +function DialogList() { + var selectedId = 0; //选中的ID + var url = '/ResourceManager/LoadWithAccess?cId='; + this.maingrid = $.CurrentDialog.find('#maingrid').datagrid({ + showToolbar: false, + filterThead: false, + target: $(this), + columns: [ + { + name: 'Id', + label: '角色ID', + hide: true + }, + { + name: 'Name', + label: '资源名称', + width: 100 + }, + + { + name: 'IsBelongUser', + label: '是否已经授权', + type: 'select', + align: 'center', + items: [{ 'false': '未授权', 'true': '已授权' }], + width: 100 + } + ], + dataUrl: url + selectedId + '&key=' + $('#moduleType').val() + '&firstId=' + $('#firstId').val(), + fullGrid: true, + showLinenumber: true, + showCheckboxcol: true, + paging: true, + filterMult: false, + showTfoot: false, + height: '100%' + }); + this.reload = function (id) { + if (id != undefined) selectedId = id; + this.maingrid.datagrid('reload', { dataUrl: url + selectedId + '&key=' + $('#moduleType').val() + '&firstId=' + $('#firstId').val() }); + } +}; +DialogList.prototype = new Grid(); +var dlgList = new DialogList(); + +var ztree = function () { + var setting = { + view: { selectedMulti: false }, + data: { + key: { + name: 'Name', + title: 'Name' + }, + simpleData: { + enable: true, + idKey: 'Id', + pIdKey: 'ParentId', + rootPId: 'null' + } + }, + callback: { onClick: zTreeOnClick } + }; + $.getJSON('/CategoryManager/LoadForTree', function (json) { + var zTreeObj = $.fn.zTree.init($.CurrentDialog.find("#tree"), setting, json); + zTreeObj.expandAll(true); + }); +}(); +function zTreeOnClick(event, treeId, treeNode) { + dlgList.reload(treeNode.Id); +} \ No newline at end of file diff --git a/OpenAuth.Mvc/BllScripts/roleManager.js b/OpenAuth.Mvc/BllScripts/roleManager.js index 980f4da8..a497c2ec 100644 --- a/OpenAuth.Mvc/BllScripts/roleManager.js +++ b/OpenAuth.Mvc/BllScripts/roleManager.js @@ -272,12 +272,13 @@ function openRoleReourceAccess(obj) { $(obj).dialog({ id: 'accessUserRole', - url: '/ResourceManager/LookupMultiForRole', + url: '/ResourceManager/AssignRes', title: '为角色分配资源', width: 600, height: 380, data: { - roleId: selected.Id + firstId: selected.Id, + key:"RoleResource" } }); } diff --git a/OpenAuth.Mvc/BllScripts/usermanager.js b/OpenAuth.Mvc/BllScripts/usermanager.js index e57215c9..cce5ece4 100644 --- a/OpenAuth.Mvc/BllScripts/usermanager.js +++ b/OpenAuth.Mvc/BllScripts/usermanager.js @@ -300,12 +300,13 @@ function openUserReourceAccess(obj) { $(obj).dialog({ id: 'accessUserRole', - url: '/ResourceManager/LookupMultiForUser', + url: '/ResourceManager/AssignRes', title: '为用户分配资源', width: 600, height: 380, data: { - userId: selected.Id + firstId: selected.Id, + key:"UserResource" } }); } diff --git a/OpenAuth.Mvc/Controllers/OrgManagerController.cs b/OpenAuth.Mvc/Controllers/OrgManagerController.cs index c07bc55d..6e5e79d5 100644 --- a/OpenAuth.Mvc/Controllers/OrgManagerController.cs +++ b/OpenAuth.Mvc/Controllers/OrgManagerController.cs @@ -41,15 +41,6 @@ namespace OpenAuth.Mvc.Controllers return View(); } - /// - /// 选择上级机构页面 - /// - /// ActionResult. - public ActionResult LookupParent() - { - return View(); - } - public ActionResult AddOrg() { return View(); diff --git a/OpenAuth.Mvc/Controllers/RelevanceManagerController.cs b/OpenAuth.Mvc/Controllers/RelevanceManagerController.cs new file mode 100644 index 00000000..70834ad0 --- /dev/null +++ b/OpenAuth.Mvc/Controllers/RelevanceManagerController.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Web; +using System.Web.Mvc; +using Infrastructure; +using OpenAuth.App; +using OpenAuth.Mvc.Models; + +namespace OpenAuth.Mvc.Controllers +{ + public class RelevanceManagerController : BaseController + { + private RevelanceManagerApp _app; + + public RelevanceManagerController() + { + _app = AutofacExt.GetFromFac(); + } + + [HttpPost] + [Anonymous] + public string Assign(string type, int firstId, string secIds) + { + try + { + var secIdList = JsonHelper.Instance.Deserialize(secIds); + _app.Assign(type, firstId, secIdList); + } + catch (Exception ex) + { + BjuiResponse.statusCode = "300"; + BjuiResponse.message = ex.Message; + } + return JsonHelper.Instance.Serialize(BjuiResponse); + } + [HttpPost] + [Anonymous] + public string UnAssign(string type, int firstId, string secIds) + { + try + { + var secIdList = JsonHelper.Instance.Deserialize(secIds); + _app.UnAssign(type, firstId, secIdList); + } + catch (Exception ex) + { + BjuiResponse.statusCode = "300"; + BjuiResponse.message = ex.Message; + } + return JsonHelper.Instance.Serialize(BjuiResponse); + } + } +} \ No newline at end of file diff --git a/OpenAuth.Mvc/Controllers/ResourceManagerController.cs b/OpenAuth.Mvc/Controllers/ResourceManagerController.cs index 4c408597..1519c6c8 100644 --- a/OpenAuth.Mvc/Controllers/ResourceManagerController.cs +++ b/OpenAuth.Mvc/Controllers/ResourceManagerController.cs @@ -76,63 +76,31 @@ namespace OpenAuth.Mvc.Controllers return JsonHelper.Instance.Serialize(BjuiResponse); } - #region 为用户分配资源 - - public ActionResult LookupMultiForUser(int userId) + /// + /// 为用户或角色分配权限 + /// + /// 关联表中的firstId. + /// 关联表中的Key + /// 如:UserResource/RoleResource + /// + /// ActionResult. + public ActionResult AssignRes(int firstId, string key) { - ViewBag.UserId = userId; + ViewBag.FirstId = firstId; + ViewBag.ModuleType = key; return View(); } - public string LoadWithUserAccess(int cId, int userId) + /// + /// 加载带有授权的资源信息 + /// + /// 分类ID + /// 关联表中的firstId + /// 关联表中的key + /// System.String. + public string LoadWithAccess(int cId, int firstId, string key) { - return JsonHelper.Instance.Serialize(_app.LoadWithAccess("UserResource",userId, cId)); + return JsonHelper.Instance.Serialize(_app.LoadWithAccess(key,firstId, 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 976495f7..7c5b71a1 100644 --- a/OpenAuth.Mvc/OpenAuth.Mvc.csproj +++ b/OpenAuth.Mvc/OpenAuth.Mvc.csproj @@ -138,6 +138,7 @@ + @@ -161,6 +162,7 @@ + @@ -609,8 +611,7 @@ - - + @@ -676,7 +677,6 @@ - diff --git a/OpenAuth.Mvc/Views/OrgManager/LookupParent.cshtml b/OpenAuth.Mvc/Views/OrgManager/LookupParent.cshtml deleted file mode 100644 index 80594154..00000000 --- a/OpenAuth.Mvc/Views/OrgManager/LookupParent.cshtml +++ /dev/null @@ -1,62 +0,0 @@ -@model dynamic - -@{ - ViewBag.Title = "title"; - Layout = null; -} -
-
-
    -
    -
    - - - diff --git a/OpenAuth.Mvc/Views/ResourceManager/AssignRes.cshtml b/OpenAuth.Mvc/Views/ResourceManager/AssignRes.cshtml new file mode 100644 index 00000000..f2e1e643 --- /dev/null +++ b/OpenAuth.Mvc/Views/ResourceManager/AssignRes.cshtml @@ -0,0 +1,31 @@ +
    + +
    + +
    +
    +
    +
      +
      + +
      +
      +
      +
      +
      + + \ No newline at end of file diff --git a/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml b/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml deleted file mode 100644 index 0dca72e2..00000000 --- a/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForRole.cshtml +++ /dev/null @@ -1,126 +0,0 @@ -@{ - 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 deleted file mode 100644 index f74d31c5..00000000 --- a/OpenAuth.Mvc/Views/ResourceManager/LookupMultiForUser.cshtml +++ /dev/null @@ -1,126 +0,0 @@ -@{ - 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.Repository/RoleRepository.cs b/OpenAuth.Repository/RoleRepository.cs index f414ddeb..672aafb6 100644 --- a/OpenAuth.Repository/RoleRepository.cs +++ b/OpenAuth.Repository/RoleRepository.cs @@ -19,7 +19,9 @@ namespace OpenAuth.Repository public IEnumerable LoadInOrgs(params int[] orgId) { - var result = from resource in Context.Resources.Where(u => orgId.Contains(u.CategoryId)) select resource; + bool isZero = orgId.Length == 1 && orgId[0] == 0; //判断传进来的是否为0 + var result = from resource in Context.Resources.Where(u =>isZero + || orgId.Contains(u.CategoryId)) select resource; return result; }