Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Net;
  6. using System.Web.Security;
  7. using SimpleWorld.DataAccessLayer;
  8. using System.Collections;
  9.  
  10. namespace SimpleWorld.Web.Bookings {
  11.  
  12. public static class LoginProvider {
  13.  
  14. #region Private Fields
  15.  
  16. private const string CYPHER = "Simple Salon is the greatest";
  17.  
  18. public const string ERROR_LoginError = "Please try again later. An error has occurred.";
  19. public const string ERROR_WrongIDorPassword = "Incorrect User ID or Password.<br>Please try again...";
  20.  
  21. #endregion
  22.  
  23.  
  24. #region Public Properties
  25.  
  26. public static OnlineClient CurrentUser {
  27. get {
  28. return HttpContext.Current.Session["user"] as OnlineClient;
  29. }
  30. set {
  31. if (value != null && HttpContext.Current.Session["user"] == null)
  32. HttpContext.Current.Session["user"] = value;
  33. }
  34. }
  35. #endregion Public Properties
  36.  
  37. public static void Login(string clientToken, bool isPersistent, ref string failureText) {
  38.  
  39. CompanyUtils companyUtils = new CompanyUtils();
  40. ClientUtils clientUtils = new ClientUtils();
  41.  
  42. if (!string.IsNullOrEmpty(clientToken)) {
  43. // check for parts
  44. string[] bits = clientToken.Split('|');
  45.  
  46. if (bits != null && bits.Length == 2) {
  47. // get token
  48. Token token = companyUtils.tokenForID(bits[0]);
  49.  
  50. if (token != null) {
  51. // check if guid matches
  52. if (!string.IsNullOrEmpty(token.GUID) && token.GUID == bits[1]) {
  53. // all bits good
  54.  
  55. if (DateTime.Now < token.CreateDate.AddHours(24)) {
  56.  
  57. // get user and add to session
  58. Client client = clientUtils.clientForID(token.UserID);
  59.  
  60.  
  61. if (client != null && client.CompanyID != null && client.CompanyID.Length > 0) {
  62.  
  63.  
  64. // convert SS Client to Online Client
  65. OnlineClient result = new OnlineClient();
  66. result.ID = client.ClientID;
  67. result.Mobile = client.Mobile;
  68. result.Email = client.Email;
  69. result.CompanyID = client.CompanyID;
  70. result.DisplayName = client.DisplayName;
  71. result.Password = client.InternetPassword;
  72.  
  73. // get client company details
  74. CompanyUtils compUtils = new CompanyUtils();
  75. Company company = compUtils.companyForID(client.CompanyID);
  76.  
  77. if (company != null) {
  78. result.BusinessName = company.BusinessName;
  79. result.BusinessAddress = company.Address;
  80. result.BusinessSuburb = company.Suburb;
  81. result.BusinessPostcode = company.Postcode;
  82. result.BusinessTelephone = company.Telephone;
  83. result.CompanyMessage = company.CompanyMessage;
  84. }
  85.  
  86. CurrentUser = result;
  87.  
  88. FormsAuthentication.SetAuthCookie(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
  89. FormsAuthentication.RedirectFromLoginPage(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
  90.  
  91. } else {
  92. failureText = ERROR_WrongIDorPassword;
  93. }
  94.  
  95. } else // remove token
  96. companyUtils.deleteToken(token);
  97. }
  98. }
  99. }
  100. }
  101. }
  102.  
  103. public static void Login(string username, string password, bool isPersistent, ref string failureText) {
  104.  
  105. if (username != null && username.Length > 0 && password != null && password.Length > 0) {
  106. // get system code off front of username
  107. string systemCode = username.Substring(0, 1);
  108.  
  109. // remove code from username
  110. username = username.Substring(1);
  111.  
  112. if (systemCode.ToLower() == "s") {
  113. // Simple Salon
  114.  
  115. ClientUtils cu = new ClientUtils();
  116. Client client = cu.clientForID(username);
  117. string decryptedPassword = null;
  118. if (client.InternetPassword != null && client.InternetPassword.Length > 0) {
  119. decryptedPassword = Utils.Decrypt(client.InternetPassword, CYPHER);
  120. }
  121.  
  122. if (client != null && client.CompanyID != null && client.CompanyID.Length > 0 && decryptedPassword != null && decryptedPassword == password) {
  123.  
  124. // convert SS Client to Online Client
  125. OnlineClient result = new OnlineClient();
  126. result.ID = client.ClientID;
  127. result.Mobile = client.Mobile;
  128. result.Email = client.Email;
  129. result.CompanyID = client.CompanyID;
  130. result.DisplayName = client.DisplayName;
  131. result.Password = client.InternetPassword;
  132.  
  133. // get client company details
  134. CompanyUtils compUtils = new CompanyUtils();
  135. Company company = compUtils.companyForID(client.CompanyID);
  136.  
  137. if (company != null) {
  138. result.BusinessName = company.BusinessName;
  139. result.BusinessAddress = company.Address;
  140. result.BusinessSuburb = company.Suburb;
  141. result.BusinessPostcode = company.Postcode;
  142. result.BusinessTelephone = company.Telephone;
  143. result.CompanyMessage = company.CompanyMessage;
  144. }
  145.  
  146. CurrentUser = result;
  147.  
  148. FormsAuthentication.SetAuthCookie(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
  149. FormsAuthentication.RedirectFromLoginPage(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
  150.  
  151. } else {
  152. failureText = ERROR_WrongIDorPassword;
  153. }
  154.  
  155. }
  156. }
  157. }
  158.  
  159. public static void LoadCurrentUser() {
  160. HttpCookie auth_cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
  161. if (auth_cookie != null) {
  162. if (auth_cookie.Value != null) {
  163. string login_str = FormsAuthentication.Decrypt(auth_cookie.Value).Name;
  164. if (!string.IsNullOrEmpty(login_str)) {
  165. string[] lex = login_str.Split(new char[] { '&' });
  166. if (lex.Length == 2) {
  167. string login = lex[0];
  168. string pass = lex[1];
  169. string err = string.Empty;
  170. try {
  171. Login(login, pass, false, ref err);
  172. } catch {
  173.  
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180.  
  181. public static void Logout() {
  182. FormsAuthentication.SignOut();
  183. HttpContext.Current.Session.Clear();
  184. HttpContext.Current.Session.Abandon();
  185. CurrentUser = null;
  186. FormsAuthentication.RedirectToLoginPage();
  187. }
  188.  
  189. public static string ForgotPassword(string clientId) {
  190.  
  191. string result = String.Empty;
  192.  
  193. if (clientId != null && clientId.Length > 0) {
  194. // remove code from username
  195. clientId = clientId.Substring(1);
  196.  
  197. ClientUtils cu = new ClientUtils();
  198. CampaignUtils campaignUtils = new CampaignUtils();
  199. Client client = cu.clientForID(clientId);
  200. User user = new User();
  201. user.CompanyID = client.CompanyID;
  202. cu.SessionUser = user;
  203. campaignUtils.SessionUser = user;
  204.  
  205. if (client != null) {
  206. ArrayList clientList = new ArrayList();
  207. clientList.Add(client);
  208. string[] campIDList = campaignUtils.sendOnlineBookingsToClientList(clientList, "email", false);
  209.  
  210. // send campaign now
  211. if (campIDList != null && campIDList.Length == 2 && !string.IsNullOrEmpty(campIDList[1])) {
  212. campaignUtils.sendCampaignForID(campIDList[1]);
  213. }
  214.  
  215. result = "Password sent!";
  216.  
  217. }
  218. }
  219.  
  220. return result;
  221. }
  222.  
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement