RoleLoader.cs 13 KB

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