/Application/BLL/MemberShipService.cs
C# | 312 lines | 190 code | 18 blank | 104 comment | 21 complexity | 509be1ea8b78e2b38a354ee6cfc73096 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Security;
- using GoShopping.Model;
- using GoShopping.Model.Enumerations;
-
- namespace GoShopping.BLL
- {
- /// <summary>
- /// Membership???
- /// </summary>
- public class MemberShipService
- {
- /// <summary>
- /// ???????????
- /// </summary>
- /// <returns></returns>
- public bool IsMembershipDBHasData()
- {
- int memCount = Convert.ToInt32(Databases.DBGoShoppingMember.Select(GoShoppingMembershipNBear.aspnet_Users, GoShoppingMembershipNBear.aspnet_Users.UserId.Count()).ToDataSet().Tables[0].Rows[0][0].ToString());
- if (memCount > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /// <summary>
- /// ????Membetship????
- /// </summary>
- /// <returns></returns>
- public IList<MembershipUser> GetAllMembers()
- {
- IList<MembershipUser> list = new List<MembershipUser>();
- System.Collections.ICollection arr = Membership.GetAllUsers();
- foreach (MembershipUser m in arr)
- {
- list.Add(m);
- }
- arr = null;
- return list;
- }
-
- /// <summary>
- /// ????
- /// </summary>
- /// <param name="username"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public bool ValidateUser(string username, string password)
- {
- return Membership.ValidateUser(username, password);
- }
-
- /// <summary>
- /// ????????????
- /// </summary>
- /// <param name="username"></param>
- /// <returns></returns>
- public bool IsUserInMembershipDB(string username)
- {
- MembershipUser mu = Membership.GetUser(username);
- if (mu == null)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
- /// <summary>
- /// ??????
- /// </summary>
- /// <param name="username"></param>
- /// <param name="oldPassword"></param>
- /// <param name="newPassword"></param>
- /// <returns></returns>
- public bool ChangeMemberPassword(string username, string oldPassword, string newPassword)
- {
- MembershipUser mu = Membership.GetUser(username);
- return mu.ChangePassword(oldPassword, newPassword);
- }
-
- /// <summary>
- /// ???????
- /// [?????????]
- /// </summary>
- /// <param name="userName"></param>
- /// <param name="isApprove">True???,False???</param>
- public void ApproveMember(string userName, bool isApprove)
- {
- MembershipUser mu = Membership.GetUser(userName);
- if (isApprove == true)//??
- {
- if (mu.IsLockedOut == false)
- {
- mu.IsApproved = isApprove;
- }
- }
- else if (!Roles.IsUserInRole(Model.Enumerations.Member_Roles.Role_SuperManager.ToString()))
- {
- if (mu.IsLockedOut == false)
- {
- mu.IsApproved = isApprove;
- }
- }
- Membership.UpdateUser(mu);
- }
-
- ///// <summary>
- ///// ?????????
- ///// [??????????????]
- ///// [??????????????????????????????????????????]
- ///// </summary>
- ///// <param name="memberUserName"></param>
- ///// <returns></returns>
- //public Member_Roles GetMemberRole(string memberUserName)
- //{
- // MembershipUser mu = Membership.GetUser(memberUserName);
- // string[] roles = Roles.GetRolesForUser(memberUserName);
- // if (roles.Length == 1)
- // {
- // Member_Roles r = (Member_Roles)Enum.Parse(typeof(Member_Roles), roles[0]);
- // return r;
- // }
- // else
- // {
- // throw (new Exception("GetMemberRole Error!"));
- // }
- //}
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <param name="memberUserName"></param>
- /// <returns></returns>
- public IList<Member_Roles> GetMemberRoleList(string memberUserName)
- {
- IList<Member_Roles> list = new List<Member_Roles>();
- MembershipUser mu = Membership.GetUser(memberUserName);
- string[] roles = Roles.GetRolesForUser(memberUserName);
- foreach (string s in roles)
- {
- Member_Roles r = (Member_Roles)Enum.Parse(typeof(Member_Roles), s);
- list.Add(r);
- }
- return list;
- }
-
- /// <summary>
- /// ??????????
- /// </summary>
- /// <param name="role"></param>
- /// <returns></returns>
- public IList<MembershipUser> GetRoleUsers(Member_Roles role)
- {
- string[] usersName = Roles.GetUsersInRole(role.ToString());
- IList<MembershipUser> userList = new List<MembershipUser>();
- foreach (string s in usersName)
- {
- userList.Add(Membership.GetUser(s));
- }
- return userList;
- }
-
- /// <summary>
- /// ??????????
- /// </summary>
- /// <param name="strUserName"></param>
- /// <returns></returns>
- public IList<MembershipUser> GetMembers(string strUserName)
- {
- IList<UserInfo> userList = Databases.DBGoShopping.Select(
- GoShoppingNBear.UserInfo).Where(GoShoppingNBear.UserInfo.Name.Like("%" + strUserName + "%")).ToList<UserInfo>();
- IList<MembershipUser> list = new List<MembershipUser>();
- foreach (UserInfo u in userList)
- {
- list.Add(Membership.GetUser(u.Name));
- }
- return list;
- }
-
-
- /// <summary>
- /// ????
- /// ???????????????
- /// </summary>
- /// <param name="userName">Member??</param>
- /// <param name="eMail"></param>
- /// <param name="password">??</param>
- /// <param name="passwordQustion"></param>
- /// <param name="passwordanser">????</param>
- /// <param name="IsApproved">????</param>
- /// <param name="role">??</param>
- /// <param name="createStatue">???????????</param>
- /// <returns>??????Ture??????False???createStatue????????</returns>
- public bool CreateMember(string userName, string eMail, string password, string passwordQustion, string passwordanser, bool IsApproved, Member_Roles role, out MembershipCreateStatus createStatue)
- {
- bool returnValue;
- using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
- {
- MembershipUser user = Membership.CreateUser(userName, password, eMail, passwordQustion,
- passwordanser, IsApproved,
- out createStatue);
- if (createStatue == MembershipCreateStatus.Success)
- {
- Roles.AddUserToRole(user.UserName, role.ToString());
- returnValue = true;
- }
- else
- {
- returnValue = false;
- if (user != null)
- {
- DeleteMember(user.UserName);
- }
- }
- //????????????????????
- if (returnValue == true)
- {
- IMember member = Factory_Member.CreateMemberOption(role);
- member.AddNewMember(user);
- }
- scope.Complete();
- }
- return returnValue;
- }
-
- /// <summary>
- /// ????
- /// </summary>
- /// <param name="userName"></param>
- /// <returns></returns>
- public bool DeleteMember(string userName)
- {
- bool returnValue;
- using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
- {
- IList<Member_Roles> roleList = this.GetMemberRoleList(userName);
- returnValue = Membership.DeleteUser(userName);
- //????????????????????
- if (returnValue == true)
- {//????????????????
- foreach (Member_Roles role in roleList)
- {
- IMember member = Factory_Member.CreateMemberOption(role);
- member.DeleteMember(userName);
- }
- }
- scope.Complete();
- }
- return returnValue;
- }
-
- /// <summary>
- /// ????????????
- /// </summary>
- /// <param name="role"></param>
- private void DeleteRoleUsers(Member_Roles role)
- {
- string[] usernames = Roles.GetUsersInRole(role.ToString());
- foreach (string user in usernames)
- {
- Membership.DeleteUser(user);
- }
- }
-
- #region ?????
-
- /// <summary>
- /// ??????Email????????
- /// ????
- /// </summary>
- /// <param name="email"></param>
- /// <param name="emailPassword"></param>
- /// <param name="statue"></param>
- /// <returns></returns>
- public bool CreateSuperManager(string emailUserName, string emailPassword, out MembershipCreateStatus statue)
- {
- bool returnValue;
- //????
- using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
- {
- DeleteRoleUsers(Member_Roles.Role_SuperManager);
-
- string UserName = emailUserName;
- string Email = emailUserName;
- string PasswordQuestion = "Email?";
- this.CreateMember(UserName, emailUserName, emailPassword, PasswordQuestion, emailUserName, true, Member_Roles.Role_SuperManager, out statue);
- if (statue == MembershipCreateStatus.Success)
- {
- returnValue = true;
- }
- else
- {
- returnValue = false;
- }
-
- scope.Complete();
- }//using scope
- return returnValue;
- }
- #endregion
- }
- }