public void LoadMenusForRole(int roleId) { var menusForRole = MenuRoleServices.GetAll(roleId, null, null, null); hdfMenusForRole.Text = new JavaScriptSerializer().Serialize(menusForRole); RM.RegisterClientScriptBlock("LoadMenusForRole", "loadMenusForRole();"); }
public void LoadMenuPermissionForRole() { if (!string.IsNullOrEmpty(hdfRoleId.Text) && int.TryParse(hdfRoleId.Text, out var roleId) && roleId > 0) { var lstMenuRole = MenuRoleServices.GetAll(roleId, null, null, null); hdfMenuRole.Text = new JavaScriptSerializer().Serialize(lstMenuRole); RM.RegisterClientScriptBlock("LoadMenuPermission", "loadMenuPermission();"); } else { Dialog.Alert("Có lỗi xảy ra trong quá trình tải dữ liệu"); } }
/// <inheritdoc /> /// <summary> /// Page load /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { // check session current user if (Session["CurrentUser"] == null) { Response.Redirect(Resource.Get("LoginUrl"), true); } // init current user CurrentUser = (UserModel)Session["CurrentUser"]; // init menu id if (int.TryParse(Request.QueryString["mId"], out var parseMenuId) && parseMenuId > 0) { MenuId = parseMenuId; } else { var menuModel = MenuController.GetByUrl(Request.ApplicationPath); MenuId = menuModel?.Id ?? 0; } // check menu id zero or use is admin if (CurrentUser != null && CurrentUser.User.IsSuperUser) { // init full control permission CurrentPermission = new PermissionModel(true, true, true, true); } else { if (Request.ApplicationPath == "/" || string.Compare(Request.ApplicationPath, "/default.aspx", StringComparison.OrdinalIgnoreCase) == 0) { // init full control permission for default page CurrentPermission = new PermissionModel(true, true, true, true); } else { if (MenuId == 0) { CurrentPermission = new PermissionModel(false, false, false, false); } else { // get permission role var menuRoles = MenuRoleServices.GetAll(null, MenuId, null, null).Where(mr => CurrentUser.Roles.Select(r => r.Id).Contains(mr.RoleId)); // int permission var canRead = false; var canWrite = false; var canDelete = false; var fullControl = false; foreach (var menuRole in menuRoles) { // permission in format RWDF - example 1100 => Read: true, Write: true, Delete: false, FullControl: false fullControl = fullControl || menuRole.Permission[3] == '1'; canDelete = fullControl || canDelete || menuRole.Permission[2] == '1'; canWrite = canDelete || canWrite || menuRole.Permission[1] == '1'; canRead = canWrite || canRead || menuRole.Permission[0] == '1'; } // update permission by level canDelete = fullControl || canDelete; canWrite = canDelete || canWrite; canRead = canWrite || canRead; // init current page permission CurrentPermission = new PermissionModel(canRead, canWrite, canDelete, fullControl); } } } // check current permission if (CurrentPermission.CanRead) { try { // check permission var toolbarIds = new[] { "toolbarFn" }; foreach (var toolbarId in toolbarIds) { // set toolbar permission var findToolbar = FindControl(toolbarId); if (findToolbar != null) { // cast to Ext.Net.Toolbar var toolbarFn = (Toolbar)findToolbar; // check all button in toolbar foreach (var item in toolbarFn.Items) { if (item.InstanceOf == "Ext.Button") { // cast obj to button var btn = (Button)item; // set write (add & edit) permission if (btn.ID.Contains("Add") || btn.ID.Contains("Edit")) { btn.Visible = CurrentPermission.CanWrite; } // set delete permission if (btn.ID.Contains("Delete")) { btn.Visible = CurrentPermission.CanDelete; } } } } } // base load base.OnLoad(e); } catch (Exception ex) { SystemLogController.Create(new SystemLogModel("System", "BasePage", ex)); } } else { Dialog.Alert("Cảnh báo truy cập", "Bạn không có quyền truy cập chức năng này"); } }