//returns 0 if admin or client not found public static int GetCurrentClientID(Controller c) { var db = new ClientsDataContext(); var results = db.S1_Clients_GetClientsFromUser(GetCurrentUserIDFromName(c.User.Identity.Name)).SingleOrDefault(); return(results.ClientID); }
public static int GetParentClientIDFromUser(int _UserID) { int?clientID = null; var dc = new ClientsDataContext(); var result = dc.S1_Clients_GetClientsFromUser(_UserID); foreach (var item in result) { clientID = item.ClientID; if (item.ParentClientID == null) { return(item.ClientID); } } if (clientID.HasValue) { int?parentClientID = new int?(); var result2 = dc.S1_Clients_GetClientParentWithOutput(clientID.Value, ref parentClientID); if (parentClientID.HasValue) { return(parentClientID.Value); } } return(0); }
// End Client Vendor Settings public static List <int> GetClientIDsFromUser(int _UserID) { List <int> outList = new List <int>(); var dc = new ClientsDataContext(); var result = dc.S1_Clients_GetClientsFromUser(_UserID); foreach (var item in result) { outList.Add(item.ClientID); } return(outList); }
public static S1_Clients_GetClientsFromUserResult GetParentClientFromUser(int _UserID) { var dc = new ClientsDataContext(); var result = dc.S1_Clients_GetClientsFromUser(_UserID); foreach (var item in result) { if (item.ParentClientID == null) { return((S1_Clients_GetClientsFromUserResult)item); } } return(null); }
public static List <SelectListItem> GetClientListFromUser(int _UserID) { List <SelectListItem> clientlist = new List <SelectListItem>(); var dc = new ClientsDataContext(); var result = dc.S1_Clients_GetClientsFromUser(_UserID); foreach (var item in result) { SelectListItem listitem = new SelectListItem(); listitem.Value = item.ClientID.ToString(); listitem.Text = item.ClientName; clientlist.Add(listitem); } return(clientlist); }
public ActionResult CreateUsers(string myname) { var dbc = new ClientContactsDataContext(); var resultc = dbc.S1_MassCreateUsers_GetClientContacts().ToList(); bool CreateEmail; string username; string useremail; int newuserid = 0; foreach (var item in resultc) { if (String.IsNullOrEmpty(item.ClientContactEmail)) { username = System.Guid.NewGuid().ToString() + "@" + ConfigurationManager.AppSettings["companyname"] + ".com"; useremail = username; CreateEmail = false; } else { Regex emailregex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,7}$"); Match m = emailregex.Match(item.ClientContactEmail); if (m.Success == false) { ViewData.ModelState.AddModelError("ErrorMessages", "Invalid email format. ClientContactID - " + item.ClientContactID); CreateEmail = false; } username = item.ClientContactEmail; useremail = item.ClientContactEmail; if (item.ClientContactStatus == true) { CreateEmail = true; } else { CreateEmail = false; } } CreateEmail = false; if (ViewData.ModelState.IsValid) { string ReturnURL = "/Security/ChangePassword"; string clientname = null; string clientaddr1 = null; string clientaddr2 = null; string clientcity = null; string clientstate = null; string clientzipcode = null; string contactpassword = item.ClientContactLastName.Substring(0, 3) + item.ClientContactZIP.Substring(0, 3) + item.ClientContactState; //CHECK TO SEE IF THE USER ALREADY EXISTS var dbu = new UsersDataContext(); var resultu = dbu.S1_Users_GetUserByEmail(item.ClientContactEmail).SingleOrDefault(); if (resultu == null) { //CREATE THE USER ACCOUNT // Attempt to register the user MembershipCreateStatus createStatus; MembershipUser newUser; if (Convert.ToBoolean(item.ClientContactStatus)) //create a user for an active clientcontact record { newUser = Provider.CreateUser(username, contactpassword, useremail, "client", "client", true, null, out createStatus); } else //create the user, but set IsActive to false on ASPNet security { newUser = Provider.CreateUser(username, contactpassword, useremail, "client", "client", false, null, out createStatus); } if (newUser != null) { var db = new UsersDataContext(); var result = db.S1_Users_CreateUser(username, item.ClientID, 1, item.ClientContactFirstName, item.ClientContactLastName).SingleOrDefault(); if (result.UserID > 0) { newuserid = Convert.ToInt32(result.UserID); Roles.AddUserToRole(username, "Client"); Roles.AddUserToRole(username, "Client_Billing"); Provider.ChangePasswordQuestionAndAnswer(username, contactpassword, "What is your Company's zipcode?", item.ClientContactZIP); if (CreateEmail) { var db3 = new ClientsDataContext(); var result3 = db3.S1_Clients_GetClientsFromUser(result.UserID).SingleOrDefault(); if (result3 == null) { clientname = ""; clientaddr1 = ""; clientaddr2 = ""; clientcity = ""; clientstate = ""; clientzipcode = ""; } else { clientname = result3.ClientName; clientaddr1 = result3.Address1; clientaddr2 = result3.Address2; clientcity = result3.City; clientstate = result3.State; clientzipcode = result3.ZipCode; } string subject = ConfigurationManager.AppSettings["CompanyName"] + " Billing: New Account for " + item.ClientContactFirstName + " " + item.ClientContactLastName + " - " + clientname; var db1 = new UsersDataContext(); //The following line is creating a message with a null status on purpose. Due to the actionGUID being used in the emails following. Note also this is S1_Users instead of S1_Messages var results1 = db.S1_Users_CreateMessageWithAction(1, subject, result.UserID, 1, 0, 3, HttpUtility.UrlDecode(ReturnURL), System.DateTime.Now, null).SingleOrDefault(); if (results1.ActionGUID != null) { Dictionary <string, string> messagevalues = new Dictionary <string, string>(); messagevalues.Add("[[USER_FIRSTNAME]]", ""); messagevalues.Add("[[USER_MI]]", ""); messagevalues.Add("[[USER_LASTNAME]]", ""); messagevalues.Add("[[USER_TITLE]]", ""); messagevalues.Add("[[COMPANY_ADDRESS_LINE1]]", clientaddr1); messagevalues.Add("[[COMPANY_ADDRESS_LINE2]]", clientaddr2); messagevalues.Add("[[COMPANY_ADDRESS_CITY]]", clientcity); messagevalues.Add("[[COMPANY_ADDRESS_STATE]]", clientstate); messagevalues.Add("[[COMPANY_ADDRESS_ZIPCODE]]", clientzipcode); messagevalues.Add("[[USER_EMAIL]]", item.ClientContactEmail); messagevalues.Add("[[USER_PHONE]]", ""); messagevalues.Add("[[USERNAME]]", username); messagevalues.Add("[[COMPANYNAME]]", clientname); messagevalues.Add("[[PASSWORD]]", contactpassword); messagevalues.Add("[[GUIDURL]]", System.Configuration.ConfigurationManager.AppSettings["DefaultPath"] + "/Account/ConfirmEmail/" + results1.ActionGUID.ToString() + "?portal=client"); var messageRecord = Messages.GetMessageTemplateRecord(0, "Create Client Account", messagevalues); string messagebody = messageRecord != null ? messageRecord.MessageText : null; if (messagebody != null) { int messageActionType = messageRecord.MessageActionTypeID.GetValueOrDefault(); int? MessageIDOutput = new int?(); Guid?MessageActionGuidOutput = new Guid?(); //Send email with UserName Messages.CreateMessageWithAction(messageActionType, subject, messagebody, result.UserID, 1, 0, 3, HttpUtility.UrlDecode(ReturnURL), System.DateTime.Now, null, "HTML", ref MessageIDOutput, ref MessageActionGuidOutput); Messages.UpdateMessageAndMarkForSending(MessageIDOutput.Value, subject, messagebody); subject = ConfigurationManager.AppSettings["CompanyName"] + " Billing: " + item.ClientContactFirstName + " " + item.ClientContactLastName + " - " + clientname; messageRecord = Messages.GetMessageTemplateRecord(0, "Create Client Password", messagevalues); messagebody = messageRecord != null ? messageRecord.MessageText : null; if (messagebody != null) { messageActionType = messageRecord.MessageActionTypeID.GetValueOrDefault(); MessageIDOutput = new int?(); MessageActionGuidOutput = new Guid?(); //Send email with Password Messages.CreateMessageWithAction(messageActionType, subject, messagebody, result.UserID, 1, 0, 3, HttpUtility.UrlDecode(ReturnURL), System.DateTime.Now, null, "HTML", ref MessageIDOutput, ref MessageActionGuidOutput); Messages.UpdateMessageAndMarkForSending(MessageIDOutput.Value, subject, messagebody); } //return View(viewClientContacts_Details); } else { ModelState.AddModelError("ErrorMessages", "There was a problem sending the confirmation email. Please re-create the account. "); return(View()); } } else { ModelState.AddModelError("ErrorMessages", "There was a problem sending the confirmation email. Please re-create the account."); return(View()); } } } else { ModelState.AddModelError("ErrorMessages", ErrorHandler.ErrorCodeToString(createStatus)); return(View()); } } else { ViewData.ModelState.AddModelError("ErrorMessages", ErrorHandler.ErrorCodeToString(createStatus)); return(View()); } } //END IF USER ALREADY EXISTS else { newuserid = Convert.ToInt32(resultu.UserID); } //NOW UPDATE THE CLIENT CONTACT var db6 = new ClientContactsDataContext(); var result6 = db6.S1_MassCreateUsers_UpdateClientContacts(item.ClientContactID, newuserid); if (result6 != 0) { ModelState.AddModelError("ErrorMessages", "Error attaching the UserID to the ClientContact"); return(View()); } } } ModelState.AddModelError("ErrorMessages", "UPDATE IS COMPLETE!!!"); return(View()); }
public JsonResult CreateClientUserPost(Security_Users viewSecuritySetup, FormCollection fc) { int newuserclientid = Convert.ToInt32(fc["primarycompany"]); string newuserrole = fc["usertypelist"]; // Basic parameter validation if (String.IsNullOrEmpty(viewSecuritySetup.firstname)) { ViewData.ModelState.AddModelError("firstname", " "); ViewData.ModelState.AddModelError("*", "Please enter a firstname."); } if (String.IsNullOrEmpty(viewSecuritySetup.lastname)) { ViewData.ModelState.AddModelError("lastname", " "); ViewData.ModelState.AddModelError("*", "Please enter a lastname."); } if (String.IsNullOrEmpty(viewSecuritySetup.username)) { ViewData.ModelState.AddModelError("username", " "); ViewData.ModelState.AddModelError("*", "Please enter a username."); } if (String.IsNullOrEmpty(viewSecuritySetup.email)) { ViewData.ModelState.AddModelError("email", " "); ViewData.ModelState.AddModelError("*", "Please enter an email address."); } else { Regex emailregex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,7}$"); Match m = emailregex.Match(viewSecuritySetup.email); if (m.Success == false) { ViewData.ModelState.AddModelError("email", " "); ViewData.ModelState.AddModelError("*", "Invalid email format."); } } if (viewSecuritySetup.password == null || viewSecuritySetup.password.Length < Provider.MinRequiredPasswordLength) { ViewData.ModelState.AddModelError("password", " "); ViewData.ModelState.AddModelError("*", String.Format(CultureInfo.InvariantCulture, "A password of {0} or more characters is required.", Provider.MinRequiredPasswordLength)); } if (!String.Equals(viewSecuritySetup.password, viewSecuritySetup.confirmPassword, StringComparison.Ordinal)) { ViewData.ModelState.AddModelError("confirmPassword", " "); ViewData.ModelState.AddModelError("*", "The password and confirmation do not match."); } if (ViewData.ModelState.IsValid) { string subject = ConfigurationManager.AppSettings["CompanyName"] + " Billing: New Account"; string clientname = null; string clientaddr1 = null; string clientaddr2 = null; string clientcity = null; string clientstate = null; string clientzipcode = null; // Attempt to register the user MembershipCreateStatus createStatus; MembershipUser newUser = Provider.CreateUser(viewSecuritySetup.username, viewSecuritySetup.password, viewSecuritySetup.email, "client", "client", true, null, out createStatus); if (newUser != null) { var db = new UsersDataContext(); var result = db.S1_Users_CreateUser(viewSecuritySetup.username, newuserclientid, 1, viewSecuritySetup.firstname, viewSecuritySetup.lastname).SingleOrDefault(); if (result.UserID > 0) { Roles.AddUserToRole(viewSecuritySetup.username, newuserrole); if (String.IsNullOrEmpty(HttpUtility.UrlDecode(viewSecuritySetup.ReturnUrl))) { viewSecuritySetup.ReturnUrl = "/Account/Logon?portal=admin"; } var db1 = new UsersDataContext(); //The following line is creating a message with a null status on purpose. Due to the actionGUID being used in the emails following. Note also this is S1_Users instead of S1_Messages var results1 = db.S1_Users_CreateMessageWithAction(1, subject, result.UserID, 1, 0, 3, HttpUtility.UrlDecode(viewSecuritySetup.ReturnUrl), System.DateTime.Now, null).SingleOrDefault(); if (results1.ActionGUID != null) { string ReturnURL = "/Account/ConfirmEmail"; var db3 = new ClientsDataContext(); var result3 = db3.S1_Clients_GetClientsFromUser(result.UserID).SingleOrDefault(); if (result3 == null) { //todo; } else { clientname = result3.ClientName; clientaddr1 = result3.Address1; clientaddr2 = result3.Address2; clientcity = result3.City; clientstate = result3.State; clientzipcode = result3.ZipCode; } Provider.ChangePasswordQuestionAndAnswer(viewSecuritySetup.username, viewSecuritySetup.password, "What is your Company's zipcode?", clientzipcode); Dictionary <string, string> messagevalues = new Dictionary <string, string>(); messagevalues.Add("[[USER_FIRSTNAME]]", ""); messagevalues.Add("[[USER_MI]]", ""); messagevalues.Add("[[USER_LASTNAME]]", ""); messagevalues.Add("[[USER_TITLE]]", ""); messagevalues.Add("[[COMPANY_ADDRESS_LINE1]]", clientaddr1); messagevalues.Add("[[COMPANY_ADDRESS_LINE2]]", clientaddr2); messagevalues.Add("[[COMPANY_ADDRESS_CITY]]", clientcity); messagevalues.Add("[[COMPANY_ADDRESS_STATE]]", clientstate); messagevalues.Add("[[COMPANY_ADDRESS_ZIPCODE]]", clientzipcode); messagevalues.Add("[[USER_EMAIL]]", viewSecuritySetup.email); messagevalues.Add("[[USER_PHONE]]", ""); messagevalues.Add("[[USERNAME]]", viewSecuritySetup.username); messagevalues.Add("[[COMPANYNAME]]", clientname); messagevalues.Add("[[GUIDURL]]", System.Configuration.ConfigurationManager.AppSettings["DefaultPath"] + ReturnURL + "/" + results1.ActionGUID.ToString() + "?portal=client"); messagevalues.Add("[[CORPORATENAME]]", System.Configuration.ConfigurationManager.AppSettings["CompanyName"]); MailGun.SendEmailToUserFromTemplate(10, 0, "Create Client Account", 0, result.UserID.Value, 0, subject, messagevalues); return(new JsonResult { Data = new { success = true } }); /*var messageRecord = Messages.GetMessageTemplateRecord(0, "Create Client Account", messagevalues); * * string messagebody = messageRecord != null ? messageRecord.MessageText : null; * * if (messagebody != null) * { * int messageActionType = messageRecord.MessageActionTypeID.GetValueOrDefault(); * int? MessageIDOutput = new int?(); * Guid? MessageActionGuidOutput = new Guid?(); * * Messages.CreateMessageWithAction(messageActionType, subject, messagebody, result.UserID, 1, 0, 3, HttpUtility.UrlDecode(ReturnURL), System.DateTime.Now, null, "HTML", ref MessageIDOutput, ref MessageActionGuidOutput); * Messages.UpdateMessageAndMarkForSending(MessageIDOutput.Value, subject, messagebody); * * return new JsonResult { Data = new { success = true } }; * } * else * { * ModelState.AddModelError("*", "There was a problem sending the confirmation email. Please re-create a new account. We apologize for the inconvenience."); * HandleCreateClientUserErrors(viewSecuritySetup, fc); * return new JsonResult * { * Data = new * { * success = false, * view = RenderToString.RenderViewToString(this, "CreateClientUser", viewSecuritySetup) * } * }; * }*/ } ModelState.AddModelError("*", "There was a problem sending the confirmation email. Please re-create a new account. We apologize for the inconvenience."); HandleCreateClientUserErrors(viewSecuritySetup, fc); return(new JsonResult { Data = new { success = false, view = RenderToString.RenderViewToString(this, "CreateClientUser", viewSecuritySetup) } }); } else { ModelState.AddModelError("*", ErrorHandler.ErrorCodeToString(createStatus)); HandleCreateClientUserErrors(viewSecuritySetup, fc); return(new JsonResult { Data = new { success = false, view = RenderToString.RenderViewToString(this, "CreateClientUser", viewSecuritySetup) } }); } } else { ViewData.ModelState.AddModelError("*", ErrorHandler.ErrorCodeToString(createStatus)); HandleCreateClientUserErrors(viewSecuritySetup, fc); return(new JsonResult { Data = new { success = false, view = RenderToString.RenderViewToString(this, "CreateClientUser", viewSecuritySetup) } }); } } return(new JsonResult { Data = new { success = false, view = RenderToString.RenderViewToString(this, "CreateClientUser", viewSecuritySetup) } }); //return new JsonResult { Data = new { success = true } }; }