protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row == null) { return; } IMenu entity = e.Row.DataItem as IMenu; if (entity == null) { return; } CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox; CheckBoxList cblist = e.Row.FindControl("CheckBoxList1") as CheckBoxList; // 检查权限 IRoleMenu rm = FindByRoleAndMenu(RoleID, entity.ID); cb.Checked = (rm != null); if (rm != null) { cb.ToolTip = rm.PermissionFlag.ToString(); } //if (rm != null) cb.Text = rm.PermissionFlag.ToString(); // 如果有子节点,则不显示 if (entity.Childs != null && entity.Childs.Count > 0) { //cb.Visible = false; cblist.Visible = false; return; } // 检查权限 Dictionary <PermissionFlags, String> flags = EnumHelper.GetDescriptions <PermissionFlags>(); cblist.Items.Clear(); foreach (PermissionFlags item in flags.Keys) { if (item == PermissionFlags.None) { continue; } if (!IsFullPermission && item >= PermissionFlags.Custom1) { continue; } ListItem li = new ListItem(flags[item], ((Int32)item).ToString()); if (rm != null && (rm.PermissionFlag & item) == item) { li.Selected = true; } cblist.Items.Add(li); } }
IRoleMenu FindByRoleAndMenu(Int32 roleID, Int32 menuID) { if (_rms == null) { Factory.Cache.Clear(null); _rms = Factory.Cache.Entities; } return(_rms.Find(delegate(IEntity e) { IRoleMenu rm = e as IRoleMenu; return rm.RoleID == roleID && rm.MenuID == menuID; }) as IRoleMenu); }
//public static void GetMenuAccessByUser(int userId, int menuId, out bool canAdd, out bool canEdit, out bool canDelete, out bool canView) //{ // canAdd = false; // canEdit = false; // canDelete = false; // canView = false; // IRoleMenu roleMenuAccess = UserDAL.GetMenuAccessByUser(userId, menuId); // if (!ReferenceEquals(roleMenuAccess, null)) // { // canAdd = roleMenuAccess.CanAdd; // canEdit = roleMenuAccess.CanEdit; // canDelete = roleMenuAccess.CanDelete; // canView = roleMenuAccess.CanView; // } //} public static IUserPermission GetMenuAccessByUser(int userId, int menuId) { IUserPermission userPermission = new UserPermission(); IRoleMenu roleMenuAccess = UserDAL.GetMenuAccessByUser(userId, menuId); if (!ReferenceEquals(roleMenuAccess, null)) { userPermission.CanAdd = roleMenuAccess.CanAdd; userPermission.CanEdit = roleMenuAccess.CanEdit; userPermission.CanDelete = roleMenuAccess.CanDelete; userPermission.CanView = roleMenuAccess.CanView; } return(userPermission); }
void CheckAndAddParent(Int32 roleid, IMenu menu) { // 如果父级没有授权,则授权 while ((menu = menu.Parent) != null) { IRoleMenu rm = FindByRoleAndMenu(roleid, menu.ID); if (rm == null) { rm = Factory.Create(false) as IRoleMenu; rm.RoleID = roleid; rm.MenuID = menu.ID; rm.PermissionFlag = PermissionFlags.All; rm.Save(); } } }
public static IRoleMenu GetMenuAccessByUser(int userId, int menuId) { string strExecution = "[admin].[uspGetMenuAccessByUser]"; IRoleMenu roleMenu = null; using (DbQuery oDq = new DbQuery(strExecution)) { oDq.AddIntegerParam("@UserId", userId); oDq.AddIntegerParam("@MenuId", menuId); DataTableReader reader = oDq.GetTableReader(); while (reader.Read()) { roleMenu = new RoleMenuEntity(reader); } reader.Close(); } return(roleMenu); }
public static IRoleMenu GetMenuAccessByUser(int userId, int menuId) { IRoleMenu roleMenu = null; using (SqlDataHelper helper = new SqlDataHelper(ConnectionString)) { helper.CommandText = "[webadmin].[uspGetMenuAccessByUser]"; helper.CommandType = CommandType.StoredProcedure; helper.Parameters.Add("@UserId", userId); helper.Parameters.Add("@MenuId", menuId); helper.Open(); helper.ExecuteReader(CommandBehavior.CloseConnection); while (helper.DataReader.Read()) { roleMenu = new RoleMenuEntity(helper.DataReader); } helper.Close(); } return(roleMenu); }
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { if (RoleID < 1) { return; } CheckBoxList cb = sender as CheckBoxList; //只需判断cb是否为空,该角色只有“查看”权限时cb.SelectedItem为空。 //if (cb == null || cb.SelectedItem == null) return; if (cb == null) { return; } GridViewRow row = cb.BindingContainer as GridViewRow; if (row == null) { return; } IMenu menu = CommonManageProvider.Provider.MenuRoot.AllChilds[row.DataItemIndex] as IMenu; if (menu == null) { return; } // 检查权限 IRoleMenu rm = FindByRoleAndMenu(RoleID, menu.ID); // 没有权限,增加 if (rm == null) { if (!Manager.Acquire(PermissionFlags.Insert)) { WebHelper.Alert("没有添加权限!"); return; } rm = Factory.Create(false) as IRoleMenu; rm.RoleID = RoleID; rm.MenuID = menu.ID; } // 遍历权限项 PermissionFlags flag = PermissionFlags.None; foreach (ListItem item in cb.Items) { if (item.Selected) { flag |= (PermissionFlags)(Int32.Parse(item.Value)); } } if (rm.PermissionFlag != flag) { if (!Manager.Acquire(PermissionFlags.Update)) { WebHelper.Alert("没有编辑权限!"); return; } rm.PermissionFlag = flag; rm.Save(); // 如果父级没有授权,则授权 CheckAndAddParent(RoleID, menu); } // 清空缓存,否则一会绑定的时候会绑定旧数据 _rms = null; gv.DataBind(); }
protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (RoleID < 1) { return; } CheckBox cb = sender as CheckBox; if (cb == null) { return; } GridViewRow row = cb.BindingContainer as GridViewRow; if (row == null) { return; } IMenu menu = CommonManageProvider.Provider.MenuRoot.AllChilds[row.DataItemIndex] as IMenu; if (menu == null) { return; } // 检查权限 IRoleMenu rm = FindByRoleAndMenu(RoleID, menu.ID); if (cb.Checked) { // 没有权限,增加 if (rm == null) { if (!Manager.Acquire(PermissionFlags.Insert)) { WebHelper.Alert("没有添加权限!"); return; } rm = Factory.Create(false) as IRoleMenu; rm.RoleID = RoleID; rm.MenuID = menu.ID; rm.PermissionFlag = PermissionFlags.All; rm.Save(); // 如果父级没有授权,则授权 CheckAndAddParent(RoleID, menu); } } else { // 如果有权限,删除 if (rm != null) { if (!Manager.Acquire(PermissionFlags.Delete)) { WebHelper.Alert("没有删除权限!"); return; } (rm as IEntity).Delete(); } } // 清空缓存,否则一会绑定的时候会绑定旧数据 _rms = null; gv.DataBind(); }
public RoleMenuController(IRole _irole, IMenu _imenu, IRoleMenu _iroleMenu) { irole = _irole; imenu = _imenu; iroleMenu = _iroleMenu; }