In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the MVC website development of rights management example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
I. Preface
Not long after arriving at the company, I began to contact MVC. Now I can't understand it. I can only say that I will use this level when I arrive. I feel that MVC is still very powerful and clear to write Web.
With regard to permission management, I feel that the permissions of the website are mainly divided into menu permissions and role permissions. First of all, role permissions are relatively simple. Different roles can see different pages. This is role permissions, and menu permissions can also be said to be operation permissions. It is specific to a certain button, or the view or use rights of a drop-down box.
II. Role permissions
1. User role
First of all, the role permissions, each user has a variety of different roles, one-to-many relationship.
two。 Menu management
In menu management, we can manage a menu like this. It is easier to control a certain menu, which can be seen and typed with √.
3. Database
Let's take a look at the database, where there should be a table of roles and a table of user-role relationships.
Then there is the relationship table between roles and menus, where the PermissionIDs field is separated by the operation permission.
4. User login
When the user logs in, we can get all his roles and save them in Session according to the login's ID, and find out the corresponding menu according to the login.
/ / basic role information SqlHelperParameter sqlHelperParameterRole = new SqlHelperParameter (); sqlHelperParameterRole.Add ("UserId", dtUserRow ["UserId"] .ToString ()); DataTable dtRole = SqlHelper.ExecuteDataTable (@ "select Sys_Roles.RoleId, Sys_Roles.RoleName, Sys_Roles.Weight from (select UserId,RoleId from Sys_UsersInRoles where UserId = @ UserId) as a left join Sys_Roles on a.RoleId = Sys_Roles.RoleId", sqlHelperParameterRole); int dtRoleCount = dtRole.Rows.Count; RoleWeightMax = int.MaxValue For (int I = 0; I
< dtRoleCount; i++) { RolesSession rs = new RolesSession(); rs.RoleID = Guid.Parse(dtRole.Rows[i]["RoleId"].ToString()); rs.RoleName = dtRole.Rows[i]["RoleName"].ToString(); rs.Weight = Convert.ToInt32(dtRole.Rows[i]["Weight"]); if (RoleWeightMax >Rs.Weight) {RoleWeightMax = rs.Weight;} RoleList.Add (rs);} public class RolesSession {public Guid RoleID {get; set;} public string RoleName {get; set;} / / weight public int Weight {get; set;}}
Foreground code:
@ H9C.PMS.BLL.LogOn.MenuList.GetMenu (ViewBag.UserName)
Controller:
Public static MvcHtmlString GetMenu (string userName) {Menu menu = new Menu (); MenuStructure ms = menu.GetMenuListStructure (userName); if (ms! = null) {ms.Children.Remove (ms.Children.FirstOrDefault (o = > o.ModelCode = = "0" & & o.ParentID = = "0"));} return new MvcHtmlString (MenuNav ("0", ms)) } private static string MenuNav (string menuCode, MenuStructure menuStruc) {if (menuStruc = = null) {return "No menu available";} List list = menuStruc.Children.Where (m = > m.ParentID = = menuCode). ToList (); StringBuilder sbMenu = new StringBuilder (); foreach (var item in list) {if (item.ParentID = = "0") {sbMenu.Append ("); sbMenu.Append ("); sbMenu.Append (") } else {sbMenu.Append (""); if (item.Children.Count = = 0) {sbMenu.Append ("");} else {sbMenu.Append (");}} sbMenu.Append ("); if (item.Url = =" / ") {sbMenu.Append (");} else {string tabsIcon = "14" If (! string.IsNullOrWhiteSpace (item.Icon)) {tabsIcon = item.Icon.Replace ("/ Content/images/", ") .Replace (" .png ",");} sbMenu.Append ("); sbMenu.Append ("
");} sbMenu.Append (" + item.Title + "); sbMenu.Append ("); if (IsExistParent (item.ModelCode, item)) {sbMenu.Append (MenuNav (item.ModelCode, item));} sbMenu.Append ("); sbMenu.Append ("); if (item.ParentID =" 0 ") {sbMenu.Append (");}} return sbMenu.ToString () } private static bool IsExistParent (string modelCode, MenuStructure menuModels) {var query = menuModels.Children.FirstOrDefault (m = > m.ParentID = = modelCode); if (query = = null) {return false;} return true;}
Menu class:
Public class MenuStructure {public string ModelCode; public string Title; public string Icon; public string Url; public string ParentID; public List Children = new List ();}
The GetMenuListStructure () method is to get the menu list structure according to the user name, which is unique in the database. The trouble here is that according to the class, you can see that the menu has a parent menu submenu, so there are two loops to add in the method.
III. Menu permissions
That is, the permission to operate, such as the permission to operate a button. First of all, we put all the operation rights about the button in a class, (if there is a better way, please recommend to me thank you)
Public class Menus {public static int gongdan = 50300000ramp / task work order}
Then we need to store the Controllers (load page) of the page where the operation permission button is located into the ViewBag, as follows:
Public ActionResult Index () {H9C.PMS.BLL.RBAC.Permission pm = new BLL.RBAC.Permission (); ViewBag.IsReportPlan = pm.IsRoleHavePermissions (Roles.Shigongduizhang, Menus.gongdan, base.UserSessionModel, Menus.GongdanReportPlanByShiGongTeamer); / / report construction plan return View () } / public bool IsRoleHavePermissions (Guid roleId, int modelCode, UserSessionModel userSessionModel, int permissionCode) {List permissionModelList = this.GetRolePermissionList (roleId, modelCode, userSessionModel); if (permissionModelList = = null) {return false } foreach (var o in permissionModelList) {if (o.PCode = = permissionCode) {return true;}} return false } / public List GetRolePermissionList (Guid roleId, int modelCode, UserSessionModel userSessionModel) {foreach (var o in userSessionModel.RoleList) {if (o.RoleID = = roleId) {List permissionList = this.PermissionList (roleId, modelCode); return permissionList;}} return null } / public List PermissionList (Guid roleId, int menuId) {List pmList = new List (); using (RBACContext connEF = new RBACContext ()) {Sys_Role_Model_Permissions srmp = connEF.Sys_Role_Model_Permissions.FirstOrDefault (o = > o.ModelID = = menuId & & o.RoleId = = roleId) If (srmp! = null) {string permissions = srmp.PermissionIDs; if (! string.IsNullOrWhiteSpace (permissions)) {string [] pids = permissions.Split (new char [] {'|'}); for (int I = 0; I < pids.Length) ) {if (! string.IsNullOrWhiteSpace (PIDs [I])) {pmList.Add (new PermissionModel () {ModelCode = menuId, PCode = Convert.ToInt32 (PIDs [I]), PName = ""});}} return pmList;}
In the last method, EF is used to obtain the permissions under a certain menu and role according to the menu and role.
The front desk is very simple:
@ if (ViewBag.IsReportPlan = = true) {@: button} Thank you for reading this article carefully. I hope the article "sample Analysis of Rights Management in MVC website Development" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.