RoleManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 VirgoUI.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", DisplayIndexPermission = 2 });
  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_READ_WRITE,
  126. DisplayIndexPermission = 2
  127. };
  128. r.MenuCollection.Add(mp);
  129. }
  130. }
  131. return r;
  132. }
  133. public RoleItem CreateRole(RoleItem role)
  134. {
  135. RoleItem newRole = new RoleItem(GenerateRoleID())
  136. {
  137. RoleName = role.RoleName,
  138. AutoLogoutTime = role.AutoLogoutTime,
  139. IsAutoLogout = role.IsAutoLogout,
  140. DisplayRoleName = role.DisplayRoleName,
  141. DisplayAutoLogoutTime = role.DisplayAutoLogoutTime,
  142. DisplayIsAutoLogout = role.DisplayIsAutoLogout
  143. };
  144. foreach (MenuInfo mInfo in role.MenuCollection)
  145. {
  146. newRole.AddMenuInfo(mInfo.Clone());
  147. }
  148. return newRole;
  149. }
  150. public RoleItem CloneRole(RoleItem role)
  151. {
  152. RoleItem newRole = new RoleItem(role.RoleID)
  153. {
  154. RoleName = role.RoleName,
  155. AutoLogoutTime = role.AutoLogoutTime,
  156. IsAutoLogout = role.IsAutoLogout,
  157. DisplayRoleName = role.DisplayRoleName,
  158. DisplayAutoLogoutTime = role.DisplayAutoLogoutTime,
  159. DisplayIsAutoLogout = role.DisplayIsAutoLogout
  160. };
  161. foreach (MenuInfo mInfo in role.MenuCollection)
  162. {
  163. newRole.AddMenuInfo(mInfo.Clone());
  164. }
  165. return newRole;
  166. }
  167. public RoleItem CloneRole(string strRoleID)
  168. {
  169. RoleItem orignalRole = GetRoleByID(strRoleID);
  170. if (null == orignalRole)
  171. return null;
  172. RoleItem newRole = new RoleItem(strRoleID)
  173. {
  174. AutoLogoutTime = orignalRole.AutoLogoutTime,
  175. IsAutoLogout = orignalRole.IsAutoLogout,
  176. RoleName = orignalRole.RoleName,
  177. DisplayRoleName = orignalRole.DisplayRoleName,
  178. DisplayAutoLogoutTime = orignalRole.DisplayAutoLogoutTime,
  179. DisplayIsAutoLogout = orignalRole.DisplayIsAutoLogout
  180. };
  181. foreach (MenuInfo mInfo in orignalRole.MenuCollection)
  182. {
  183. newRole.AddMenuInfo(mInfo.Clone());
  184. }
  185. return newRole;
  186. }
  187. public bool CheckAvailable(RoleItem role)
  188. {
  189. if (role == null)
  190. return false;
  191. foreach (RoleItem r in _RoleContainer)
  192. {
  193. if (role.RoleName == r.RoleName && role.RoleID != r.RoleID)
  194. {
  195. LOG.Info("Check account available fail because the name already exists, in RoleManager");
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. public bool SaveRole(RoleItem pRoletoSave)
  202. {
  203. if (!CheckAvailable(pRoletoSave))
  204. return false;
  205. Role r = new Role(
  206. pRoletoSave.RoleID,
  207. pRoletoSave.RoleName,
  208. pRoletoSave.IsAutoLogout,
  209. pRoletoSave.AutoLogoutTime,
  210. MenuPermission.PermissionToString(pRoletoSave.MenuCollection.ToList())
  211. );
  212. if (RoleAccountProvider.Instance.UpdateRole(r))
  213. {
  214. RoleItem orignalRole = GetRoleByID(pRoletoSave.RoleID);
  215. if (null == orignalRole)
  216. {
  217. RoleItem NewRole = CloneRole(pRoletoSave);
  218. AddRole(NewRole);
  219. return true;
  220. }
  221. orignalRole.RoleName = pRoletoSave.RoleName;
  222. orignalRole.IsAutoLogout = pRoletoSave.IsAutoLogout;
  223. orignalRole.AutoLogoutTime = pRoletoSave.AutoLogoutTime;
  224. orignalRole.MenuCollection.Clear();
  225. foreach (MenuInfo mInfo in pRoletoSave.MenuCollection)
  226. {
  227. orignalRole.AddMenuInfo(mInfo.Clone());
  228. mInfo.ComboBoxSaved = true;
  229. }
  230. }
  231. else
  232. {
  233. LOG.Error("UpdateRoles method failed, in RoleManager");
  234. return false;
  235. }
  236. return true;
  237. }
  238. public bool DeleteRole(string strRoleID)
  239. {
  240. RoleItem r = GetRoleByID(strRoleID);
  241. if (r != null)
  242. {
  243. if (RoleAccountProvider.Instance.DeleteRole(strRoleID))
  244. {
  245. _RoleContainer.Remove(r);
  246. return true;
  247. }
  248. else
  249. {
  250. LOG.Error("DeleteRole method failed, in RoleManager");
  251. }
  252. }
  253. else
  254. {
  255. LOG.Warning("Can not find the role to delete, in RoleManager");
  256. }
  257. return false;
  258. }
  259. private static RoleManager _Instance = null;
  260. public static RoleManager Instance
  261. {
  262. get
  263. {
  264. if (_Instance == null)
  265. {
  266. _Instance = new RoleManager();
  267. }
  268. return _Instance;
  269. }
  270. }
  271. private ObservableCollection<RoleItem> _RoleContainer = new ObservableCollection<RoleItem>();
  272. public ObservableCollection<RoleItem> RoleContainer
  273. {
  274. get { return _RoleContainer; }
  275. }
  276. private static int _RoleNum = 0;
  277. private List<AppMenu> _listMenuItems = null;
  278. }
  279. public class MenuPermission
  280. {
  281. public void ParsePermission(string strPermission)
  282. {
  283. try
  284. {
  285. int groupIndex = strPermission.IndexOf(';');
  286. if (groupIndex == -1)
  287. {
  288. AddToDictionary(strPermission);
  289. return;
  290. }
  291. string strGroup = strPermission.Substring(0, groupIndex);
  292. AddToDictionary(strGroup);
  293. ParsePermission(strPermission.Substring(groupIndex + 1));
  294. }
  295. catch (System.Exception ex)
  296. {
  297. LOG.Write(ex);
  298. }
  299. }
  300. public MenuPermissionEnum GetPermissionByPageID(string strPageID)
  301. {
  302. if (MenuPermissionDictionary.ContainsKey(strPageID))
  303. {
  304. return MenuPermissionDictionary[strPageID];
  305. }
  306. return MenuPermissionEnum.MP_NONE;
  307. }
  308. public static string PermissionToString(List<MenuInfo> listPermission)
  309. {
  310. string strResult = "";
  311. if (listPermission == null)
  312. return strResult;
  313. foreach (MenuInfo info in listPermission)
  314. {
  315. strResult += info.ID;
  316. strResult += ",";
  317. strResult += info.IndexPermission;
  318. strResult += ";";
  319. }
  320. return strResult;
  321. }
  322. private void AddToDictionary(string strGroupName)
  323. {
  324. int Index = strGroupName.IndexOf(',');
  325. if (Index == -1)
  326. return;
  327. string strMenuID = strGroupName.Substring(0, Index);
  328. string strMenuPermision = strGroupName.Substring(Index + 1);
  329. MenuPermissionDictionary.Add(strMenuID, (MenuPermissionEnum)Convert.ToInt32(strMenuPermision));
  330. }
  331. public MenuPermissionEnum this[string strPageID]
  332. {
  333. get
  334. {
  335. return GetPermissionByPageID(strPageID);
  336. }
  337. }
  338. private Dictionary<string, MenuPermissionEnum> _dicPermission = new Dictionary<string, MenuPermissionEnum>();
  339. public Dictionary<string, MenuPermissionEnum> MenuPermissionDictionary
  340. {
  341. get { return _dicPermission; }
  342. }
  343. }
  344. }