Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Net;
- using System.Web.Security;
- using SimpleWorld.DataAccessLayer;
- using System.Collections;
- namespace SimpleWorld.Web.Bookings {
- public static class LoginProvider {
- #region Private Fields
- private const string CYPHER = "Simple Salon is the greatest";
- public const string ERROR_LoginError = "Please try again later. An error has occurred.";
- public const string ERROR_WrongIDorPassword = "Incorrect User ID or Password.<br>Please try again...";
- #endregion
- #region Public Properties
- public static OnlineClient CurrentUser {
- get {
- return HttpContext.Current.Session["user"] as OnlineClient;
- }
- set {
- if (value != null && HttpContext.Current.Session["user"] == null)
- HttpContext.Current.Session["user"] = value;
- }
- }
- #endregion Public Properties
- public static void Login(string clientToken, bool isPersistent, ref string failureText) {
- CompanyUtils companyUtils = new CompanyUtils();
- ClientUtils clientUtils = new ClientUtils();
- if (!string.IsNullOrEmpty(clientToken)) {
- // check for parts
- string[] bits = clientToken.Split('|');
- if (bits != null && bits.Length == 2) {
- // get token
- Token token = companyUtils.tokenForID(bits[0]);
- if (token != null) {
- // check if guid matches
- if (!string.IsNullOrEmpty(token.GUID) && token.GUID == bits[1]) {
- // all bits good
- if (DateTime.Now < token.CreateDate.AddHours(24)) {
- // get user and add to session
- Client client = clientUtils.clientForID(token.UserID);
- if (client != null && client.CompanyID != null && client.CompanyID.Length > 0) {
- // convert SS Client to Online Client
- OnlineClient result = new OnlineClient();
- result.ID = client.ClientID;
- result.Mobile = client.Mobile;
- result.Email = client.Email;
- result.CompanyID = client.CompanyID;
- result.DisplayName = client.DisplayName;
- result.Password = client.InternetPassword;
- // get client company details
- CompanyUtils compUtils = new CompanyUtils();
- Company company = compUtils.companyForID(client.CompanyID);
- if (company != null) {
- result.BusinessName = company.BusinessName;
- result.BusinessAddress = company.Address;
- result.BusinessSuburb = company.Suburb;
- result.BusinessPostcode = company.Postcode;
- result.BusinessTelephone = company.Telephone;
- result.CompanyMessage = company.CompanyMessage;
- }
- CurrentUser = result;
- FormsAuthentication.SetAuthCookie(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
- FormsAuthentication.RedirectFromLoginPage(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
- } else {
- failureText = ERROR_WrongIDorPassword;
- }
- } else // remove token
- companyUtils.deleteToken(token);
- }
- }
- }
- }
- }
- public static void Login(string username, string password, bool isPersistent, ref string failureText) {
- if (username != null && username.Length > 0 && password != null && password.Length > 0) {
- // get system code off front of username
- string systemCode = username.Substring(0, 1);
- // remove code from username
- username = username.Substring(1);
- if (systemCode.ToLower() == "s") {
- // Simple Salon
- ClientUtils cu = new ClientUtils();
- Client client = cu.clientForID(username);
- string decryptedPassword = null;
- if (client.InternetPassword != null && client.InternetPassword.Length > 0) {
- decryptedPassword = Utils.Decrypt(client.InternetPassword, CYPHER);
- }
- if (client != null && client.CompanyID != null && client.CompanyID.Length > 0 && decryptedPassword != null && decryptedPassword == password) {
- // convert SS Client to Online Client
- OnlineClient result = new OnlineClient();
- result.ID = client.ClientID;
- result.Mobile = client.Mobile;
- result.Email = client.Email;
- result.CompanyID = client.CompanyID;
- result.DisplayName = client.DisplayName;
- result.Password = client.InternetPassword;
- // get client company details
- CompanyUtils compUtils = new CompanyUtils();
- Company company = compUtils.companyForID(client.CompanyID);
- if (company != null) {
- result.BusinessName = company.BusinessName;
- result.BusinessAddress = company.Address;
- result.BusinessSuburb = company.Suburb;
- result.BusinessPostcode = company.Postcode;
- result.BusinessTelephone = company.Telephone;
- result.CompanyMessage = company.CompanyMessage;
- }
- CurrentUser = result;
- FormsAuthentication.SetAuthCookie(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
- FormsAuthentication.RedirectFromLoginPage(String.Format("S{0}&{1}", CurrentUser.ID, CurrentUser.Password), isPersistent);
- } else {
- failureText = ERROR_WrongIDorPassword;
- }
- }
- }
- }
- public static void LoadCurrentUser() {
- HttpCookie auth_cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
- if (auth_cookie != null) {
- if (auth_cookie.Value != null) {
- string login_str = FormsAuthentication.Decrypt(auth_cookie.Value).Name;
- if (!string.IsNullOrEmpty(login_str)) {
- string[] lex = login_str.Split(new char[] { '&' });
- if (lex.Length == 2) {
- string login = lex[0];
- string pass = lex[1];
- string err = string.Empty;
- try {
- Login(login, pass, false, ref err);
- } catch {
- }
- }
- }
- }
- }
- }
- public static void Logout() {
- FormsAuthentication.SignOut();
- HttpContext.Current.Session.Clear();
- HttpContext.Current.Session.Abandon();
- CurrentUser = null;
- FormsAuthentication.RedirectToLoginPage();
- }
- public static string ForgotPassword(string clientId) {
- string result = String.Empty;
- if (clientId != null && clientId.Length > 0) {
- // remove code from username
- clientId = clientId.Substring(1);
- ClientUtils cu = new ClientUtils();
- CampaignUtils campaignUtils = new CampaignUtils();
- Client client = cu.clientForID(clientId);
- User user = new User();
- user.CompanyID = client.CompanyID;
- cu.SessionUser = user;
- campaignUtils.SessionUser = user;
- if (client != null) {
- ArrayList clientList = new ArrayList();
- clientList.Add(client);
- string[] campIDList = campaignUtils.sendOnlineBookingsToClientList(clientList, "email", false);
- // send campaign now
- if (campIDList != null && campIDList.Length == 2 && !string.IsNullOrEmpty(campIDList[1])) {
- campaignUtils.sendCampaignForID(campIDList[1]);
- }
- result = "Password sent!";
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement