AccountViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Security.Policy;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Xml;
  9. using Aitex.Core.Account;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.UI.MVVM;
  12. using Aitex.Core.Util;
  13. namespace MECF.Framework.UI.Core.Accounts
  14. {
  15. public class AccountViewModel
  16. {
  17. public class AccountInfo
  18. {
  19. public Account Account { get; set; }
  20. public int No { get; set; }
  21. public string AccountId { get { return Account.AccountId; } }
  22. public bool IsEnabled { get { return Account.AccountStatus; } }
  23. public string RealName { get { return Account.RealName; } }
  24. public string Role { get { return Account.Role; } }
  25. public string Department { get { return Account.Department; } }
  26. public string LastLoginTime { get { return Account.LastLoginTime; } }
  27. public string Description { get { return Account.Description; } }
  28. public string Email { get { return Account.Email; } }
  29. public string Telephone { get { return Account.Telephone; } }
  30. }
  31. public List<AccountInfo> AccountList { get; private set; }
  32. /// <summary>
  33. /// 总共创建的账号数目
  34. /// </summary>
  35. public int TotalAccountNum
  36. {
  37. get
  38. {
  39. return AccountList.Count;
  40. }
  41. }
  42. /// <summary>
  43. /// 账号有效的用户数
  44. /// </summary>
  45. public int EnabledAccountNum
  46. {
  47. get
  48. {
  49. int num = 0;
  50. foreach (var item in AccountList)
  51. {
  52. if (item.IsEnabled) num++;
  53. }
  54. return num;
  55. }
  56. }
  57. /// <summary>
  58. /// Construction
  59. /// </summary>
  60. /// <param name="hideDisabledAccounts"></param>
  61. public AccountViewModel(bool hideDisabledAccounts)
  62. {
  63. AccountList = new List<AccountInfo>();
  64. var accounts = AccountClient.Instance.Service.GetAccountList();
  65. if (accounts == null) return;
  66. int num = 1;
  67. foreach (var account in accounts.AccountList)
  68. {
  69. if (!account.AccountStatus && hideDisabledAccounts) continue;
  70. AccountList.Add(new AccountInfo() { Account = account, No = num++ });
  71. }
  72. }
  73. }
  74. public class RolePermissionViewModel : INotifyPropertyChanged
  75. {
  76. #region Command
  77. public DelegateCommand<object> SavePermissionCommand { get; set; }
  78. #endregion
  79. public SerializableDictionary<string, SerializableDictionary<string, ViewPermission>> Roles
  80. {
  81. get;
  82. set;
  83. }
  84. ObservableCollection<string> roleNames = new ObservableCollection<string>();
  85. public ObservableCollection<string> RoleNames
  86. {
  87. get
  88. {
  89. return roleNames;
  90. }
  91. set
  92. {
  93. roleNames = value;
  94. }
  95. }
  96. public RolePermissionViewModel()
  97. {
  98. //cs 设置
  99. loadPermissionFile();
  100. Roles = AccountClient.Instance.Service.GetAllRolesPermission();
  101. RoleNames.Clear();
  102. foreach (string rolename in Roles.Keys)
  103. {
  104. RoleNames.Add(rolename);
  105. }
  106. }
  107. public void SelectRoleChanged(string role)
  108. {
  109. CurrentRoleName = role;
  110. BindAll();
  111. }
  112. public void InitialRolePermissionVM()
  113. {
  114. SavePermissionCommand = new DelegateCommand<object>(
  115. param =>
  116. {
  117. try
  118. {
  119. //_xmlRecipeFormat.Save(recipePermissionFile);
  120. bool suc = AccountClient.Instance.Service.SaveProcessViewPermission(_xmlRecipeFormat.InnerXml);
  121. if (suc)
  122. MessageBox.Show("保存菜单查看权限成功");
  123. //Publisher.Notify(Subject.SendSaveSuccessMessage, "保存菜单查看权限成功");
  124. }
  125. catch (Exception ex)
  126. {
  127. LOG.Write(ex);
  128. MessageBox.Show("保存菜单查看权限失败!");
  129. //Publisher.Notify(Subject.SendWarningMessage, "保存菜单查看权限失败!");
  130. }
  131. },
  132. param => { if (string.IsNullOrEmpty(CurrentRoleName))return false; return true; });
  133. }
  134. public CheckTreeViewModel ChamberAViewModel { get; set; }
  135. //public CheckTreeViewModel ChamberBViewModel { get; set; }
  136. //public CheckTreeViewModel ChamberCViewModel { get; set; }
  137. //public CheckTreeViewModel ChamberDViewModel { get; set; }
  138. public string CurrentRoleName { get; set; }
  139. #region UI Logical
  140. public
  141. void loadPermissionFile()
  142. {
  143. ChamberAViewModel = new CheckTreeViewModel();
  144. }
  145. public static XmlDocument _xmlRecipeFormat = new XmlDocument();
  146. public void BindAll()
  147. {
  148. }
  149. #endregion
  150. #region INotifyPropertyChanged Members
  151. public event PropertyChangedEventHandler PropertyChanged;
  152. protected virtual void OnPropertyChanged(string proName)
  153. {
  154. if (PropertyChanged != null)
  155. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  156. }
  157. #endregion
  158. }
  159. public class CheckTreeViewModel : INotifyPropertyChanged
  160. {
  161. public NodeViewModel Nodes { get; private set; }
  162. public NodeInfo NodeInfo { get; private set; }
  163. public CheckTreeViewModel()
  164. {
  165. NodeInfo = new NodeInfo();
  166. NodeInfo.SelectedNodeChanged += (s, e) => RefreshCommands();
  167. Nodes = new NodeViewModel(NodeInfo);
  168. }
  169. NodeViewModel GetOperationNode()
  170. {
  171. if (NodeInfo.SelectedNode == null)
  172. return Nodes;
  173. return NodeInfo.SelectedNode;
  174. }
  175. bool CheckSelection(object obj)
  176. {
  177. return NodeInfo.SelectedNode != null;
  178. }
  179. void RefreshCommands()
  180. {
  181. }
  182. #region INotifyPropertyChanged Members
  183. public event PropertyChangedEventHandler PropertyChanged;
  184. protected virtual void OnPropertyChanged(string proName)
  185. {
  186. if (PropertyChanged != null)
  187. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  188. }
  189. #endregion
  190. }
  191. public class NodeInfo : INotifyPropertyChanged
  192. {
  193. NodeViewModel selectedNode;
  194. int count;
  195. public NodeViewModel SelectedNode
  196. {
  197. get { return selectedNode; }
  198. set
  199. {
  200. if (selectedNode != value)
  201. {
  202. selectedNode = value;
  203. OnSelectedNodeChanged();
  204. OnPropertyChanged("SelectedNode");
  205. }
  206. }
  207. }
  208. public int Count
  209. {
  210. get { return count; }
  211. private set
  212. {
  213. if (count != value)
  214. {
  215. count = value;
  216. OnPropertyChanged("Count");
  217. }
  218. }
  219. }
  220. internal void SetCount(int newcount)
  221. {
  222. Count = newcount;
  223. }
  224. public event EventHandler SelectedNodeChanged;
  225. protected virtual void OnSelectedNodeChanged()
  226. {
  227. if (SelectedNodeChanged != null)
  228. SelectedNodeChanged(this, EventArgs.Empty);
  229. }
  230. #region INotifyPropertyChanged Members
  231. public event PropertyChangedEventHandler PropertyChanged;
  232. protected virtual void OnPropertyChanged(string proName)
  233. {
  234. if (PropertyChanged != null)
  235. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  236. }
  237. #endregion
  238. }
  239. public class NodeViewModel : INotifyPropertyChanged
  240. {
  241. /*
  242. * 省去构造函数,公共方法和一些静态成员
  243. * */
  244. const string DataName = "数据 ";
  245. public XmlNode NodeXmlData { get; set; }
  246. public string CurrentRoleName { get; set; }
  247. static int DataCounter = 1;
  248. public static string GetNextDataName()
  249. {
  250. return String.Concat(DataName, DataCounter++);
  251. }
  252. public NodeViewModel(NodeInfo info)
  253. {
  254. children = new ObservableCollection<NodeViewModel>();
  255. NodeInfo = info;
  256. CheckBoxCommand = new DelegateCommand<object>((p) => checkBoxSelect(p), param => { return true; });
  257. }
  258. private NodeViewModel(NodeViewModel parent, string name, string role)
  259. : this(parent, name, false, null, role)
  260. {
  261. }
  262. private NodeViewModel(NodeViewModel parent, string name, bool isSelected, XmlNode node, string role)
  263. {
  264. Parent = parent;
  265. Name = name;
  266. NodeInfo = parent.NodeInfo;
  267. IsSelected = isSelected;
  268. children = new ObservableCollection<NodeViewModel>();
  269. SelectedNode = node;
  270. CheckBoxCommand = new DelegateCommand<object>((p) => checkBoxSelect(p), param =>
  271. {
  272. return true;
  273. });
  274. CurrentRoleName = role;
  275. }
  276. public XmlNode SelectedNode = null;
  277. void checkBoxSelect(object p)
  278. {
  279. if (SelectedNode != null)
  280. {
  281. CheckBox cb = p as CheckBox;
  282. if (null != SelectedNode.Attributes["DenyRole"])
  283. {
  284. string denyroles = "," + SelectedNode.Attributes["DenyRole"].Value + ",";
  285. string tagrole = "," + CurrentRoleName + ",";
  286. if (cb.IsChecked.Value)
  287. {
  288. denyroles = denyroles.Replace(tagrole, ",");
  289. SelectedNode.Attributes["DenyRole"].Value = denyroles.Trim(","[0]);
  290. }
  291. else
  292. {
  293. //没权限
  294. if (denyroles.IndexOf(tagrole) < 0)
  295. {
  296. //本来有权限,现在要没权限
  297. SelectedNode.Attributes["DenyRole"].Value = (denyroles + CurrentRoleName).Trim(","[0]);
  298. }
  299. }
  300. }
  301. else
  302. {
  303. if (cb.IsChecked.Value)
  304. {
  305. }
  306. else
  307. {
  308. XmlAttribute xmlAttribute = RolePermissionViewModel._xmlRecipeFormat.CreateAttribute("DenyRole");
  309. xmlAttribute.Value = CurrentRoleName;
  310. SelectedNode.Attributes.Append(xmlAttribute);
  311. }
  312. }
  313. //SelectedNode.Attributes["DenyRole"].Value = cb.IsChecked + "";
  314. }
  315. }
  316. #region 字段
  317. string name;
  318. bool isExpanded;
  319. bool isSelected;
  320. ObservableCollection<NodeViewModel> children;
  321. #endregion
  322. #region 属性
  323. public DelegateCommand<object> CheckBoxCommand { get; private set; }
  324. public NodeInfo NodeInfo { get; private set; }
  325. public NodeViewModel Parent { get; private set; }
  326. public string Name
  327. {
  328. get
  329. {
  330. return name;
  331. }
  332. set
  333. {
  334. if (name != value)
  335. {
  336. name = value;
  337. OnPropertyChanged("Name");
  338. }
  339. }
  340. }
  341. public bool IsExpanded
  342. {
  343. get
  344. {
  345. return isExpanded;
  346. }
  347. set
  348. {
  349. if (isExpanded != value)
  350. {
  351. isExpanded = value;
  352. OnPropertyChanged("IsExpanded");
  353. OnIsExpandedChanged();
  354. }
  355. }
  356. }
  357. public bool IsSelected
  358. {
  359. get { return isSelected; }
  360. set
  361. {
  362. if (isSelected != value)
  363. {
  364. isSelected = value;
  365. OnPropertyChanged("IsSelected");
  366. OnIsSelectedChanged();
  367. }
  368. }
  369. }
  370. public ReadOnlyObservableCollection<NodeViewModel> Children
  371. {
  372. get
  373. {
  374. return new ReadOnlyObservableCollection<NodeViewModel>(children);
  375. }
  376. }
  377. #endregion
  378. #region 方法
  379. public void Remove()
  380. {
  381. //if (Parent != null)
  382. //{
  383. // Parent.children.Remove(this);
  384. // RefreshInfoCount(-1);
  385. //}
  386. children.Clear();
  387. RefreshInfoCount(-1);
  388. }
  389. public NodeViewModel Add(string name, XmlNode node, string role)
  390. {
  391. return Add(name, false, node, role);
  392. }
  393. public NodeViewModel Add(string name, bool ischecked, XmlNode node, string role)
  394. {
  395. //SelectedNode = node;
  396. IsExpanded = true;
  397. NodeViewModel nodeModel = new NodeViewModel(this, name, ischecked, node, role);
  398. children.Insert(0, nodeModel);
  399. RefreshInfoCount(1);
  400. return nodeModel;
  401. }
  402. public void Append(string name, string role)
  403. {
  404. IsExpanded = true;
  405. children.Add(new NodeViewModel(this, name, role));
  406. RefreshInfoCount(1);
  407. }
  408. public void Rename()
  409. {
  410. Name = GetNextDataName();
  411. }
  412. public void MoveUp()
  413. {
  414. if (Parent != null)
  415. {
  416. var idx = Parent.children.IndexOf(this);
  417. if (idx > 0)
  418. {
  419. Parent.children.RemoveAt(idx);
  420. Parent.children.Insert(--idx, this);
  421. }
  422. IsSelected = true;
  423. }
  424. }
  425. public void MoveDown()
  426. {
  427. if (Parent != null)
  428. {
  429. var idx = Parent.children.IndexOf(this);
  430. if (idx < Parent.children.Count - 1)
  431. {
  432. Parent.children.RemoveAt(idx);
  433. Parent.children.Insert(++idx, this);
  434. }
  435. IsSelected = true;
  436. }
  437. }
  438. #endregion
  439. #region 私有方法
  440. void RefreshInfoCount(int addition)
  441. {
  442. if (NodeInfo != null)
  443. NodeInfo.SetCount(NodeInfo.Count + addition);
  444. }
  445. #endregion
  446. #region 事件
  447. public event EventHandler IsExpandedChanged;
  448. public event EventHandler IsSelectedChanged;
  449. protected virtual void OnIsExpandedChanged()
  450. {
  451. if (IsExpandedChanged != null)
  452. IsExpandedChanged(this, EventArgs.Empty);
  453. }
  454. protected virtual void OnIsSelectedChanged()
  455. {
  456. if (IsSelectedChanged != null)
  457. IsSelectedChanged(this, EventArgs.Empty);
  458. if (IsSelected)
  459. NodeInfo.SelectedNode = this;
  460. }
  461. #endregion
  462. #region INotifyPropertyChanged Members
  463. public event PropertyChangedEventHandler PropertyChanged;
  464. protected virtual void OnPropertyChanged(string proName)
  465. {
  466. if (PropertyChanged != null)
  467. PropertyChanged(this, new PropertyChangedEventArgs(proName));
  468. }
  469. #endregion
  470. }
  471. }