From 92ead809094e2c667a94fde6252aac132430e169 Mon Sep 17 00:00:00 2001 From: yubaolee Date: Mon, 30 Nov 2015 00:12:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7=E6=A8=A1?= =?UTF-8?q?=E5=9D=97/=E7=94=A8=E6=88=B7=E8=A7=92=E8=89=B2=E5=88=86?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/ModuleManagerApp.cs | 6 ++ OpenAuth.App/OpenAuth.App.csproj | 1 + OpenAuth.App/RoleManagerApp.cs | 37 +++++++-- OpenAuth.App/UserManagerApp.cs | 11 +-- OpenAuth.App/ViewModel/RoleVM.cs | 64 +++++++++++++++ .../Interface/IUserRoleRepository.cs | 14 ++++ OpenAuth.Domain/OpenAuth.Domain.csproj | 1 + .../Controllers/ModuleManagerController.cs | 17 ++++ .../Controllers/RoleManagerController.cs | 26 +++++-- .../Controllers/UserManagerController.cs | 19 +---- OpenAuth.Mvc/OpenAuth.Mvc.csproj | 1 + .../Views/ModuleManager/LookupMulti.cshtml | 2 +- .../Views/RoleManager/LookupMulti.cshtml | 77 +++++++++++++++++++ OpenAuth.Mvc/Views/UserManager/Index.cshtml | 37 ++++----- OpenAuth.Mvc/Web.config | 10 ++- .../OpenAuth.Repository.csproj | 1 + OpenAuth.Repository/UserRoleRepository.cs | 34 ++++++++ OpenAuth.UnitTest/TestRoleApp.cs | 4 +- OpenAuth.UnitTest/TestUserApp.cs | 3 +- 19 files changed, 297 insertions(+), 68 deletions(-) create mode 100644 OpenAuth.App/ViewModel/RoleVM.cs create mode 100644 OpenAuth.Domain/Interface/IUserRoleRepository.cs create mode 100644 OpenAuth.Mvc/Views/RoleManager/LookupMulti.cshtml create mode 100644 OpenAuth.Repository/UserRoleRepository.cs diff --git a/OpenAuth.App/ModuleManagerApp.cs b/OpenAuth.App/ModuleManagerApp.cs index caba25f9..99f3dfa9 100644 --- a/OpenAuth.App/ModuleManagerApp.cs +++ b/OpenAuth.App/ModuleManagerApp.cs @@ -114,6 +114,12 @@ namespace OpenAuth.App } } + public void AccessModules(int userId, int[] ids) + { + _userModuleRepository.DeleteByUser(userId); + _userModuleRepository.AddUserModule(userId, ids); + } + #region 私有方法 //根据同一级中最大的语义ID diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj index 3fea74be..3499c01a 100644 --- a/OpenAuth.App/OpenAuth.App.csproj +++ b/OpenAuth.App/OpenAuth.App.csproj @@ -50,6 +50,7 @@ + diff --git a/OpenAuth.App/RoleManagerApp.cs b/OpenAuth.App/RoleManagerApp.cs index b443cb02..362bbcc0 100644 --- a/OpenAuth.App/RoleManagerApp.cs +++ b/OpenAuth.App/RoleManagerApp.cs @@ -4,6 +4,7 @@ using OpenAuth.Domain.Interface; using System; using System.Collections.Generic; using System.Linq; +using System.Web.Security; namespace OpenAuth.App { @@ -11,12 +12,15 @@ namespace OpenAuth.App { private IRoleRepository _repository; private IOrgRepository _orgRepository; + private IUserRoleRepository _userRoleRepository; public RoleManagerApp(IRoleRepository repository, - IOrgRepository orgRepository) + IOrgRepository orgRepository, + IUserRoleRepository userRoleRepository) { _repository = repository; _orgRepository = orgRepository; + _userRoleRepository = userRoleRepository; } public int GetRoleCntInOrg(int orgId) @@ -45,12 +49,12 @@ namespace OpenAuth.App } else { - roles = _repository.LoadInOrgs(pageindex, pagesize,GetSubOrgIds(orgId)); + roles = _repository.LoadInOrgs(pageindex, pagesize, GetSubOrgIds(orgId)); total = _repository.GetRoleCntInOrgs(orgId); } - - return new + + return new { total = total, list = roles, @@ -70,8 +74,8 @@ namespace OpenAuth.App public Role Find(int id) { - var role = _repository.FindSingle(u => u.Id == id); - if(role == null) role = new Role(); + var role = _repository.FindSingle(u => u.Id == id); + if (role == null) role = new Role(); return role; } @@ -95,6 +99,25 @@ namespace OpenAuth.App } - + + public List LoadWithUser(int userId) + { + var roleIds = _repository.Find(null).ToList(); + var rolevms = new List(); + foreach (var role in roleIds) + { + RoleVM rolevm = role; + rolevm.IsBelongUser = (_userRoleRepository.FindSingle(u => u.RoleId == role.Id && u.UserId == userId) != + null); + rolevms.Add(rolevm); + } + return rolevms; + } + + public void AccessRole(int userId, int[] roleIds) + { + _userRoleRepository.DeleteByUser(userId); + _userRoleRepository.AddUserRole(userId, roleIds); + } } } \ No newline at end of file diff --git a/OpenAuth.App/UserManagerApp.cs b/OpenAuth.App/UserManagerApp.cs index a3949d69..c1f75b07 100644 --- a/OpenAuth.App/UserManagerApp.cs +++ b/OpenAuth.App/UserManagerApp.cs @@ -11,15 +11,12 @@ namespace OpenAuth.App { private IUserRepository _repository; private IOrgRepository _orgRepository; - private IUserModuleRepository _usermoduleRepository; public UserManagerApp(IUserRepository repository, - IOrgRepository orgRepository, - IUserModuleRepository usermoduleRepository) + IOrgRepository orgRepository) { _repository = repository; _orgRepository = orgRepository; - _usermoduleRepository = usermoduleRepository; } public int GetUserCntInOrg(int orgId) @@ -115,11 +112,5 @@ namespace OpenAuth.App _repository.SetOrg(user.Id, orgIds); } - - public void AccessModules(int userId, int[] ids) - { - _usermoduleRepository.DeleteByUser(userId); - _usermoduleRepository.AddUserModule(userId, ids); - } } } \ No newline at end of file diff --git a/OpenAuth.App/ViewModel/RoleVM.cs b/OpenAuth.App/ViewModel/RoleVM.cs new file mode 100644 index 00000000..00abee63 --- /dev/null +++ b/OpenAuth.App/ViewModel/RoleVM.cs @@ -0,0 +1,64 @@ +// *********************************************************************** +// Assembly : OpenAuth.App +// Author : yubaolee +// Created : 11-29-2015 +// +// Last Modified By : yubaolee +// Last Modified On : 11-29-2015 +// *********************************************************************** +// +// Copyright (c) www.cnblogs.com/yubaolee. All rights reserved. +// +// 角色模型视图 +// *********************************************************************** + +using System; +using Infrastructure; +using OpenAuth.Domain; + +namespace OpenAuth.App.ViewModel +{ + public partial class RoleVM + { + /// + /// 用户ID + /// + /// + public int Id { get; set; } + + /// + /// 名称 + /// + /// + public string Name { get; set; } + + + /// + /// 所属部门节点语义ID + /// + /// + public string OrgCascadeId { get; set; } + + /// + /// 所属部门名称 + /// + /// + public string OrgName { get; set; } + + /// + ///是否属于某用户 + /// + public bool IsBelongUser { get; set; } + + public static implicit operator RoleVM(Role role) + { + return AutoMapperExt.ConvertTo(role); + } + + public static implicit operator Role(RoleVM rolevm) + { + return AutoMapperExt.ConvertTo(rolevm); + } + + } +} diff --git a/OpenAuth.Domain/Interface/IUserRoleRepository.cs b/OpenAuth.Domain/Interface/IUserRoleRepository.cs new file mode 100644 index 00000000..82c6151b --- /dev/null +++ b/OpenAuth.Domain/Interface/IUserRoleRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OpenAuth.Domain.Interface +{ + public interface IUserRoleRepository : IRepository + { + void DeleteByUser(params int[] userIds); + void AddUserRole(int userId, params int[] roleIds); + } +} diff --git a/OpenAuth.Domain/OpenAuth.Domain.csproj b/OpenAuth.Domain/OpenAuth.Domain.csproj index 41c5ff15..926d847b 100644 --- a/OpenAuth.Domain/OpenAuth.Domain.csproj +++ b/OpenAuth.Domain/OpenAuth.Domain.csproj @@ -46,6 +46,7 @@ + diff --git a/OpenAuth.Mvc/Controllers/ModuleManagerController.cs b/OpenAuth.Mvc/Controllers/ModuleManagerController.cs index 6a635bb1..fd90e365 100644 --- a/OpenAuth.Mvc/Controllers/ModuleManagerController.cs +++ b/OpenAuth.Mvc/Controllers/ModuleManagerController.cs @@ -2,6 +2,7 @@ using Infrastructure; using OpenAuth.App; using OpenAuth.Domain; using System; +using System.Linq; using System.Web.Mvc; namespace OpenAuth.Mvc.Controllers @@ -86,6 +87,22 @@ namespace OpenAuth.Mvc.Controllers return JsonHelper.Instance.Serialize(orgs); } + public string AccessModule(int userId, string moduleIds) + { + try + { + var ids = moduleIds.Split(',').Select(id => int.Parse(id)).ToArray(); + _app.AccessModules(userId, ids); + } + catch (Exception e) + { + BjuiResponse.message = e.Message; + BjuiResponse.statusCode = "300"; + } + + return JsonHelper.Instance.Serialize(BjuiResponse); + } + #region 命令操作 public ActionResult Add(int id = 0) { diff --git a/OpenAuth.Mvc/Controllers/RoleManagerController.cs b/OpenAuth.Mvc/Controllers/RoleManagerController.cs index e8af4734..79218a86 100644 --- a/OpenAuth.Mvc/Controllers/RoleManagerController.cs +++ b/OpenAuth.Mvc/Controllers/RoleManagerController.cs @@ -1,9 +1,9 @@ -using System; -using System.Web.Mvc; using Infrastructure; using OpenAuth.App; -using OpenAuth.App.ViewModel; using OpenAuth.Domain; +using System; +using System.Linq; +using System.Web.Mvc; namespace OpenAuth.Mvc.Controllers { @@ -28,14 +28,13 @@ namespace OpenAuth.Mvc.Controllers return View(_app.Find(id)); } - //添加或修改组织 + //添加或修改角色 [HttpPost] public string Add(Role role) { try { _app.AddOrUpdate(role); - } catch (Exception ex) { @@ -46,7 +45,7 @@ namespace OpenAuth.Mvc.Controllers } /// - /// 加载组织下面的所有用户 + /// 加载角色下面的所有用户 /// public string Load(int orgId, int pageCurrent = 1, int pageSize = 30) { @@ -70,5 +69,20 @@ namespace OpenAuth.Mvc.Controllers return JsonHelper.Instance.Serialize(BjuiResponse); } + + #region 为用户设置角色界面 + public ActionResult LookUpMulti(int userId) + { + ViewBag.UserId = userId; + return View(_app.LoadWithUser(userId)); + } + + public string AccessRoles(int userId, string ids) + { + var roleids = ids.Split(',').Select(id => int.Parse(id)).ToArray(); + _app.AccessRole(userId, roleids); + return JsonHelper.Instance.Serialize(BjuiResponse); + } + #endregion } } \ No newline at end of file diff --git a/OpenAuth.Mvc/Controllers/UserManagerController.cs b/OpenAuth.Mvc/Controllers/UserManagerController.cs index 5e3dc208..c48701cc 100644 --- a/OpenAuth.Mvc/Controllers/UserManagerController.cs +++ b/OpenAuth.Mvc/Controllers/UserManagerController.cs @@ -1,12 +1,9 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Infrastructure; using OpenAuth.App; using OpenAuth.App.ViewModel; -using OpenAuth.Mvc.Models; -using WebGrease.Css.Extensions; namespace OpenAuth.Mvc.Controllers { @@ -74,20 +71,6 @@ namespace OpenAuth.Mvc.Controllers return JsonHelper.Instance.Serialize(BjuiResponse); } - public string AccessModule(int userId, string moduleIds) - { - try - { - var ids = moduleIds.Split(',').Select(id => int.Parse(id)).ToArray(); - _app.AccessModules(userId, ids); - } - catch (Exception e) - { - BjuiResponse.message = e.Message; - BjuiResponse.statusCode = "300"; - } - - return JsonHelper.Instance.Serialize(BjuiResponse); - } + } } \ No newline at end of file diff --git a/OpenAuth.Mvc/OpenAuth.Mvc.csproj b/OpenAuth.Mvc/OpenAuth.Mvc.csproj index 2a82ef70..2cfd6bca 100644 --- a/OpenAuth.Mvc/OpenAuth.Mvc.csproj +++ b/OpenAuth.Mvc/OpenAuth.Mvc.csproj @@ -624,6 +624,7 @@ + 10.0 diff --git a/OpenAuth.Mvc/Views/ModuleManager/LookupMulti.cshtml b/OpenAuth.Mvc/Views/ModuleManager/LookupMulti.cshtml index bf962021..2cc8117d 100644 --- a/OpenAuth.Mvc/Views/ModuleManager/LookupMulti.cshtml +++ b/OpenAuth.Mvc/Views/ModuleManager/LookupMulti.cshtml @@ -89,7 +89,7 @@ } function save() { - $.post('UserManager/AccessModule', { userId: $('#userId').val(), moduleIds: moduleIds }, + $.post('ModuleManager/AccessModule', { userId: $('#userId').val(), moduleIds: moduleIds }, function (json) { var rel = $.parseJSON(json); if (rel.statusCode == "200") { diff --git a/OpenAuth.Mvc/Views/RoleManager/LookupMulti.cshtml b/OpenAuth.Mvc/Views/RoleManager/LookupMulti.cshtml new file mode 100644 index 00000000..52bc66d8 --- /dev/null +++ b/OpenAuth.Mvc/Views/RoleManager/LookupMulti.cshtml @@ -0,0 +1,77 @@ +@model List +@{ + ViewBag.Title = "title"; + Layout = null; +} +@*
+ +
*@ + +
+ + + + + + + + + + + + + @foreach (var role in Model) + { + + + + + + + + } + +
ID名称所属部门部门级联ID是否已经分配
@role.Id@role.Name@role.OrgName@role.OrgCascadeId checked }>
+
+ + + \ No newline at end of file diff --git a/OpenAuth.Mvc/Views/UserManager/Index.cshtml b/OpenAuth.Mvc/Views/UserManager/Index.cshtml index 8327bf80..9de647da 100644 --- a/OpenAuth.Mvc/Views/UserManager/Index.cshtml +++ b/OpenAuth.Mvc/Views/UserManager/Index.cshtml @@ -22,23 +22,6 @@ $(document).ready(function () { initZtree(); loadDataGrid(); - - $('#btnAccess').on('afterchange.bjui.lookup', function (e, data) { - var accessIds = data.value; //得到授权的模块IDS - - var selected = getSelected(2); - if (selected == null) return; - - $.post('UserManager/AccessModule', { userId: selected, moduleIds: accessIds }, - function (json) { - var rel = $.parseJSON(json); - if (rel.statusCode == "200") { - $(this).alertmsg('ok', rel.message); - } else { - $(this).alertmsg('error', rel.message); - } - }); - }); }); //加载数据到datagrid function loadDataGrid() { @@ -54,9 +37,8 @@ toolbarCustom: '添加' + '' + - '' + - '', + ''+ + '', columns: [ { name: 'Id', @@ -210,5 +192,20 @@ } }); } + + //用户角色授权 + function openRoleAccess(obj) { + var selected = getSelected(2); + if (selected == null) return; + + $(obj).dialog({ + id: 'accessUserRole', + url: '/RoleManager/LookupMulti', + title: '为用户分配角色', + data: { + userId: selected + } + }); + } //@@ sourceURL=userManagerIndex.js \ No newline at end of file diff --git a/OpenAuth.Mvc/Web.config b/OpenAuth.Mvc/Web.config index 431efff3..56637258 100644 --- a/OpenAuth.Mvc/Web.config +++ b/OpenAuth.Mvc/Web.config @@ -39,11 +39,15 @@ - - - + + + + diff --git a/OpenAuth.Repository/OpenAuth.Repository.csproj b/OpenAuth.Repository/OpenAuth.Repository.csproj index 516a9a87..8b41fcc7 100644 --- a/OpenAuth.Repository/OpenAuth.Repository.csproj +++ b/OpenAuth.Repository/OpenAuth.Repository.csproj @@ -73,6 +73,7 @@ + diff --git a/OpenAuth.Repository/UserRoleRepository.cs b/OpenAuth.Repository/UserRoleRepository.cs new file mode 100644 index 00000000..4787687f --- /dev/null +++ b/OpenAuth.Repository/UserRoleRepository.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OpenAuth.Domain; +using OpenAuth.Domain.Interface; + +namespace OpenAuth.Repository +{ + public class UserRoleRepository :BaseRepository, IUserRoleRepository + { + public void DeleteByUser(params int[] userIds) + { + Delete(u =>userIds.Contains(u.UserId)); + } + + public void AddUserRole(int userId, params int[] roleIds) + { + + foreach (var roleid in roleIds) + { + Add(new UserRole + { + UserId = userId, + RoleId = roleid, + OperateTime = DateTime.Now + }); + } + Save(); + + } + } +} diff --git a/OpenAuth.UnitTest/TestRoleApp.cs b/OpenAuth.UnitTest/TestRoleApp.cs index 056b2fbd..572bc7e5 100644 --- a/OpenAuth.UnitTest/TestRoleApp.cs +++ b/OpenAuth.UnitTest/TestRoleApp.cs @@ -16,7 +16,9 @@ namespace OpenAuth.UnitTest public class TestRoleApp { - private RoleManagerApp _app = new RoleManagerApp(new RoleRepository(), new OrgRepository()); + private RoleManagerApp _app = new RoleManagerApp(new RoleRepository(), + new OrgRepository(), + new UserRoleRepository()); private string _time = DateTime.Now.ToString("HH_mm_ss_ms"); diff --git a/OpenAuth.UnitTest/TestUserApp.cs b/OpenAuth.UnitTest/TestUserApp.cs index 1be08c32..ec67a4b2 100644 --- a/OpenAuth.UnitTest/TestUserApp.cs +++ b/OpenAuth.UnitTest/TestUserApp.cs @@ -17,8 +17,7 @@ namespace OpenAuth.UnitTest { private UserManagerApp _app = new UserManagerApp(new UserRepository(), - new OrgRepository(), - new UserModuleRepository()); + new OrgRepository()); private string _time = DateTime.Now.ToString("HH_mm_ss_ms");