Construct the background management system injected with ASP. NET MVC4 + EF5 + EasyUI + Unity2.x (26)-permission management system-assign roles

Source: Internet
Author: User

Authorize the role permission settings to add the following button @ Html to SysUser's Index. toolButton ("btnAllot", "icon-share", "Assign role", perm, "Allot", true) We need to pop up the selection window for all roles to check and save. You should have thought of this, so first set the view on the pop-up interface, I named GetRoleByUser, so I added the following three methods to SysUser to copy the Code # region sets the user role [SupportFilter (ActionName = "Allot")] public ActionResult GetRoleByUser (string userId) {ViewBag. userId = userId; ViewBag. perm = GetPermission (); return View ();} [SupportFilter (ActionName = "Allot")] public JsonResult GetRoleListByUser (GridPager pager, string userId) {if (string. isNullOrWhiteSpace (userId) return Json (0); var userList = userBLL. getRoleByUserId (ref pager, userId); var jsonData = new {total = pager. totalRows, rows = (from r in userList select new SysRoleModel () {Id = r. id, Name = r. name, Description = r. description, Flag = r. flag = "0 "? "0": "1 ",}). toArray ()}; return Json (jsonData);} # endregion [SupportFilter (ActionName = "Save")] public JsonResult UpdateUserRoleByUserId (string userId, string roleIds) {string [] arr = roleIds. split (','); if (userBLL. updateSysRoleSysUser (userId, arr) {LogHandler. writeServiceLog (GetUserId (), "Ids:" + roleIds, "success", "Assign role", "user settings"); return Json (JsonHandler. createMessage (1, Suggestion. setSucceed), JsonRequestBehavior. allowGet);} else {string ErrorCol = errors. error; LogHandler. writeServiceLog (GetUserId (), "Ids:" + roleIds, "failed", "Assign role", "user settings"); return Json (JsonHandler. createMessage (0, Suggestion. setFail), JsonRequestBehavior. allowGet) ;}} copy the code GetRoleByUser. In the pop-up view, GetRoleListByUser obtains the role based on the selected user and displays the selected role status (Stored Procedure Implementation) copy the Code USE [AppDB] GO/****** Object: StoredProcedure [dbo]. [P_Sys_GetRoleByU SerId] Script Date: 02/21/2014 11:17:38 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCreate proc [dbo]. [P_Sys_GetRoleByUserId] @ UserId varchar (50) asbegin -- read the role select. *, ISNULL (B. sysUserId, 0) as flag from SysRole a left join SysRoleSysUser B on. id = B. sysRoleId and B. sysUserId = @ UserId order by B. sysUserId descend copy code UpdateUserRoleByUserId after the selected Update (Stored PROCEDURE Implementation) Copy 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: the statement used to copy the Stored Procedure Code is short. If it is made into linq, It is not changeable. Therefore, I wrote the stored procedure. You can refer to the logic to make a program. SysBLL adds the following method to copy the code public IQueryable <P_Sys_GetRoleByUserId_Result> GetRoleByUserId (ref GridPager pager, string userId) {IQueryable <strong> queryData = sysUserRepository. getRoleByUserId (db, userId); pager. totalRows = queryData. count (); queryData = sysUserRepository. getRoleByUserId (db, userId); return queryData. skip (pager. page-1) * pager. rows ). take (pager. rows);} public bool UpdateSysRoleSysUser (string userId, string [] roleIds) {try {sysUserRepository. updateSysRoleSysUser (userId, roleIds); return true;} catch (Exception ex) {ExceptionHander. writeException (ex); return false ;}} copy code SysRepository Add the following method copy code public IQueryable <P_Sys_GetRoleByUserId_Result> GetRoleByUserId (DBContainer db, string userId) {return db. p_Sys_GetRoleByUserId (userId ). asQueryable ();} publ Ic void UpdateSysRoleSysUser (string userId, string [] roleIds) {using (DBContainer db = new DBContainer () {db. p_Sys_DeleteSysRoleSysUserByUserId (userId); foreach (string roleid in roleIds) {if (! String. isNullOrWhiteSpace (roleid) {db. p_Sys_UpdateSysRoleSysUser (roleid, userId) ;}} db. saveChanges () ;}} copy the code and add the interface method by yourself. Then enter the GetRoleByUser view code to copy the code @ using App. admin; @ using App. common; @ using App. models. sys; @ {ViewBag. title = "Set User Roles"; 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 = "UserList"> </table> </div> @ * Jqgrid * @ <script type = "text/ javascript "> $ (function () {$ ('# userlist '). datagrid ({url: '/SysUser/GetRol EListByUser? UserId = @ (ViewBag. userId) ', 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: 'name ', title: 'Role name', width: 120}, {fi Eld: 'description', title: 'description', width: 80}, {field: 'flag', title: 'allocation? ', width: 80, editor: {type: 'checkbox', options: {on: '1', off: '0' }}]], onLoadSuccess: function () {var rows = $ ("# UserList "). datagrid ("getRows"); for (var I = 0; I <rows. length; I ++) {// get the data of each row $ ('# userlist '). datagrid ('ineinedit ', I) ;}}) ;}); </script >@* operation * @ <script type = "text/javascript" >$ (function () {$ ("# BtnSave "). click (function () {var rows = $ ("# UserList "). 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 roleIds = data. join (); // submit the database $. post ("/SysUser/UpdateUserRoleByUserId", {userId: '@ (ViewBag. userId) ', roleIds: roleIds}, 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>

Related Article

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.