RoleEditView.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using Aitex.Core.Account;
  9. namespace MECF.Framework.UI.Core.Accounts
  10. {
  11. /// <summary>
  12. /// Interaction logic for RoleEditView.xaml
  13. /// </summary>
  14. public partial class RoleEditView : Window
  15. {
  16. public RoleEditView()
  17. {
  18. InitializeComponent();
  19. Loaded += new RoutedEventHandler(RoleEditView_Loaded);
  20. dataGrid1.CanUserAddRows = false;
  21. dataGrid1.RowHeight = 25;
  22. }
  23. public class PermissionClass
  24. {
  25. public static Dictionary<Permission_CN, string> ViewPermissionCheck = new Dictionary<Permission_CN, string>();
  26. static IEnumerable<Permission_CN> permissions = Enum.GetValues(typeof(Permission_CN)).Cast<Permission_CN>();
  27. static PermissionClass()
  28. {
  29. foreach (Permission_CN p in permissions)
  30. {
  31. if (p == Permission_CN.ProcessHidden)
  32. {
  33. //只针对ReactorProcess相关界面
  34. ViewPermissionCheck.Add(p, "RecipeMonitor"); //"ProcessView"
  35. }
  36. else
  37. {
  38. ViewPermissionCheck.Add(p, "");
  39. }
  40. }
  41. }
  42. public static List<Permission_CN> GetPermissions(string viewName)
  43. {
  44. List<Permission_CN> permissionInfos = new List<Permission_CN>();
  45. foreach (Permission_CN p in ViewPermissionCheck.Keys)
  46. {
  47. if (ViewPermissionCheck[p] == "")
  48. permissionInfos.Add(p);
  49. else if(viewName.IndexOf(ViewPermissionCheck[p])>=0)
  50. permissionInfos.Add(p);
  51. }
  52. return permissionInfos;
  53. }
  54. }
  55. public enum Permission_CN
  56. {
  57. Hidden,
  58. ReadOnly,
  59. Partial,
  60. Full,
  61. ProcessHidden
  62. }
  63. public List<ExpandoObject> DataSource = new List<ExpandoObject>();
  64. void RoleEditView_Loaded(object sender, RoutedEventArgs e)
  65. {
  66. var viewList = AccountClient.Instance.Service.GetAllViewList();
  67. var viewPermission = AccountClient.Instance.Service.GetAllRolesPermission();
  68. //Dynamically do data binding function here
  69. foreach (var role in viewPermission.Keys)
  70. {
  71. dynamic singleViewPermission = new ExpandoObject();
  72. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>("RoleName", role));
  73. foreach (var view in viewPermission[role].Keys)
  74. {
  75. switch (viewPermission[role][view])
  76. {
  77. case ViewPermission.Invisiable:
  78. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>(view, Permission_CN.Hidden));
  79. break;
  80. case ViewPermission.Readonly:
  81. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>(view, Permission_CN.ReadOnly));
  82. break;
  83. case ViewPermission.PartlyControl:
  84. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>(view, Permission_CN.Partial));
  85. break;
  86. case ViewPermission.FullyControl:
  87. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>(view, Permission_CN.Full));
  88. break;
  89. case ViewPermission.ProcessOPControl:
  90. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>(view, Permission_CN.ProcessHidden));
  91. break;
  92. }
  93. }
  94. DataSource.Add(singleViewPermission);
  95. }
  96. dataGrid1.DataContext = DataSource;
  97. //dynamically creating data grid column
  98. foreach (var view in viewList)
  99. {
  100. var datagGridColumn = new DataGridComboBoxColumn() { Header = view.Value, Width = 100 };
  101. dataGrid1.Columns.Add(datagGridColumn);
  102. datagGridColumn.SelectedItemBinding = new Binding() { Path = new PropertyPath(view.Key) };
  103. datagGridColumn.ItemsSource = PermissionClass.GetPermissions(view.Key);
  104. }
  105. }
  106. private void btnClose_Click(object sender, RoutedEventArgs e)
  107. {
  108. Close();
  109. }
  110. private void btnAddNew_Click(object sender, RoutedEventArgs e)
  111. {
  112. dynamic singleViewPermission = new ExpandoObject();
  113. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>("RoleName", "New Role"));
  114. var viewList = AccountClient.Instance.Service.GetAllViewList();
  115. foreach (var view in viewList.Keys)
  116. {
  117. ((IDictionary<String, object>)singleViewPermission).Add(new KeyValuePair<string, object>(view, Permission_CN.Hidden));
  118. }
  119. DataSource.Add(singleViewPermission);
  120. dataGrid1.DataContext = null;
  121. dataGrid1.DataContext = DataSource;
  122. }
  123. /// <summary>
  124. /// Save role to xml file
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. private void btnSave_Click(object sender, RoutedEventArgs e)
  129. {
  130. for (int i = 0; i < DataSource.Count - 1; i++)
  131. {
  132. for (int j = i + 1; j < DataSource.Count; j++)
  133. {
  134. dynamic obj1 = DataSource[i];
  135. dynamic obj2 = DataSource[j];
  136. if (String.Compare(obj1.RoleName, obj2.RoleName) == 0)
  137. {
  138. MessageBox.Show(string.Format("\"{0}\" Existed,modify to save!", obj1.RoleName), "Role Editor Error", MessageBoxButton.OK, MessageBoxImage.Warning);
  139. return;
  140. }
  141. }
  142. }
  143. var permissionDic = new Dictionary<string, Dictionary<string,ViewPermission>>();
  144. foreach (dynamic role in DataSource)
  145. {
  146. permissionDic.Add(role.RoleName, new Dictionary<string, ViewPermission>());
  147. foreach (var item in ((IDictionary<String, object>)role).Keys)
  148. {
  149. if (item == "RoleName") continue;
  150. var perm = (Permission_CN)((IDictionary<String, object>)role)[item];
  151. switch (perm)
  152. {
  153. case Permission_CN.Full:
  154. permissionDic[role.RoleName].Add(item, ViewPermission.FullyControl);
  155. break;
  156. case Permission_CN.Partial:
  157. permissionDic[role.RoleName].Add(item, ViewPermission.PartlyControl);
  158. break;
  159. case Permission_CN.ReadOnly:
  160. permissionDic[role.RoleName].Add(item, ViewPermission.Readonly);
  161. break;
  162. case Permission_CN.Hidden:
  163. permissionDic[role.RoleName].Add(item, ViewPermission.Invisiable);
  164. break;
  165. case Permission_CN.ProcessHidden:
  166. permissionDic[role.RoleName].Add(item, ViewPermission.ProcessOPControl);
  167. break;
  168. }
  169. }
  170. }
  171. if (AccountClient.Instance.Service.SaveAllRolesPermission(permissionDic))
  172. {
  173. MessageBox.Show(Application.Current.Resources["GlobalLableAccountViewSaveRoleOk"].ToString());
  174. }
  175. else
  176. {
  177. MessageBox.Show(Application.Current.Resources["GlobalLableAccountViewSaveRoleFailed"].ToString());
  178. }
  179. }
  180. /// <summary>
  181. /// Delete current selecte role
  182. /// </summary>
  183. /// <param name="sender"></param>
  184. /// <param name="e"></param>
  185. private void btnRoleDelete_Click(object sender, RoutedEventArgs e)
  186. {
  187. dynamic obj = dataGrid1.SelectedItem;
  188. if (obj != null)
  189. {
  190. if (MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableAccountViewDeleteRoleInfo"].ToString(), obj.RoleName), Application.Current.Resources["GlobalLableAccountViewMsgTitle"].ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  191. {
  192. foreach (dynamic role in DataSource)
  193. {
  194. if (role.RoleName == obj.RoleName)
  195. {
  196. DataSource.Remove(role);
  197. break;
  198. }
  199. }
  200. dataGrid1.DataContext = null;
  201. dataGrid1.DataContext = DataSource;
  202. }
  203. }
  204. }
  205. }
  206. }