| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- /*******************************************************************************
- * 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
- {
- /// <summary>
- /// 页面功能权限控制
- /// 需要将页面上需要控制的控件的Visible属性设置为False,当该用户有权限后会直接设置为True
- /// </summary>
- public class FormPermissionManager
- {
- /// <summary>
- /// 设置页面控件的权限设置Visible的值 如果用户有权限设置为TURE,其他情况不处理
- /// </summary>
- /// <param name="pFormName">当前窗体名称</param>
- /// <param name="control">当前控件名称</param>
- /// <param name="pUserRightData">用户功能权限列表</param>
- /// <param name="pFunction">功能权限列表</param>
- 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 私有方法
- /// <summary>
- /// 判断用户功能权限表中是否包括该权限
- /// </summary>
- /// <param name="pFormName">当前窗体名称</param>
- /// <param name="pName">当前控件名称</param>
- /// <param name="pUserRightData">用户功能权限列表</param>
- /// <returns>True有权限使用 False表示没有权限使用</returns>
- 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;
- }
- /// <summary>
- /// 根据控件名称获取该控件是否需要进行功能控制
- /// </summary>
- /// <param name="pFormName"></param>
- /// <param name="pName"></param>
- /// <param name="pFunction"></param>
- /// <returns>返回True表示需要控制 False表示不需要控制</returns>
- 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;
- }
- /// <summary>
- /// 设置CToolStrip的通用样式
- /// </summary>
- /// <param name="pControl">控件</param>
- 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)));
- }
- }
- /// <summary>
- /// 设置ToolStripItem的通用样式
- /// </summary>
- /// <param name="pControl">控件</param>
- 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);
- }
- }
- /// <summary>
- /// 设置C_Button的通用样式
- /// </summary>
- /// <param name="pControl">控件</param>
- 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)));
- }
- }
- /// <summary>
- /// 设置C_DataGridView的样式
- /// </summary>
- /// <param name="pControl"></param>
- 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
- }
- }
|