FormPermissionManager.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*******************************************************************************
  2. * Copyright(c) 2014 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:FormPermissionManager.cs
  5. * 2.功能描述:页面功能权限控制
  6. * 需要将页面上需要控制的控件的Visible属性设置为False,当该用户有权限后会直接设置为True
  7. * 编辑履历:
  8. * 作者 日期 版本 修改内容
  9. * 张国印 2014/9/18 1.00 新建
  10. *******************************************************************************/
  11. using System.Data;
  12. using System.Linq;
  13. using System.Windows.Forms;
  14. using Dongke.IBOSS.PRD.Basics.BaseControls;
  15. using Dongke.IBOSS.PRD.Basics.BaseResources;
  16. namespace Dongke.IBOSS.PRD.Client.CommonModule
  17. {
  18. /// <summary>
  19. /// 页面功能权限控制
  20. /// 需要将页面上需要控制的控件的Visible属性设置为False,当该用户有权限后会直接设置为True
  21. /// </summary>
  22. public class FormPermissionManager
  23. {
  24. /// <summary>
  25. /// 设置页面控件的权限设置Visible的值 如果用户有权限设置为TURE,其他情况不处理
  26. /// </summary>
  27. /// <param name="pFormName">当前窗体名称</param>
  28. /// <param name="control">当前控件名称</param>
  29. /// <param name="pUserRightData">用户功能权限列表</param>
  30. /// <param name="pFunction">功能权限列表</param>
  31. public static void FormPermissionControl(string pFormName, Control control, DataTable pUserRightData, DataTable pFunction)
  32. {
  33. if (control == null || string.IsNullOrEmpty(pFormName) || pUserRightData == null
  34. || pUserRightData.Rows.Count <= 0 || pFunction == null || pFunction.Rows.Count <= 0)
  35. {
  36. return;
  37. }
  38. if (control is IDKControl || control is ToolStrip)
  39. {
  40. //if (control is C_ToolStrip)
  41. if (control is ToolStrip)
  42. {
  43. #region 设置C_ToolStrip的可用状态
  44. //C_ToolStrip tempToolStrip = control as C_ToolStrip;
  45. ToolStrip tempToolStrip = control as ToolStrip;
  46. SetCToolStripStyle(tempToolStrip); // 设置CToolStrip的通用样式
  47. foreach (ToolStripItem tempStrip in tempToolStrip.Items)
  48. {
  49. SetToolStripButtonStyle(tempStrip); //设置ToolStripItem的通用样式
  50. if (tempStrip is ToolStripButton)
  51. {
  52. ToolStripButton tempToolButton = tempStrip as ToolStripButton;
  53. if (tempToolButton.Tag != null && tempToolButton.Tag.ToString() == "[ALL]")
  54. {
  55. continue;
  56. }
  57. if (GetFormIsPermission(pFormName, tempToolButton.Name, pFunction))
  58. {
  59. if (tempToolButton.Name != "tsbtnAdaptive" && tempToolButton.Name != "tsbtnClose")
  60. tempToolButton.Visible = GetUserRightDataState(pFormName, tempToolButton.Name, pUserRightData);
  61. }
  62. }
  63. }
  64. #endregion
  65. }
  66. else
  67. {
  68. if (control is C_DataGridView)
  69. {
  70. SetCDataGridViewStyle(control); //设置C_DataGridView的样式
  71. }
  72. else if (control is C_Button)
  73. {
  74. SetCButtonStyle(control); //设置C_Button的通用样式
  75. }
  76. if (GetFormIsPermission(pFormName, control.Name, pFunction))
  77. {
  78. control.Visible = GetUserRightDataState(pFormName, control.Name, pUserRightData);
  79. }
  80. }
  81. return;
  82. }
  83. else
  84. {
  85. if (control.Controls == null || control.Controls.Count == 0)
  86. {
  87. return;
  88. }
  89. foreach (Control item in control.Controls)
  90. {
  91. //设置控件是否可用
  92. FormPermissionControl(pFormName, item, pUserRightData, pFunction);
  93. }
  94. }
  95. }
  96. #region 私有方法
  97. /// <summary>
  98. /// 判断用户功能权限表中是否包括该权限
  99. /// </summary>
  100. /// <param name="pFormName">当前窗体名称</param>
  101. /// <param name="pName">当前控件名称</param>
  102. /// <param name="pUserRightData">用户功能权限列表</param>
  103. /// <returns>True有权限使用 False表示没有权限使用</returns>
  104. private static bool GetUserRightDataState(string pFormName, string pName, DataTable pUserRightData)
  105. {
  106. string strWhere = "FormName = '" + pFormName + "' And ButtonName = '" + pName + "'";
  107. DataRow[] newDataRows = pUserRightData.Select(strWhere);
  108. if (newDataRows != null && newDataRows.Count() > 0)
  109. {
  110. return true;
  111. }
  112. return false;
  113. }
  114. /// <summary>
  115. /// 根据控件名称获取该控件是否需要进行功能控制
  116. /// </summary>
  117. /// <param name="pFormName"></param>
  118. /// <param name="pName"></param>
  119. /// <param name="pFunction"></param>
  120. /// <returns>返回True表示需要控制 False表示不需要控制</returns>
  121. private static bool GetFormIsPermission(string pFormName, string pName, DataTable pFunction)
  122. {
  123. string strWhere = "FormName = '" + pFormName + "' And ButtonName = '" + pName + "'";
  124. DataRow[] newDataRows = pFunction.Select(strWhere);
  125. if (newDataRows != null && newDataRows.Count() > 0)
  126. {
  127. return true;
  128. }
  129. return false;
  130. }
  131. /// <summary>
  132. /// 设置CToolStrip的通用样式
  133. /// </summary>
  134. /// <param name="pControl">控件</param>
  135. private static void SetCToolStripStyle(Control pControl)
  136. {
  137. if (pControl is C_ToolStrip)
  138. {
  139. C_ToolStrip pToolStrip = pControl as C_ToolStrip;
  140. pToolStrip.BackgroundImage = StyleImage.functionbackground;
  141. pToolStrip.Height = 35;
  142. pToolStrip.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  143. }
  144. }
  145. /// <summary>
  146. /// 设置ToolStripItem的通用样式
  147. /// </summary>
  148. /// <param name="pControl">控件</param>
  149. private static void SetToolStripButtonStyle(ToolStripItem pControl)
  150. {
  151. if (pControl is ToolStripButton)
  152. {
  153. ToolStripButton tempToolButton = pControl as ToolStripButton;
  154. tempToolButton.AutoSize = false;
  155. tempToolButton.Height = 25;
  156. tempToolButton.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  157. }
  158. else if (pControl is ToolStripSeparator)
  159. {
  160. ToolStripSeparator tempToolStripSeparator = pControl as ToolStripSeparator;
  161. tempToolStripSeparator.AutoSize = false;
  162. tempToolStripSeparator.Size = new System.Drawing.Size(6, 25);
  163. }
  164. }
  165. /// <summary>
  166. /// 设置C_Button的通用样式
  167. /// </summary>
  168. /// <param name="pControl">控件</param>
  169. private static void SetCButtonStyle(Control pControl)
  170. {
  171. if (pControl is C_Button)
  172. {
  173. C_Button pButton = pControl as C_Button;
  174. pButton.Height = 30;
  175. pButton.Width = 85;
  176. pButton.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  177. }
  178. }
  179. /// <summary>
  180. /// 设置C_DataGridView的样式
  181. /// </summary>
  182. /// <param name="pControl"></param>
  183. private static void SetCDataGridViewStyle(Control pControl)
  184. {
  185. if (pControl is C_DataGridView)
  186. {
  187. C_DataGridView pDataGridView = pControl as C_DataGridView;
  188. pDataGridView.ColumnHeadersHeight = 23;
  189. pDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
  190. pDataGridView.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))),
  191. ((int)(((byte)(235)))), ((int)(((byte)(235)))));
  192. pDataGridView.AutoGenerateColumns = false;
  193. }
  194. }
  195. #endregion
  196. }
  197. }