PageRenderTime 603ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Application/BLL/MemberShipService.cs

http://goshopping2009.googlecode.com/
C# | 312 lines | 190 code | 18 blank | 104 comment | 21 complexity | 509be1ea8b78e2b38a354ee6cfc73096 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Security;
  6. using GoShopping.Model;
  7. using GoShopping.Model.Enumerations;
  8. namespace GoShopping.BLL
  9. {
  10. /// <summary>
  11. /// Membership???
  12. /// </summary>
  13. public class MemberShipService
  14. {
  15. /// <summary>
  16. /// ???????????
  17. /// </summary>
  18. /// <returns></returns>
  19. public bool IsMembershipDBHasData()
  20. {
  21. int memCount = Convert.ToInt32(Databases.DBGoShoppingMember.Select(GoShoppingMembershipNBear.aspnet_Users, GoShoppingMembershipNBear.aspnet_Users.UserId.Count()).ToDataSet().Tables[0].Rows[0][0].ToString());
  22. if (memCount > 0)
  23. {
  24. return true;
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31. /// <summary>
  32. /// ????Membetship????
  33. /// </summary>
  34. /// <returns></returns>
  35. public IList<MembershipUser> GetAllMembers()
  36. {
  37. IList<MembershipUser> list = new List<MembershipUser>();
  38. System.Collections.ICollection arr = Membership.GetAllUsers();
  39. foreach (MembershipUser m in arr)
  40. {
  41. list.Add(m);
  42. }
  43. arr = null;
  44. return list;
  45. }
  46. /// <summary>
  47. /// ????
  48. /// </summary>
  49. /// <param name="username"></param>
  50. /// <param name="password"></param>
  51. /// <returns></returns>
  52. public bool ValidateUser(string username, string password)
  53. {
  54. return Membership.ValidateUser(username, password);
  55. }
  56. /// <summary>
  57. /// ????????????
  58. /// </summary>
  59. /// <param name="username"></param>
  60. /// <returns></returns>
  61. public bool IsUserInMembershipDB(string username)
  62. {
  63. MembershipUser mu = Membership.GetUser(username);
  64. if (mu == null)
  65. {
  66. return false;
  67. }
  68. else
  69. {
  70. return true;
  71. }
  72. }
  73. /// <summary>
  74. /// ??????
  75. /// </summary>
  76. /// <param name="username"></param>
  77. /// <param name="oldPassword"></param>
  78. /// <param name="newPassword"></param>
  79. /// <returns></returns>
  80. public bool ChangeMemberPassword(string username, string oldPassword, string newPassword)
  81. {
  82. MembershipUser mu = Membership.GetUser(username);
  83. return mu.ChangePassword(oldPassword, newPassword);
  84. }
  85. /// <summary>
  86. /// ???????
  87. /// [?????????]
  88. /// </summary>
  89. /// <param name="userName"></param>
  90. /// <param name="isApprove">True???,False???</param>
  91. public void ApproveMember(string userName, bool isApprove)
  92. {
  93. MembershipUser mu = Membership.GetUser(userName);
  94. if (isApprove == true)//??
  95. {
  96. if (mu.IsLockedOut == false)
  97. {
  98. mu.IsApproved = isApprove;
  99. }
  100. }
  101. else if (!Roles.IsUserInRole(Model.Enumerations.Member_Roles.Role_SuperManager.ToString()))
  102. {
  103. if (mu.IsLockedOut == false)
  104. {
  105. mu.IsApproved = isApprove;
  106. }
  107. }
  108. Membership.UpdateUser(mu);
  109. }
  110. ///// <summary>
  111. ///// ?????????
  112. ///// [??????????????]
  113. ///// [??????????????????????????????????????????]
  114. ///// </summary>
  115. ///// <param name="memberUserName"></param>
  116. ///// <returns></returns>
  117. //public Member_Roles GetMemberRole(string memberUserName)
  118. //{
  119. // MembershipUser mu = Membership.GetUser(memberUserName);
  120. // string[] roles = Roles.GetRolesForUser(memberUserName);
  121. // if (roles.Length == 1)
  122. // {
  123. // Member_Roles r = (Member_Roles)Enum.Parse(typeof(Member_Roles), roles[0]);
  124. // return r;
  125. // }
  126. // else
  127. // {
  128. // throw (new Exception("GetMemberRole Error!"));
  129. // }
  130. //}
  131. /// <summary>
  132. /// ?????????
  133. /// </summary>
  134. /// <param name="memberUserName"></param>
  135. /// <returns></returns>
  136. public IList<Member_Roles> GetMemberRoleList(string memberUserName)
  137. {
  138. IList<Member_Roles> list = new List<Member_Roles>();
  139. MembershipUser mu = Membership.GetUser(memberUserName);
  140. string[] roles = Roles.GetRolesForUser(memberUserName);
  141. foreach (string s in roles)
  142. {
  143. Member_Roles r = (Member_Roles)Enum.Parse(typeof(Member_Roles), s);
  144. list.Add(r);
  145. }
  146. return list;
  147. }
  148. /// <summary>
  149. /// ??????????
  150. /// </summary>
  151. /// <param name="role"></param>
  152. /// <returns></returns>
  153. public IList<MembershipUser> GetRoleUsers(Member_Roles role)
  154. {
  155. string[] usersName = Roles.GetUsersInRole(role.ToString());
  156. IList<MembershipUser> userList = new List<MembershipUser>();
  157. foreach (string s in usersName)
  158. {
  159. userList.Add(Membership.GetUser(s));
  160. }
  161. return userList;
  162. }
  163. /// <summary>
  164. /// ??????????
  165. /// </summary>
  166. /// <param name="strUserName"></param>
  167. /// <returns></returns>
  168. public IList<MembershipUser> GetMembers(string strUserName)
  169. {
  170. IList<UserInfo> userList = Databases.DBGoShopping.Select(
  171. GoShoppingNBear.UserInfo).Where(GoShoppingNBear.UserInfo.Name.Like("%" + strUserName + "%")).ToList<UserInfo>();
  172. IList<MembershipUser> list = new List<MembershipUser>();
  173. foreach (UserInfo u in userList)
  174. {
  175. list.Add(Membership.GetUser(u.Name));
  176. }
  177. return list;
  178. }
  179. /// <summary>
  180. /// ????
  181. /// ???????????????
  182. /// </summary>
  183. /// <param name="userName">Member??</param>
  184. /// <param name="eMail"></param>
  185. /// <param name="password">??</param>
  186. /// <param name="passwordQustion"></param>
  187. /// <param name="passwordanser">????</param>
  188. /// <param name="IsApproved">????</param>
  189. /// <param name="role">??</param>
  190. /// <param name="createStatue">???????????</param>
  191. /// <returns>??????Ture??????False???createStatue????????</returns>
  192. public bool CreateMember(string userName, string eMail, string password, string passwordQustion, string passwordanser, bool IsApproved, Member_Roles role, out MembershipCreateStatus createStatue)
  193. {
  194. bool returnValue;
  195. using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
  196. {
  197. MembershipUser user = Membership.CreateUser(userName, password, eMail, passwordQustion,
  198. passwordanser, IsApproved,
  199. out createStatue);
  200. if (createStatue == MembershipCreateStatus.Success)
  201. {
  202. Roles.AddUserToRole(user.UserName, role.ToString());
  203. returnValue = true;
  204. }
  205. else
  206. {
  207. returnValue = false;
  208. if (user != null)
  209. {
  210. DeleteMember(user.UserName);
  211. }
  212. }
  213. //????????????????????
  214. if (returnValue == true)
  215. {
  216. IMember member = Factory_Member.CreateMemberOption(role);
  217. member.AddNewMember(user);
  218. }
  219. scope.Complete();
  220. }
  221. return returnValue;
  222. }
  223. /// <summary>
  224. /// ????
  225. /// </summary>
  226. /// <param name="userName"></param>
  227. /// <returns></returns>
  228. public bool DeleteMember(string userName)
  229. {
  230. bool returnValue;
  231. using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
  232. {
  233. IList<Member_Roles> roleList = this.GetMemberRoleList(userName);
  234. returnValue = Membership.DeleteUser(userName);
  235. //????????????????????
  236. if (returnValue == true)
  237. {//????????????????
  238. foreach (Member_Roles role in roleList)
  239. {
  240. IMember member = Factory_Member.CreateMemberOption(role);
  241. member.DeleteMember(userName);
  242. }
  243. }
  244. scope.Complete();
  245. }
  246. return returnValue;
  247. }
  248. /// <summary>
  249. /// ????????????
  250. /// </summary>
  251. /// <param name="role"></param>
  252. private void DeleteRoleUsers(Member_Roles role)
  253. {
  254. string[] usernames = Roles.GetUsersInRole(role.ToString());
  255. foreach (string user in usernames)
  256. {
  257. Membership.DeleteUser(user);
  258. }
  259. }
  260. #region ?????
  261. /// <summary>
  262. /// ??????Email????????
  263. /// ????
  264. /// </summary>
  265. /// <param name="email"></param>
  266. /// <param name="emailPassword"></param>
  267. /// <param name="statue"></param>
  268. /// <returns></returns>
  269. public bool CreateSuperManager(string emailUserName, string emailPassword, out MembershipCreateStatus statue)
  270. {
  271. bool returnValue;
  272. //????
  273. using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
  274. {
  275. DeleteRoleUsers(Member_Roles.Role_SuperManager);
  276. string UserName = emailUserName;
  277. string Email = emailUserName;
  278. string PasswordQuestion = "Email?";
  279. this.CreateMember(UserName, emailUserName, emailPassword, PasswordQuestion, emailUserName, true, Member_Roles.Role_SuperManager, out statue);
  280. if (statue == MembershipCreateStatus.Success)
  281. {
  282. returnValue = true;
  283. }
  284. else
  285. {
  286. returnValue = false;
  287. }
  288. scope.Complete();
  289. }//using scope
  290. return returnValue;
  291. }
  292. #endregion
  293. }
  294. }