AccountManager.cs 13 KB

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