RoleLoader.cs 15 KB

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