public AssociationUser SaveAssociationUser(AssociationUser user) { try { string email = user.Email != null ? user.Email.ToLower().Trim() : null; using (var db = new LomsContext()) { db.Connection.Open(); using (var transaction = db.Connection.BeginTransaction()) { if (!string.IsNullOrWhiteSpace(email)) { //check id user with such email existed already var existedUser = (from u in db.AssociationUsers where u.AssociationId == CurrentAssociationId && u.Email == email && (user.Id == 0 || u.Id != user.Id) select u).SingleOrDefault(); if (existedUser != null) { user.AddError("Email", "User with such email is already registered!"); return user; } user.Email = email; } else user.Email = null; user.AssociationId = CurrentAssociationId; user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow; db.AssociationUsers.ApplyChanges(user); db.SaveChanges(); if (!string.IsNullOrWhiteSpace(user.Email)) { AssociationUserActivation activation = new AssociationUserActivation(); activation.UserId = user.Id; activation.Guid = Guid.NewGuid(); activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0); //expiry db.AssociationUserActivations.ApplyChanges(activation); db.SaveChanges(); var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId); if (emailProvider != null) { var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId); var uri = HttpContext.Current.Request.Url; string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port); string activtionLink = Path.Combine(baseUrl + string.Format("/#Activation/{0}", activation.Guid.ToString("D"))); string contactUsLink = Path.Combine(baseUrl + "/#Contact"); string termAndConditionsLink = Path.Combine(baseUrl + "/terms"); var emailTemplate = new EmailTemplate("StaffAdminAddsNewManagedProfileWithEmail"); emailTemplate["UserName"] = user.FullName.ToUpper(); emailTemplate["AssociationName"] = association.Name.ToUpper(); emailTemplate["ActivationLink"] = activtionLink; emailTemplate["ContactUsLink"] = contactUsLink; emailTemplate["TermAndConditionLink"] = termAndConditionsLink; var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html); emailProvider.SendMail(user.Email, association.Name + " Account activation", emailTemplate.Txt, null, avBody, true); } } transaction.Commit(); } } using (var db = new LomsContext()) { var entity = db.AssociationUsers.IncludeAll("Country").Single(u => u.Id == user.Id); return entity; } } catch (Exception ex) { StringBuilder builder = new StringBuilder(); builder.AppendLine(ex.Message); if (ex.InnerException != null) { builder.AppendLine(ex.InnerException.Message); if (ex.InnerException.InnerException != null) builder.AppendLine(ex.InnerException.InnerException.Message); } user.AddError("Error", builder.ToString()); return user; } }
public AssociationUser SaveProfile(AssociationUser profile) { using (TransactionScope scope = new TransactionScope()) using (var db = new LomsContext()) { if (!HasUniqueEmail(profile)) { profile.AddError("Error", "The email you have entered is already in use on the system. Please use a unique email address, or call us for assistance."); // TODO: Miguel. Change this based on Andrew's feedback (something like If you think this is an error, please contact) return profile; } bool isManager = profile.TravelAgencyRole != null; if (profile.Id == 0) { profile.AssociationId = CurrentAssociationId; profile.CreatedTime = profile.LastUpdatedTime = DateTime.UtcNow; } if (profile.Email != null) profile.Email = profile.Email.Trim(); profile.LastUpdatedTime = DateTime.UtcNow; db.AssociationUsers.ApplyChanges(profile); db.SaveChanges(); if (isManager) profile = db.AssociationUsers.IncludeAll("Country", "Activation", "TravelAgencyRole", "TravelAgencyRole.TravelAgency", "TravelAgencyRole.TravelAgency.Country", "TravelAgencyRole.TravelAgency.State", "TravelAgencyRole.TravelAgency.State.Country", "TravelAgencyRole.TravelAgency.Suburb", "TravelAgencyRole.TravelAgency.Suburb.Country", "TravelAgencyRole.TravelAgency.Suburb.State", "TravelAgencyRole.TravelAgency.Suburb.State.Country").FirstOrDefault(u => u.Id == profile.Id); else profile = db.AssociationUsers.IncludeAll("Country", "Activation").FirstOrDefault(u => u.Id == profile.Id); //onlie access if (profile != null && !string.IsNullOrWhiteSpace(profile.Email) && profile.Activation == null && !profile.HasOnlineAccess) { //start activation process AssociationUserActivation activation = new AssociationUserActivation(); activation.UserId = profile.Id; activation.Guid = Guid.NewGuid(); activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0); //expiry db.AssociationUserActivations.ApplyChanges(activation); db.SaveChanges(); //send email var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == profile.AssociationId); if (emailProvider != null) { var association = db.Associations.FirstOrDefault(a => a.Id == profile.AssociationId); var request = HttpContext.Current.Request; var uri = request.Url; string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port); string activtionLink = Path.Combine(baseUrl + string.Format("/#Activation/{0}", activation.Guid.ToString("D"))); string contactUsLink = Path.Combine(baseUrl + "/#Contact"); string termAndConditionsLink = Path.Combine(baseUrl + "/terms"); var manager = db.AssociationUsers.FirstOrDefault(u => u.Id == profile.ManagerId); if (profile.IataNumber != null) { var travelAgencyName = (from a in db.AssociationUserAddresses where a.AssociationUserId == profile.Id && a.TypeId == (int)AddressType.Work select a.BusinessName).SingleOrDefault(); var txtContent = MailTemplateHelper.GetByTravelAgentRegistrationActivationTxtContent(association.Name.ToUpper(), profile.FullName.ToUpper(), activtionLink, contactUsLink, manager.FullName, manager.Email, termAndConditionsLink, travelAgencyName); var htmlContent = MailTemplateHelper.GetByTravelAgentRegistrationActivationHtmlContent(association.Name, profile.FullName, activtionLink, contactUsLink, manager.FullName, manager.Email, termAndConditionsLink, travelAgencyName); var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html); emailProvider.SendMail(profile.Email, association.Name.ToUpper() + " Account activation", txtContent, null, avBody, true); } else { var txtContent = MailTemplateHelper.GetByGeneralUserRegistrationActivationTxtContent(association.Name.ToUpper(), profile.FullName.ToUpper(), activtionLink, contactUsLink, manager.FullName, manager.Email, termAndConditionsLink); var htmlContent = MailTemplateHelper.GetByGeneralUserRegistrationActivationHtmlContent(association.Name, profile.FullName, activtionLink, contactUsLink, manager.FullName, manager.Email, termAndConditionsLink); var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html); try { emailProvider.SendMail(profile.Email, association.Name.ToUpper() + " Account activation", txtContent, null, avBody, true); } catch { } } } } scope.Complete(); } profile.AcceptChanges(); return profile; }
public AssociationUser SaveAssociationUser(AssociationUser user) { if (!Roles.IsUserInRole(RoleName.StaffUser)) throw new SecurityException(); using (TransactionScope scope = new TransactionScope()) using (var db = new LomsContext()) { if (!HasUniqueEmail(user)) { user.AddError("Error", "The email you have entered is already in use on the system. Please use a unique email address, or call us for assistance."); // TODO: Miguel. Change this based on Andrew's feedback (something like If you think this is an error, please contact) return user; } user.AssociationId = CurrentAssociationId; if (user.Id == 0) user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow; user.LastUpdatedTime = DateTime.UtcNow; db.AssociationUsers.ApplyChanges(user); db.SaveChanges(); user = db.AssociationUsers.IncludeAll("Country", "Activation").FirstOrDefault(u => u.Id == user.Id); scope.Complete(); } return user; }