RoleManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using Aitex.Core.RT.Log;
  7. using MECF.Framework.Common.Account.Extends;
  8. using OpenSEMI.ClientBase;
  9. namespace Virgo_DUI.Client.Models.Utility.RolePage
  10. {
  11. public class RoleManager
  12. {
  13. private RoleManager()
  14. {
  15. }
  16. public bool Initialize()
  17. {
  18. _RoleContainer.Clear();
  19. List<Role> roles = RoleAccountProvider.Instance.GetRoles();
  20. if (roles == null)
  21. {
  22. LOG.Error("GetRoles method failed,in RoleManager");
  23. return false;
  24. }
  25. List<AppMenu> menus = ClientApp.Instance.MenuLoader.MenuList;
  26. this._listMenuItems = menus;
  27. if (menus == null)
  28. {
  29. LOG.Error("GetMenus method failed,in RoleManager");
  30. return false;
  31. }
  32. foreach (Role r in roles)
  33. {
  34. RoleItem role = new RoleItem(r);
  35. role.DisplayRoleName = role.RoleName;
  36. role.DisplayAutoLogoutTime = role.AutoLogoutTime;
  37. role.DisplayIsAutoLogout = role.IsAutoLogout;
  38. //Parse menu permission
  39. MenuPermission menuPermissionParse = new MenuPermission();
  40. menuPermissionParse.ParsePermission(r.MenuPermission);
  41. MenuInfo mp = new MenuInfo() { ID = "Header" };
  42. mp.Name = "Header";
  43. mp.EnumPermission = menuPermissionParse.GetPermissionByPageID("Header");
  44. role.AddMenuInfo(mp);
  45. //Get Menu information
  46. foreach (AppMenu topMenuItem in menus)
  47. {
  48. foreach (AppMenu subMenuItem in topMenuItem.MenuItems)
  49. {
  50. mp = new MenuInfo() { ID = subMenuItem.MenuID };
  51. if (subMenuItem.System == "PMA")
  52. {
  53. mp.Name = "PMA " + subMenuItem.ResKey;
  54. }
  55. else if (subMenuItem.System == "PMB")
  56. {
  57. mp.Name = "PMB " + subMenuItem.ResKey;
  58. }
  59. else
  60. {
  61. mp.Name = subMenuItem.ResKey; //Application.Current.FindResource(subMenuItem.ResKey) as string;
  62. }
  63. mp.EnumPermission = menuPermissionParse.GetPermissionByPageID(subMenuItem.MenuID);
  64. role.AddMenuInfo(mp);
  65. }
  66. }
  67. _RoleContainer.Add(role);
  68. }
  69. return true;
  70. }
  71. public List<RoleItem> GetAllRoles()
  72. {
  73. return _RoleContainer.ToList();
  74. }
  75. public bool AddRole(RoleItem r)
  76. {
  77. RoleItem existRole = GetRoleByName(r.RoleName);
  78. if (existRole != null)
  79. {
  80. LOG.Info("the role already exists, in RoleManager");
  81. return false;
  82. }
  83. existRole = GetRoleByID(r.RoleID);
  84. if (existRole != null)
  85. {
  86. LOG.Info("the role already exists, in RoleManager");
  87. return false;
  88. }
  89. _RoleContainer.Add(r);
  90. return true;
  91. }
  92. public string GenerateRoleID()
  93. {
  94. RoleItem r = GetRoleByID(_RoleNum.ToString());
  95. while (r != null)
  96. {
  97. _RoleNum++;
  98. r = GetRoleByID(_RoleNum.ToString());
  99. }
  100. return _RoleNum.ToString();
  101. }
  102. public RoleItem GetRoleByID(string ID)
  103. {
  104. return _RoleContainer.FirstOrDefault(t => t.RoleID == ID);
  105. }
  106. public RoleItem GetRoleByName(string name)
  107. {
  108. return _RoleContainer.FirstOrDefault(t => t.RoleName == name);
  109. }
  110. public RoleItem CreateRole()
  111. {
  112. RoleItem r = new RoleItem(GenerateRoleID());
  113. r.DisplayRoleName = r.RoleName = "No Name";
  114. r.DisplayAutoLogoutTime = r.AutoLogoutTime = 10;
  115. r.DisplayIsAutoLogout = r.IsAutoLogout = false;
  116. r.MenuCollection.Add(new MenuInfo() { ID = "Header", Name = "Header", EnumPermission = MenuPermissionEnum.MP_NONE });
  117. foreach (AppMenu topMenuItem in _listMenuItems)
  118. {
  119. foreach (AppMenu subMenuItem in topMenuItem.MenuItems)
  120. {
  121. MenuInfo mp = new MenuInfo()
  122. {
  123. ID = subMenuItem.MenuID,
  124. Name = subMenuItem.ResKey, //Application.Current.FindResource(subMenuItem.ResKey) as string,
  125. EnumPermission = MenuPermissionEnum.MP_NONE
  126. };
  127. r.MenuCollection.Add(mp);
  128. }
  129. }
  130. return r;
  131. }
  132. public RoleItem CreateRole(RoleItem role)
  133. {
  134. RoleItem newRole = new RoleItem(GenerateRoleID())
  135. {
  136. RoleName = role.RoleName,
  137. AutoLogoutTime = role.AutoLogoutTime,
  138. IsAutoLogout = role.IsAutoLogout,
  139. DisplayRoleName = role.DisplayRoleName,
  140. DisplayAutoLogoutTime = role.DisplayAutoLogoutTime,
  141. DisplayIsAutoLogout = role.DisplayIsAutoLogout
  142. };
  143. foreach (MenuInfo mInfo in role.MenuCollection)
  144. {
  145. newRole.AddMenuInfo(mInfo.Clone());
  146. }
  147. return newRole;
  148. }
  149. public RoleItem CloneRole(RoleItem role)
  150. {
  151. RoleItem newRole = new RoleItem(role.RoleID)
  152. {
  153. RoleName = role.RoleName,
  154. AutoLogoutTime = role.AutoLogoutTime,
  155. IsAutoLogout = role.IsAutoLogout,
  156. DisplayRoleName = role.DisplayRoleName,
  157. DisplayAutoLogoutTime = role.DisplayAutoLogoutTime,
  158. DisplayIsAutoLogout = role.DisplayIsAutoLogout
  159. };
  160. foreach (MenuInfo mInfo in role.MenuCollection)
  161. {
  162. newRole.AddMenuInfo(mInfo.Clone());
  163. }
  164. return newRole;
  165. }
  166. public RoleItem CloneRole(string strRoleID)
  167. {
  168. RoleItem orignalRole = GetRoleByID(strRoleID);
  169. if (null == orignalRole)
  170. return null;
  171. RoleItem newRole = new RoleItem(strRoleID)
  172. {
  173. AutoLogoutTime = orignalRole.AutoLogoutTime,
  174. IsAutoLogout = orignalRole.IsAutoLogout,
  175. RoleName = orignalRole.RoleName,
  176. DisplayRoleName = orignalRole.DisplayRoleName,
  177. DisplayAutoLogoutTime = orignalRole.DisplayAutoLogoutTime,
  178. DisplayIsAutoLogout = orignalRole.DisplayIsAutoLogout
  179. };
  180. foreach (MenuInfo mInfo in orignalRole.MenuCollection)
  181. {
  182. newRole.AddMenuInfo(mInfo.Clone());
  183. }
  184. return newRole;
  185. }
  186. public bool CheckAvailable(RoleItem role)
  187. {
  188. if (role == null)
  189. return false;
  190. foreach (RoleItem r in _RoleContainer)
  191. {
  192. if (role.RoleName == r.RoleName && role.RoleID != r.RoleID)
  193. {
  194. LOG.Info("Check account available fail because the name already exists, in RoleManager");
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. public bool SaveRole(RoleItem pRoletoSave)
  201. {
  202. if (!CheckAvailable(pRoletoSave))
  203. return false;
  204. Role r = new Role(
  205. pRoletoSave.RoleID,
  206. pRoletoSave.RoleName,
  207. pRoletoSave.IsAutoLogout,
  208. pRoletoSave.AutoLogoutTime,
  209. MenuPermission.PermissionToString(pRoletoSave.MenuCollection.ToList())
  210. );
  211. if (RoleAccountProvider.Instance.UpdateRole(r))
  212. {
  213. RoleItem orignalRole = GetRoleByID(pRoletoSave.RoleID);
  214. if (null == orignalRole)
  215. {
  216. RoleItem NewRole = CloneRole(pRoletoSave);
  217. AddRole(NewRole);
  218. return true;
  219. }
  220. orignalRole.RoleName = pRoletoSave.RoleName;
  221. orignalRole.IsAutoLogout = pRoletoSave.IsAutoLogout;
  222. orignalRole.AutoLogoutTime = pRoletoSave.AutoLogoutTime;
  223. orignalRole.MenuCollection.Clear();
  224. foreach (MenuInfo mInfo in pRoletoSave.MenuCollection)
  225. {
  226. orignalRole.AddMenuInfo(mInfo.Clone());
  227. mInfo.ComboBoxSaved = true;
  228. }
  229. }
  230. else
  231. {
  232. LOG.Error("UpdateRoles method failed, in RoleManager");
  233. return false;
  234. }
  235. return true;
  236. }
  237. public bool DeleteRole(string strRoleID)
  238. {
  239. RoleItem r = GetRoleByID(strRoleID);
  240. if (r != null)
  241. {
  242. if (RoleAccountProvider.Instance.DeleteRole(strRoleID))
  243. {
  244. _RoleContainer.Remove(r);
  245. return true;
  246. }
  247. else
  248. {
  249. LOG.Error("DeleteRole method failed, in RoleManager");
  250. }
  251. }
  252. else
  253. {
  254. LOG.Warning("Can not find the role to delete, in RoleManager");
  255. }
  256. return false;
  257. }
  258. private static RoleManager _Instance = null;
  259. public static RoleManager Instance
  260. {
  261. get
  262. {
  263. if (_Instance == null)
  264. {
  265. _Instance = new RoleManager();
  266. }
  267. return _Instance;
  268. }
  269. }
  270. private ObservableCollection<RoleItem> _RoleContainer = new ObservableCollection<RoleItem>();
  271. public ObservableCollection<RoleItem> RoleContainer
  272. {
  273. get { return _RoleContainer; }
  274. }
  275. private static int _RoleNum = 0;
  276. private List<AppMenu> _listMenuItems = null;
  277. }
  278. public class MenuPermission
  279. {
  280. public void ParsePermission(string strPermission)
  281. {
  282. try
  283. {
  284. int groupIndex = strPermission.IndexOf(';');
  285. if (groupIndex == -1)
  286. {
  287. AddToDictionary(strPermission);
  288. return;
  289. }
  290. string strGroup = strPermission.Substring(0, groupIndex);
  291. AddToDictionary(strGroup);
  292. ParsePermission(strPermission.Substring(groupIndex + 1));
  293. }
  294. catch (System.Exception ex)
  295. {
  296. LOG.Write(ex);
  297. }
  298. }
  299. public MenuPermissionEnum GetPermissionByPageID(string strPageID)
  300. {
  301. if (MenuPermissionDictionary.ContainsKey(strPageID))
  302. {
  303. return MenuPermissionDictionary[strPageID];
  304. }
  305. return MenuPermissionEnum.MP_NONE;
  306. }
  307. public static string PermissionToString(List<MenuInfo> listPermission)
  308. {
  309. string strResult = "";
  310. if (listPermission == null)
  311. return strResult;
  312. foreach (MenuInfo info in listPermission)
  313. {
  314. strResult += info.ID;
  315. strResult += ",";
  316. strResult += info.IndexPermission;
  317. strResult += ";";
  318. }
  319. return strResult;
  320. }
  321. private void AddToDictionary(string strGroupName)
  322. {
  323. int Index = strGroupName.IndexOf(',');
  324. if (Index == -1)
  325. return;
  326. string strMenuID = strGroupName.Substring(0, Index);
  327. string strMenuPermision = strGroupName.Substring(Index + 1);
  328. MenuPermissionDictionary.Add(strMenuID, (MenuPermissionEnum)Convert.ToInt32(strMenuPermision));
  329. }
  330. public MenuPermissionEnum this[string strPageID]
  331. {
  332. get
  333. {
  334. return GetPermissionByPageID(strPageID);
  335. }
  336. }
  337. private Dictionary<string, MenuPermissionEnum> _dicPermission = new Dictionary<string, MenuPermissionEnum>();
  338. public Dictionary<string, MenuPermissionEnum> MenuPermissionDictionary
  339. {
  340. get { return _dicPermission; }
  341. }
  342. }
  343. }