RoleEditView.xaml.cs 9.7 KB

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