Construct a background management system injected with ASP. NET MVC4 + EF5 + EasyUI + Unity2.x (27)-permission management system-assign users

Source: Internet
Author: User

After the permission is set, grant the permission to the Administrator and add the operation code and js event @ Html in SysRole's index. toolButton ("btnAllot", "icon-share", "Assign user", perm, "Allot", true) Copy code $ ("# btnAllot "). click (function () {var row = $ ('# list '). datagrid ('getselected'); if (row! = Null) {$ ("# modalwindow" ).html ("<iframe width = '000000' height = '000000' scrolling = 'no' frameborder = '0' src = '/SysRole/GetUserByRole? RoleId = "+ row. id + "& Ieguid =" + GetGuid () + "'> </iframe>"); $ ("# modalwindow "). window ({title: 'assign user', width: 720, height: 400, iconCls: 'icon-edit '}). window ('open');} else {$. messageBox5s ('hprompt ', 'select a Role to assign a User') ;}}); copy the code because it is basically the same as that in section 26, but change the User to a Role, role to User Add the following three methods to SysRoleController to copy the Code # region sets the Role User [SupportFilter (ActionName = "Allot")] public ActionResult GetUserByRole (string roleId) {ViewB Ag. roleId = roleId; ViewBag. perm = GetPermission (); return View ();} [SupportFilter (ActionName = "Allot")] public JsonResult GetUserListByRole (GridPager pager, string roleId) {if (string. isNullOrWhiteSpace (roleId) return Json (0); var userList = m_BLL.GetUserByRoleId (ref pager, roleId); var jsonData = new {total = pager. totalRows, rows = (from r in userList select new SysUserModel () {Id = r. id, U SerName = r. UserName, TrueName = r. TrueName, Flag = r. flag = "0 "? "0": "1 ",}). toArray ()}; return Json (jsonData);} # endregion [SupportFilter (ActionName = "Save")] public JsonResult UpdateUserRoleByRoleId (string roleId, string userIds) {string [] arr = userIds. split (','); if (m_BLL.UpdateSysRoleSysUser (roleId, arr) {LogHandler. writeServiceLog (GetUserId (), "Ids:" + arr, "success", "Assign user", "role setting"); return Json (JsonHandler. createMessage (1, Suggestion. setSucceed), Jso NRequestBehavior. allowGet);} else {string ErrorCol = errors. error; LogHandler. writeServiceLog (GetUserId (), "Ids:" + arr, "failed", "Assign user", "role setting"); return Json (JsonHandler. createMessage (0, Suggestion. setFail), JsonRequestBehavior. allowGet) ;}} copy the code GetUserByRole. In the pop-up view, GetUserListByRole obtains the role based on the selected user and displays the selected role status (Stored Procedure Implementation) copy the Code Create proc [dbo]. [P_Sys_GetUserByRoleId] @ RoleId varchar (50) asbegin -- read User select. *, ISNULL (B. sysUserId, 0) as flag from SysUser a left joinSysRoleSysUser B on. id = B. sysUserIdand B. sysRoleId = @ RoleIdorder by B. sysRoleId descend: copy the code UpdateUserRoleByRoleId to update the selected information (Stored PROCEDURE Implementation) and copy the Code Create PROCEDURE [dbo]. [P_Sys_UpdateSysRoleSysUser] @ roleId varchar (50), @ userId varchar (50) AS -- Update the BEGIN insert into SysRoleSysUser (SysRoleId, SysUserId) values (@ roleId, @ userId) END copy the code to add the BLL Method Copy the code /// <summary> /// obtain all users corresponding to the role /// </summary> /// <param name = "roleId"> role id </param >/// <returns> </returns> public string GetRefSysUser (string roleId) {string UserName = ""; var userList = m_Rep.GetRefSysUser (db, roleId); if (userList! = Null) {foreach (var user in userList) {UserName + = "[" + user. userName + "]" ;}} return UserName;} public IQueryable <strong> GetUserByRoleId (ref GridPager pager, string roleId) {IQueryable <strong> queryData = m_Rep.GetUserByRoleId (db, roleId ); pager. totalRows = queryData. count (); queryData = m_Rep.GetUserByRoleId (db, roleId); return queryData. skip (Pager. page-1) * pager. rows ). take (pager. rows);} public bool UpdateSysRoleSysUser (string roleId, string [] userIds) {try {m_Rep.UpdateSysRoleSysUser (roleId, userIds); return true;} catch (Exception ex) {ExceptionHander. writeException (ex); return false ;}} copy Code add DAL method copy code public IQueryable <SysUser> GetRefSysUser (DBContainer db, string id) {if (! String. isNullOrEmpty (id) {return from m in db. sysRole from f in m. sysUser where m. id = id select f;} return null;} public IQueryable <P_Sys_GetUserByRoleId_Result> GetUserByRoleId (DBContainer db, string roleId) {return db. p_Sys_GetUserByRoleId (roleId ). asQueryable ();} public void UpdateSysRoleSysUser (string roleId, string [] userIds) {using (DBContainer db = new DBContainer () {db. p_Sys_DeleteS YsRoleSysUserByRoleId (roleId); foreach (string userid in userIds) {if (! String. IsNullOrWhiteSpace (userid) {db. P_Sys_UpdateSysRoleSysUser (roleId, userid) ;}} db. SaveChanges ();}}
Create PROCEDURE [dbo]. [P_Sys_DeleteSysRoleSysUserByRoleId] @ roleId varchar (50) AS -- update the role-user intermediate relationship table, delete association BEGIN delete SysRoleSysUser where SysRoleId = @ roleIdEND copy Code add BLL and DAL Interface Method Add GetUserByRole. copy the code @ using App in the cshtml view. admin; @ using App. common; @ using App. models. sys; @ {ViewBag. title = "set users included in the role"; Layout = "~ /Views/Shared/_ Index_LayoutEdit.cshtml "; List <permModel> perm = (List <permModel>) ViewBag. perm; if (perm = null) {perm = new List <permModel> () ;}< div class = "mvctool bgb" >@html. toolButton ("btnSave", "icon-save", "save", perm, "Create", true) </div> <div class = "pd3"> <table id = "RoleList"> </table> </div> @ * Jqgrid * @ <script type = "text/ javascript "> $ (function () {$ ('# rolelist '). datagrid ({url: '/SysRole/GetUse RListByRole? RoleId = @ (ViewBag. roleId) ', width: SetGridWidthSub (6), methord: 'post', height: SetGridHeightSub (41), fitColumns: true, sortName: 'id', sortOrder: 'desc ', idField: 'id', pageSize: 12, pageList: [12, 20, 30, 40, 50], pagination: true, striped: true, // whether the odd/even rows distinguish singleSelect: true, // single-choice mode columns: [{field: 'id', title: 'id', width: 80, hidden: true}, {field: 'username ', title: 'username', width: 120 },{ Field: 'truename', title: 'name', width: 80}, {field: 'flag', title: 'allocation? ', width: 80, editor: {type: 'checkbox', options: {on: '1', off: '0' }}]], onLoadSuccess: function () {var rows = $ ("# RoleList "). datagrid ("getRows"); for (var I = 0; I <rows. length; I ++) {// get the data of each row $ ('# rolelist '). datagrid ('ineinedit ', I) ;}}) ;}); </script >@* operation * @ <script type = "text/javascript" >$ (function () {$ ("# BtnSave "). click (function () {var rows =$ ("# RoleList "). datagrid ("getRows"); // This code obtains all rows on the current page. Var data = new Array (); for (var I = 0; I <rows. length; I ++) {var setFlag =$ ("td [field = 'flag'] input "). eq (I ). prop ("checked"); if (setFlag) // you can check whether any modification is made. {data. push (rows [I]. id) ;}} var userIds = data. join (); // submit the database $. post ("/SysRole/UpdateUserRoleByRoleId", {roleId: '@ (ViewBag. roleId) ', userIds: userIds}, function (data) {if (data. type = 1) {window. parent. frameReturnByMes (data. message); window. parent. frameReturnByReload (true); window. parent. frameReturnByClose ()} else {window. parent. frameReturnByMes (data. message) ;}}, "json") ;};}); </script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.