RoleLoader.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. namespace MECF.Framework.Common.Account.Extends
  7. {
  8. public class RoleLoader : XmlLoader
  9. {
  10. private List<Role> m_rolelist;
  11. public List<Role> RoleList
  12. {
  13. get { return m_rolelist; }
  14. set { m_rolelist = value; }
  15. }
  16. private List<Role> m_Sperolelist;
  17. public List<Role> SpeRoleList
  18. {
  19. get { return m_Sperolelist; }
  20. set { m_Sperolelist = value; }
  21. }
  22. private List<AccountEx> m_accountlist;
  23. public List<AccountEx> AccountList
  24. {
  25. get { return m_accountlist; }
  26. set { m_accountlist = value; }
  27. }
  28. public RoleLoader(string p_strPath)
  29. : base(p_strPath)
  30. {
  31. }
  32. /// <summary>
  33. /// Return all roles except super role
  34. /// </summary>
  35. public List<Role> GetRoles()
  36. {
  37. return this.m_rolelist.Where(e => { return !e.IsSuper; }).ToList();
  38. }
  39. public List<Role> GetSpeRoles()
  40. {
  41. if (m_Sperolelist == null)
  42. m_Sperolelist = new List<Role>();
  43. return this.m_Sperolelist.Where(e => { return !e.IsSuper; }).ToList();
  44. }
  45. /// <summary>
  46. /// Return all accounts except super account
  47. /// </summary>
  48. public List<AccountEx> GetAccounts()
  49. {
  50. return this.m_accountlist.Where(e => { return !e.IsSuper; }).ToList();
  51. }
  52. bool IsAutoLogout = false;
  53. int nLogoutTime;
  54. List<Role> rolelist = new List<Role>();
  55. List<Role> Specialrolelist = new List<Role>();
  56. List<AccountEx> accountlist = new List<AccountEx>();
  57. protected override void AnalyzeXml()
  58. {
  59. if (this.m_xdoc != null)
  60. {
  61. //load roles
  62. var results = from r in this.m_xdoc.Descendants("roleItem") select r;
  63. List<Role> rolelist = new List<Role>();
  64. bool IsAutoLogout = false;
  65. int nLogoutTime;
  66. foreach (var result in results)
  67. {
  68. string RoleID = result.Attribute("id").Value;
  69. string RoleName = result.Attribute("name").Value;
  70. string AutoLogout = result.Attribute("autologout").Value;
  71. string LogoutTime = result.Attribute("logouttime").Value;
  72. int.TryParse(LogoutTime, out nLogoutTime);
  73. IsAutoLogout = AutoLogout == "1" ? true : false;
  74. string Description = "";
  75. if (result.Attribute("description") != null)
  76. Description = result.Attribute("description").Value;
  77. string Permissions = result.Value;
  78. Role roleObject = new Role(RoleID, RoleName, IsAutoLogout, nLogoutTime, Permissions, Description);
  79. rolelist.Add(roleObject);
  80. }
  81. //Create an super role
  82. Role superRole = new Role("-1", "Administrators", true, 20, null, null) { IsSuper = true };
  83. rolelist.Add(superRole);
  84. this.m_rolelist = rolelist;
  85. //load users
  86. results = from r in this.m_xdoc.Descendants("userItem") select r;
  87. List<AccountEx> accountlist = new List<AccountEx>();
  88. foreach (var result in results)
  89. {
  90. List<string> roleIds = new List<string>();
  91. string UserID = result.Attribute("id").Value;
  92. string LoginName = result.Attribute("loginname").Value;
  93. string Password = Decrypt(result.Attribute("password").Value);
  94. string FirstName = result.Attribute("firstname").Value;
  95. string LastName = result.Attribute("lastname").Value;
  96. string Email = result.Attribute("email").Value;
  97. string Description = "";
  98. if (result.Attribute("description") != null)
  99. Description = result.Attribute("description").Value;
  100. var roles = from ro in result.Descendants("role") select ro;
  101. foreach (var role in roles)
  102. {
  103. string strID = role.Attribute("id").Value;
  104. roleIds.Add(strID);
  105. }
  106. AccountEx accountObject = new AccountEx(UserID, LoginName, Password, FirstName, LastName, Email, roleIds, Description);
  107. accountlist.Add(accountObject);
  108. }
  109. AccountEx superAccount = new AccountEx("-1", "admin", "admin", "", "", "", new List<string>() { "-1" }, "") { IsSuper = true };
  110. accountlist.Add(superAccount);
  111. this.m_accountlist = accountlist;
  112. }
  113. }
  114. public bool UpdateRole(Role p_newRole)
  115. {
  116. Role m_role = m_rolelist.Find(item => item.RoleID == p_newRole.RoleID);
  117. if (m_role == null)
  118. m_rolelist.Add(p_newRole);
  119. else
  120. m_rolelist[m_rolelist.IndexOf(m_role)] = p_newRole;
  121. //save the roles to file
  122. XDocument xdoc = this.m_xdoc;
  123. var results = (from m_xRole in xdoc.Descendants("roleItem")
  124. where m_xRole.Attribute("id").Value == p_newRole.RoleID
  125. select m_xRole).ToList();
  126. if (results.Count > 0)
  127. {
  128. results[0].Attribute("name").Value = p_newRole.RoleName;
  129. results[0].Attribute("autologout").Value = p_newRole.IsAutoLogout ? "1" : "0";
  130. results[0].Attribute("logouttime").Value = p_newRole.LogoutTime.ToString();
  131. if (results[0].Attribute("description") != null)
  132. results[0].Attribute("description").Value = p_newRole.Description.ToString();
  133. results[0].Value = p_newRole.MenuPermission;
  134. }
  135. else
  136. {
  137. XElement m_new =
  138. new XElement("roleItem",
  139. new XAttribute("id", p_newRole.RoleID),
  140. new XAttribute("name", p_newRole.RoleName),
  141. new XAttribute("autologout", p_newRole.IsAutoLogout ? "1" : "0"),
  142. new XAttribute("logouttime", p_newRole.LogoutTime),
  143. new XAttribute("description", p_newRole.Description)
  144. )
  145. { Value = p_newRole.MenuPermission };
  146. xdoc.Root.Element("roles").Add(m_new);
  147. }
  148. xdoc.Save(this.m_strPath);
  149. return true;
  150. }
  151. public bool DeleteRole(string p_strRoleID)
  152. {
  153. this.Load();
  154. Role m_role = m_rolelist.Find(item => item.RoleID == p_strRoleID);
  155. if (m_role != null)
  156. {
  157. m_rolelist.Remove(m_role);
  158. //save the roles to file
  159. XDocument xdoc = this.m_xdoc;
  160. var results = (from m_xRole in xdoc.Descendants("roleItem")
  161. where m_xRole.Attribute("id").Value == p_strRoleID
  162. select m_xRole).ToList();
  163. if (results.Count > 0)
  164. {
  165. results[0].Remove();
  166. //remove role from account
  167. foreach (var account in this.m_accountlist)
  168. {
  169. if (account.RoleIDs.Contains(m_role.RoleID))
  170. account.RoleIDs.Remove(m_role.RoleID);
  171. }
  172. results = (from m_xRole in xdoc.Descendants("role")
  173. where m_xRole.Attribute("id").Value == m_role.RoleID
  174. select m_xRole).ToList();
  175. if (results.Count > 0)
  176. results.Remove();
  177. xdoc.Save(this.m_strPath);
  178. return true;
  179. }
  180. else
  181. return false;
  182. }
  183. else
  184. return false;
  185. }
  186. private List<string> GetRolePermission(string roleid)
  187. {
  188. List<string> rolePermissions = new List<string>();
  189. foreach (Role role in this.m_rolelist)
  190. {
  191. if (role.RoleID == roleid)
  192. {
  193. rolePermissions = role.MenuPermission.Split(';').ToList();
  194. break;
  195. }
  196. }
  197. if (rolePermissions.Count==0)
  198. {
  199. foreach (Role role in this.m_Sperolelist)
  200. {
  201. if (role.RoleID == roleid)
  202. {
  203. rolePermissions = role.MenuPermission.Split(';').ToList();
  204. break;
  205. }
  206. }
  207. }
  208. return rolePermissions;
  209. }
  210. private int GetMenuPermission(List<string> rolePermissions, string menuid)
  211. {
  212. foreach (string menuPermission in rolePermissions)
  213. {
  214. if (menuPermission.IndexOf(menuid) >= 0)
  215. {
  216. string[] pair = menuPermission.Split(',');
  217. if (pair.Length > 1 && pair[0].Trim() == menuid) //need check the whole menuid
  218. return int.Parse(pair[1].Trim());
  219. }
  220. }
  221. return 3;
  222. }
  223. public List<AppMenu> GetMenusByRole(string roleid, List<AppMenu> menulist)
  224. {
  225. return GetMenusByRoleInfo(roleid, menulist, "4");
  226. }
  227. public List<AppMenu> GetMenusByRole(Role role, List<AppMenu> menulist)
  228. {
  229. return GetMenusByRoleInfo(role.RoleID, menulist, "1");
  230. }
  231. public List<AppMenu> GetMenusByRoleInfo(string roleid, List<AppMenu> menulist,string ManagerRoleid)
  232. {
  233. List<AppMenu> menus = new List<AppMenu>();
  234. List<string> rolePermissions = GetRolePermission(roleid);
  235. foreach (AppMenu menuItem in menulist)
  236. {
  237. List<AppMenu> subMenus = new List<AppMenu>();
  238. foreach (AppMenu subMenu in menuItem.MenuItems)
  239. {
  240. AppMenu RetSubMenu = new AppMenu(subMenu.MenuID, subMenu.ViewModel, subMenu.ResKey, null);
  241. RetSubMenu.System = subMenu.System;
  242. RetSubMenu.Type = subMenu.Type;
  243. RetSubMenu.AlarmModule = subMenu.AlarmModule;
  244. RetSubMenu.Permission = this.GetMenuPermission(rolePermissions, subMenu.MenuID);
  245. if (RetSubMenu.Permission > 1)
  246. subMenus.Add(RetSubMenu);
  247. }
  248. if (subMenus.Count > 0)
  249. {
  250. var menu = new AppMenu(menuItem.MenuID, menuItem.ViewModel, menuItem.ResKey, subMenus);
  251. menu.System = menuItem.System;
  252. menu.Type = menuItem.Type;
  253. menu.AlarmModule = menuItem.AlarmModule;
  254. menus.Add(menu);
  255. }
  256. }
  257. return menus;
  258. }
  259. public int GetMenuPermission(string roleid, string menuName)
  260. {
  261. List<string> rolePermissions = GetRolePermission(roleid);
  262. int menuPermission = this.GetMenuPermission(rolePermissions, menuName);
  263. return menuPermission;
  264. }
  265. public bool UpdateAccount(AccountEx p_newAccount)
  266. {
  267. AccountEx Acc = m_accountlist.Find(item => item.UserID == p_newAccount.UserID);
  268. if (Acc == null)
  269. m_accountlist.Add(p_newAccount);
  270. else
  271. m_accountlist[m_accountlist.IndexOf(Acc)] = p_newAccount;
  272. //save the roles to file
  273. XDocument xdoc = this.m_xdoc;
  274. var results = (from xAccount in xdoc.Descendants("userItem")
  275. where xAccount.Attribute("id").Value == p_newAccount.UserID
  276. select xAccount).ToList();
  277. if (results.Count > 0)
  278. {
  279. results[0].Attribute("loginname").Value = p_newAccount.LoginName;
  280. results[0].Attribute("password").Value = Encrypt(p_newAccount.Password);
  281. results[0].Attribute("firstname").Value = p_newAccount.FirstName;
  282. results[0].Attribute("lastname").Value = p_newAccount.LastName;
  283. results[0].Attribute("email").Value = p_newAccount.Email;
  284. results[0].Attribute("description").Value = p_newAccount.Description;
  285. results[0].Element("rolegroup").RemoveAll();
  286. foreach (string strRole in p_newAccount.RoleIDs)
  287. {
  288. results[0].Element("rolegroup").Add(new XElement("role", new XAttribute("id", strRole)));
  289. }
  290. }
  291. else
  292. {
  293. XElement m_new =
  294. new XElement("userItem",
  295. new XAttribute("id", p_newAccount.UserID),
  296. new XAttribute("loginname", p_newAccount.LoginName),
  297. new XAttribute("password", Encrypt(p_newAccount.Password)),
  298. new XAttribute("firstname", p_newAccount.FirstName),
  299. new XAttribute("lastname", p_newAccount.LastName),
  300. new XAttribute("email", p_newAccount.Email),
  301. new XAttribute("description", p_newAccount.Description),
  302. new XElement("rolegroup"));
  303. foreach (string strRole in p_newAccount.RoleIDs)
  304. {
  305. m_new.Element("rolegroup").Add(new XElement("role", new XAttribute("id", strRole)));
  306. }
  307. xdoc.Root.Element("users").Add(m_new);
  308. }
  309. xdoc.Save(this.m_strPath);
  310. return true;
  311. }
  312. public bool DeleteAccount(string p_strUserID)
  313. {
  314. AccountEx Acc = m_accountlist.Find(item => item.UserID == p_strUserID);
  315. if (Acc != null)
  316. {
  317. m_accountlist.Remove(Acc);
  318. XDocument xdoc = this.m_xdoc;
  319. var results = (from xAccount in xdoc.Descendants("userItem")
  320. where xAccount.Attribute("id").Value == p_strUserID
  321. select xAccount).ToList();
  322. if (results.Count > 0)
  323. {
  324. results[0].Remove();
  325. xdoc.Save(this.m_strPath);
  326. return true;
  327. }
  328. else
  329. return false;
  330. }
  331. else
  332. return false;
  333. }
  334. public String Encrypt(String encrytStr)
  335. {
  336. if (String.IsNullOrWhiteSpace(encrytStr)) return String.Empty;
  337. try
  338. {
  339. Byte[] bytes = Encoding.UTF8.GetBytes(encrytStr);
  340. return Convert.ToBase64String(bytes);
  341. }
  342. catch
  343. {
  344. return encrytStr;
  345. }
  346. }
  347. public String Decrypt(String decryptStr)
  348. {
  349. if (String.IsNullOrWhiteSpace(decryptStr)) return String.Empty;
  350. try
  351. {
  352. Byte[] bytes = Convert.FromBase64String(decryptStr);
  353. return Encoding.UTF8.GetString(bytes);
  354. }
  355. catch
  356. {
  357. return decryptStr;
  358. }
  359. }
  360. }
  361. }