RoleManager.cs 13 KB

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