Construct a background management system injected with ASP. NET MVC4 + EF5 + EasyUI + Unity2.x (24)-permission management system-grant permissions

Source: Internet
Author: User

Select a role group (the role to be authorized, select the module to be authorized, and then authorize the operation code. When an operation code exists, we should change the rightflag field in the SysRight table to indicate that it has permissions. I don't know if you still remember this picture. We also want to do it. The figure shows that JQGrid looks a little different from Easyui, but the method is the same.) return to the header and see the IsValid of the SysRightOperate table, we associate the authorization role with the three tables of the module and the operation code. The IsValid field is used to identify whether the operation is permitted. When the first authorization is performed, a record is added to SysRightOperate, if the next update is used to determine whether authorization has been granted or not, delete or update the IsValid. Here I am updating the IsValid. You can also delete it. The same principle applies. We have already created the DAL layer and BLL layer of SysRight. Based on the idea, we only need to create two more methods to update SysRightOperate (that is, the above mentioned) and attach the module permission item according to the selected role and module (determine whether the operation code is authorized, that is, the check box in the figure) now add two methods to ISysRightRepository // update int UpdateRight (SysRightOperate model); // according to the selected role and module load module permission List <rule> GetRightByRoleAndModule (string roleId, string moduleId); P_Sys_GetRightByRoleAndModule_Result this is a stored procedure. Because these two methods are complex, we use the stored procedure to copy the Code Create proc [dbo]. [P_Sys_Get RightByRoleAndModule] @ roleId varchar (50), @ moduleId varchar (50) as -- beginselect. id,. name,. keyCode,. moduleId, ISNULL (B. isValid, 0) as isvalid,. sort, @ roleId + @ moduleId as RightId from SysModuleOperate a left outer join (select c. id,. isValid from SysRightOperate a, SysRight B, SysModuleOperate c where RightId in (select Id From SysRight where RoleId = @ roleId and ModuleId = @ moduleId) And. rightId = B. id and B. moduleId = c. moduleId and. keyCode = c. keyCode) B on. id = B. id where. moduleId = @ moduleIdend to copy the code. Therefore, you must add this stored procedure to EF and generate complex types of entities P_Sys_GetRightByRoleAndModule_Result. Then Create proc [dbo]. [P_Sys_UpdateSysRightRightFlag] @ moduleId varchar (200), @ roleId varchar (200) asbegin -- calculate the rightflag ID of the upper-level module: declare @ count int -- the first layer: permission calculated by Operation permission item: select @ count = COUN T (*) from SysRightOperate where RightId = @ roleId + @ moduleId and IsValid = 1 if (@ count> 0) begin update SysRight set Rightflag = 1 where ModuleId = @ moduleId and RoleId = @ brief update SysRight set Rightflag = 0 where ModuleId = @ moduleId and RoleId = @ roleIdend -- calculate the next declare @ parentId varchar (50) set @ parentId = @ moduleId while (@ parentId <> '0') begin select @ parentid = ParentId from SysModule where Id = @ parent Id if (@ parentId is null) begin return end select @ count = COUNT (*) from SysRight where ModuleId in (select Id from SysModule where ParentId = @ parentId) and RoleId = @ roleId and Rightflag = 1 if (@ count> 0) begin update SysRight set Rightflag = 1 where ModuleId = @ parentId and RoleId = @ roleId end else begin update SysRight set Rightflag = 0 where ModuleId = @ parentId and RoleId = @ roleId endendend copy the code is the right of the computing upper-level module The flag ID is the RightFlag field at the beginning. This field determines the display of the navigation bar. Therefore, you must add the following SysRightRepository logic code to copy the public int UpdateRight (SysRightOperateModel model) for each authorization operation) {// convert SysRightOperate rightOperate = new SysRightOperate (); rightOperate. id = model. id; rightOperate. rightId = model. rightId; rightOperate. keyCode = model. keyCode; rightOperate. isValid = model. isValid; // determine whether rightOperate exists. If yes, update rightOperate. Otherwise, add a using (DBCo Ntainer db = new DBContainer () {SysRightOperate right = db. SysRightOperate. Where (a => a. Id = rightOperate. Id). FirstOrDefault (); if (right! = Null) {right. isValid = rightOperate. isValid;} else {db. sysRightOperate. addObject (rightOperate);} if (db. saveChanges ()> 0) {// update role -- Effective module flag var sysRight = (from r in db. sysRight where r. id = rightOperate. rightId select r ). first (); db. p_Sys_UpdateSysRightRightFlag (sysRight. moduleId, sysRight. roleId); return 1 ;}} return 0 ;}// press the selected role and the permission item of the module to load the public List <P_Sys_GetRightByRoleAndModule_Result> GetRightByRoleAndModule (string roleId, string moduleId) {List <P_Sys_GetRightByRoleAndModule_Result> result = null; using (DBContainer db = new DBContainer () {result = db. p_Sys_GetRightByRoleAndModule (roleId, moduleId ). toList ();} return result;} copy the code as expected. We need to add code to IBLL and BLL. You can add the code to access the DAL layer by yourself.

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.