AccountManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Collections.ObjectModel;
  7. using MECF.Framework.Common.Account.Extends;
  8. using OpenSEMI.ClientBase;
  9. namespace VirgoUI.Client.Models.Utility.AccountPage
  10. {
  11. public class AccountManager
  12. {
  13. private static int s_AccountNum = 0;
  14. #region Property define
  15. private ObservableCollection<AccountItem> m_AccountContainer = new ObservableCollection<AccountItem>();
  16. public ObservableCollection<AccountItem> AccountContainer
  17. {
  18. get { return m_AccountContainer; }
  19. }
  20. /// <summary>
  21. /// Store all the roles
  22. /// </summary>
  23. private List<Role> m_RoleList;
  24. public List<Role> RoleList
  25. {
  26. get { return m_RoleList; }
  27. }
  28. #endregion
  29. #region Functions define
  30. /// <summary>
  31. /// Singleton implement
  32. /// </summary>
  33. private AccountManager()
  34. {
  35. m_RoleList = new List<Role>();
  36. }
  37. private static AccountManager m_Instance = null;
  38. public static AccountManager Instance
  39. {
  40. get
  41. {
  42. if (m_Instance == null)
  43. m_Instance = new AccountManager();
  44. return m_Instance;
  45. }
  46. }
  47. /// <summary>
  48. /// (1)Initialize information about role
  49. /// (2)This method must be the first called
  50. /// </summary>
  51. /// <returns>false indicates error</returns>
  52. ///
  53. public bool Initialize()
  54. {
  55. this.m_AccountContainer.Clear();
  56. this.m_RoleList.Clear();
  57. this.m_RoleList = RoleAccountProvider.Instance.GetRoles();
  58. List<AccountEx> Accounts = RoleAccountProvider.Instance.GetAccounts();
  59. if (Accounts == null)
  60. {
  61. return false;
  62. }
  63. foreach (AccountEx Acc in Accounts)
  64. {
  65. AccountItem account = new AccountItem(Acc);
  66. account.InitRoleList(m_RoleList);
  67. m_AccountContainer.Add(account);
  68. }
  69. return true;
  70. }
  71. /// <summary>
  72. /// Get all the available Accounts
  73. /// </summary>
  74. /// <returns></returns>
  75. public List<AccountItem> GetAllAccounts()
  76. {
  77. return m_AccountContainer.ToList();
  78. }
  79. /// <summary>
  80. /// Generate Account ID
  81. /// </summary>
  82. /// <returns></returns>
  83. public string GenerateAccountID()
  84. {
  85. AccountItem Acc = GetAccountByID(s_AccountNum.ToString());
  86. while (Acc != null)
  87. {
  88. s_AccountNum++;
  89. Acc = GetAccountByID(s_AccountNum.ToString());
  90. }
  91. return s_AccountNum.ToString();
  92. }
  93. /// <summary>
  94. /// Add Account
  95. /// </summary>
  96. /// <param name="r">Account object</param>
  97. public bool AddAccount(AccountItem Acc)
  98. {
  99. AccountItem ExistAcc = GetAccountByName(Acc.AccountName);
  100. if (ExistAcc != null)
  101. {
  102. //ClientApp.Instance.Log.Info("Name of account to add exists,in CAccountManager");
  103. return false;
  104. }
  105. ExistAcc = GetAccountByID(Acc.AccountID);
  106. if (ExistAcc != null)
  107. {
  108. //ClientApp.Instance.Log.Info("ID of account to add exists,in CAccountManager");
  109. return false;
  110. }
  111. m_AccountContainer.Add(Acc);
  112. return true;
  113. }
  114. /// <summary>
  115. /// Get Account object by name
  116. /// </summary>
  117. /// <param name="name">Account name </param>
  118. /// <returns>null indicates error</returns>
  119. public AccountItem GetAccountByName(string name)
  120. {
  121. return m_AccountContainer.FirstOrDefault(t => t.AccountName == name);
  122. }
  123. /// <summary>
  124. /// Get Account object by ID
  125. /// </summary>
  126. /// <param name="name">Account ID </param>
  127. /// <returns>null indicates error</returns>
  128. public AccountItem GetAccountByID(string strID)
  129. {
  130. return m_AccountContainer.FirstOrDefault(t => t.AccountID == strID);
  131. }
  132. /// <summary>
  133. /// Create a default Account
  134. /// </summary>
  135. /// <returns></returns>
  136. public AccountItem CreateAccount()
  137. {
  138. AccountItem Acc = new AccountItem(GenerateAccountID());
  139. Acc.DisplayAccountName = Acc.AccountName = "NewUser";
  140. Acc.InitRoleList(RoleList);
  141. return Acc;
  142. }
  143. /// <summary>
  144. /// Create a copy Account
  145. /// </summary>
  146. /// <returns></returns>
  147. public AccountItem CreateAccount(AccountItem account)
  148. {
  149. AccountItem newAccount = new AccountItem(GenerateAccountID())
  150. {
  151. AccountName = account.AccountName,
  152. FirstName = account.FirstName,
  153. LastName = account.LastName,
  154. Email = account.Email,
  155. Password = account.Password,
  156. NewPassword = account.NewPassword,
  157. ConfirmPassword = account.ConfirmPassword,
  158. DisplayAccountName = account.AccountName,
  159. DisplayFirstName = account.FirstName,
  160. DisplayLastName = account.LastName,
  161. DisplayEmail = account.Email
  162. };
  163. foreach (RoleStatusItem item in account.RoleColleciton)
  164. {
  165. newAccount.RoleColleciton.Add(item.Clone());
  166. }
  167. return newAccount;
  168. }
  169. /// <summary>
  170. /// Clone a Account
  171. /// </summary>
  172. /// <returns></returns>
  173. public AccountItem CloneAccount(AccountItem account)
  174. {
  175. AccountItem newAccount = new AccountItem(account.AccountID)
  176. {
  177. AccountName = account.AccountName,
  178. FirstName = account.FirstName,
  179. LastName = account.LastName,
  180. Email = account.Email,
  181. Password = account.Password,
  182. NewPassword = account.NewPassword,
  183. ConfirmPassword = account.ConfirmPassword,
  184. DisplayAccountName = account.AccountName,
  185. DisplayFirstName = account.FirstName,
  186. DisplayLastName = account.LastName,
  187. DisplayEmail = account.Email
  188. };
  189. foreach (RoleStatusItem item in account.RoleColleciton)
  190. {
  191. newAccount.RoleColleciton.Add(item.Clone());
  192. }
  193. return newAccount;
  194. }
  195. /// <summary>
  196. /// Clone a Account by ID
  197. /// </summary>
  198. /// <param name="strAccountID"></param>
  199. /// <returns></returns>
  200. public AccountItem CloneAccount(string strAccountID)
  201. {
  202. AccountItem orignalAccount = GetAccountByID(strAccountID);
  203. if (null == orignalAccount)
  204. return null;
  205. AccountItem newAccount = new AccountItem(strAccountID)
  206. {
  207. AccountName = orignalAccount.AccountName,
  208. FirstName = orignalAccount.FirstName,
  209. LastName = orignalAccount.LastName,
  210. Email = orignalAccount.Email,
  211. Password = orignalAccount.Password,
  212. NewPassword = orignalAccount.NewPassword,
  213. ConfirmPassword = orignalAccount.ConfirmPassword,
  214. DisplayAccountName = orignalAccount.AccountName,
  215. DisplayFirstName = orignalAccount.FirstName,
  216. DisplayLastName = orignalAccount.LastName,
  217. DisplayEmail = orignalAccount.Email
  218. };
  219. foreach (RoleStatusItem RoleItem in orignalAccount.RoleColleciton)
  220. {
  221. newAccount.RoleColleciton.Add(RoleItem.Clone());
  222. }
  223. return newAccount;
  224. }
  225. /// <summary>
  226. /// Check if account data avilable
  227. /// </summary>
  228. /// <param name="Acc"></param>
  229. public bool CheckAvilable(AccountItem account)
  230. {
  231. if (account == null)
  232. return false;
  233. //same name
  234. foreach (AccountItem Acc in m_AccountContainer)
  235. {
  236. if (account.AccountName == Acc.AccountName && account.AccountID != Acc.AccountID)
  237. {
  238. //ClientApp.Instance.Log.Info("Check account avilable fail because the name exists,in CAccountManager");
  239. return false;
  240. }
  241. }
  242. return true;
  243. }
  244. /// <summary>
  245. /// Save Account
  246. /// </summary>
  247. /// <param name="Acc"></param>
  248. public bool SaveAccount(AccountItem Acc)
  249. {
  250. if (!CheckAvilable(Acc))
  251. return false;
  252. List<string> RoleList = new List<string>();
  253. foreach (RoleStatusItem RoleItem in Acc.RoleColleciton)
  254. {
  255. if (RoleItem.RoleStatus)
  256. {
  257. RoleList.Add(RoleItem.RoleID);
  258. }
  259. }
  260. string strPassword = string.Empty;
  261. if (!Acc.TryUpdatePassword())
  262. {
  263. //ClientApp.Instance.Log.Info("New password not match,in CAccountManager");
  264. strPassword = Acc.Password;
  265. }
  266. else
  267. {
  268. strPassword = Acc.NewPassword;
  269. }
  270. AccountEx newAccount = new AccountEx(
  271. Acc.AccountID,
  272. Acc.AccountName,
  273. string.IsNullOrWhiteSpace(strPassword) ? string.Empty : strPassword,
  274. string.IsNullOrWhiteSpace(Acc.FirstName) ? string.Empty : Acc.FirstName,
  275. string.IsNullOrWhiteSpace(Acc.LastName) ? string.Empty : Acc.LastName,
  276. string.IsNullOrWhiteSpace(Acc.Email) ? string.Empty : Acc.Email,
  277. RoleList);
  278. if (RoleAccountProvider.Instance.UpdateAccount(newAccount))
  279. {
  280. Acc.UpdatePassword();
  281. AccountItem orignalAccount = GetAccountByID(Acc.AccountID);
  282. if (null == orignalAccount)
  283. {
  284. AccountItem NewAccount = CloneAccount(Acc);
  285. AddAccount(NewAccount);
  286. return true;
  287. }
  288. orignalAccount.AccountName = Acc.AccountName;
  289. orignalAccount.FirstName = Acc.FirstName;
  290. orignalAccount.LastName = Acc.LastName;
  291. orignalAccount.Email = Acc.Email;
  292. orignalAccount.Password = Acc.Password;
  293. orignalAccount.NewPassword = Acc.NewPassword;
  294. orignalAccount.ConfirmPassword = Acc.ConfirmPassword;
  295. orignalAccount.RoleColleciton.Clear();
  296. foreach (RoleStatusItem RoleItem in Acc.RoleColleciton)
  297. {
  298. orignalAccount.RoleColleciton.Add(RoleItem.Clone());
  299. }
  300. }
  301. else
  302. {
  303. //ClientApp.Instance.Log.Info( "UpdateAccount method failed,in CAccountManager");
  304. return false;
  305. }
  306. return true;
  307. }
  308. /// <summary>
  309. /// Delete a Account by ID
  310. /// </summary>
  311. /// <param name="strAccountName"></param>
  312. public bool DeleteAccount(string strAccountID)
  313. {
  314. AccountItem Acc = GetAccountByID(strAccountID);
  315. if (Acc != null)
  316. {
  317. if (RoleAccountProvider.Instance.DeleteAccount(strAccountID))
  318. {
  319. m_AccountContainer.Remove(Acc);
  320. return true;
  321. }
  322. else
  323. {
  324. //ClientApp.Instance.Log.Info("DeleteAccount method failed,in CAccountManager");
  325. }
  326. }
  327. else
  328. {
  329. //ClientApp.Instance.Log.Info("Can not find the account to delete,in CAccountManager");
  330. }
  331. return false;
  332. }
  333. #endregion
  334. }
  335. }