/******************************************************************************* * Copyright(c) 2014 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:FormPermissionManager.cs * 2.功能描述:页面功能权限控制 * 需要将页面上需要控制的控件的Visible属性设置为False,当该用户有权限后会直接设置为True * 编辑履历: * 作者 日期 版本 修改内容 * 张国印 2014/9/18 1.00 新建 *******************************************************************************/ using System.Data; using System.Linq; using System.Windows.Forms; using Dongke.IBOSS.PRD.Basics.BaseControls; using Dongke.IBOSS.PRD.Basics.BaseResources; namespace Dongke.IBOSS.PRD.Client.CommonModule { /// /// 页面功能权限控制 /// 需要将页面上需要控制的控件的Visible属性设置为False,当该用户有权限后会直接设置为True /// public class FormPermissionManager { /// /// 设置页面控件的权限设置Visible的值 如果用户有权限设置为TURE,其他情况不处理 /// /// 当前窗体名称 /// 当前控件名称 /// 用户功能权限列表 /// 功能权限列表 public static void FormPermissionControl(string pFormName, Control control, DataTable pUserRightData, DataTable pFunction) { if (control == null || string.IsNullOrEmpty(pFormName) || pUserRightData == null || pUserRightData.Rows.Count <= 0 || pFunction == null || pFunction.Rows.Count <= 0) { return; } if (control is IDKControl || control is ToolStrip) { //if (control is C_ToolStrip) if (control is ToolStrip) { #region 设置C_ToolStrip的可用状态 //C_ToolStrip tempToolStrip = control as C_ToolStrip; ToolStrip tempToolStrip = control as ToolStrip; SetCToolStripStyle(tempToolStrip); // 设置CToolStrip的通用样式 foreach (ToolStripItem tempStrip in tempToolStrip.Items) { SetToolStripButtonStyle(tempStrip); //设置ToolStripItem的通用样式 if (tempStrip is ToolStripButton) { ToolStripButton tempToolButton = tempStrip as ToolStripButton; if (tempToolButton.Tag != null && tempToolButton.Tag.ToString() == "[ALL]") { continue; } if (GetFormIsPermission(pFormName, tempToolButton.Name, pFunction)) { if (tempToolButton.Name != "tsbtnAdaptive" && tempToolButton.Name != "tsbtnClose") tempToolButton.Visible = GetUserRightDataState(pFormName, tempToolButton.Name, pUserRightData); } } } #endregion } else { if (control is C_DataGridView) { SetCDataGridViewStyle(control); //设置C_DataGridView的样式 } else if (control is C_Button) { SetCButtonStyle(control); //设置C_Button的通用样式 } if (GetFormIsPermission(pFormName, control.Name, pFunction)) { control.Visible = GetUserRightDataState(pFormName, control.Name, pUserRightData); } } return; } else { if (control.Controls == null || control.Controls.Count == 0) { return; } foreach (Control item in control.Controls) { //设置控件是否可用 FormPermissionControl(pFormName, item, pUserRightData, pFunction); } } } #region 私有方法 /// /// 判断用户功能权限表中是否包括该权限 /// /// 当前窗体名称 /// 当前控件名称 /// 用户功能权限列表 /// True有权限使用 False表示没有权限使用 private static bool GetUserRightDataState(string pFormName, string pName, DataTable pUserRightData) { string strWhere = "FormName = '" + pFormName + "' And ButtonName = '" + pName + "'"; DataRow[] newDataRows = pUserRightData.Select(strWhere); if (newDataRows != null && newDataRows.Count() > 0) { return true; } return false; } /// /// 根据控件名称获取该控件是否需要进行功能控制 /// /// /// /// /// 返回True表示需要控制 False表示不需要控制 private static bool GetFormIsPermission(string pFormName, string pName, DataTable pFunction) { string strWhere = "FormName = '" + pFormName + "' And ButtonName = '" + pName + "'"; DataRow[] newDataRows = pFunction.Select(strWhere); if (newDataRows != null && newDataRows.Count() > 0) { return true; } return false; } /// /// 设置CToolStrip的通用样式 /// /// 控件 private static void SetCToolStripStyle(Control pControl) { if (pControl is C_ToolStrip) { C_ToolStrip pToolStrip = pControl as C_ToolStrip; pToolStrip.BackgroundImage = StyleImage.functionbackground; pToolStrip.Height = 35; pToolStrip.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } } /// /// 设置ToolStripItem的通用样式 /// /// 控件 private static void SetToolStripButtonStyle(ToolStripItem pControl) { if (pControl is ToolStripButton) { ToolStripButton tempToolButton = pControl as ToolStripButton; tempToolButton.AutoSize = false; tempToolButton.Height = 25; tempToolButton.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } else if (pControl is ToolStripSeparator) { ToolStripSeparator tempToolStripSeparator = pControl as ToolStripSeparator; tempToolStripSeparator.AutoSize = false; tempToolStripSeparator.Size = new System.Drawing.Size(6, 25); } } /// /// 设置C_Button的通用样式 /// /// 控件 private static void SetCButtonStyle(Control pControl) { if (pControl is C_Button) { C_Button pButton = pControl as C_Button; pButton.Height = 30; pButton.Width = 85; pButton.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); } } /// /// 设置C_DataGridView的样式 /// /// private static void SetCDataGridViewStyle(Control pControl) { if (pControl is C_DataGridView) { C_DataGridView pDataGridView = pControl as C_DataGridView; pDataGridView.ColumnHeadersHeight = 23; pDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; pDataGridView.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(235))))); pDataGridView.AutoGenerateColumns = false; } } #endregion } }