/// <summary> /// </summary> public GenericUserResources(BusinessDbContext context, ILogger <GenericUserResources> logger, ValidationSender validation, ITokenMemoryCache memoryCache) { _context = context; _logger = logger; _validation = validation; _memoryCache = memoryCache; }
public NotificationResources(BusinessDbContext context, ILogger <NotificationResources> logger, ValidationSender validation, IHostingEnvironment hostingEnvironment, IConfigurationRoot configuration) { _context = context; _logger = logger; _validation = validation; _hostingEnvironment = hostingEnvironment; _configuration = configuration; }
private async Task Broadcast(string connectionString, ValidationSender sender, List <Rows> users, Notification notification) { var options = new DbContextOptionsBuilder <BusinessDbContext>(); options.UseNpgsql(connectionString); using (var context = new BusinessDbContext(options.Options)) { foreach (var user in users) { try { if (user.ValidatedSms && user.EnabledSms && !string.IsNullOrWhiteSpace(user.PhoneNumber)) { Log.Information("Sending out notification to {phone}", user.PhoneNumber); await sender.SendMSMessage(user.PhoneNumber, notification); context.NotificationLog.Add(new BroadCastLogEntry() { NotificationId = notification.Id.Value, UserId = user.Id, Type = LogType.Email }); } if (user.ValidatedEmail && user.EnabledEmail && !string.IsNullOrEmpty(user.Email)) { Log.Information("Sending out notification to {email}", user.Email); await sender.SendEmailMessage(user.Email, notification); context.NotificationLog.Add(new BroadCastLogEntry() { NotificationId = notification.Id.Value, UserId = user.Id, Type = LogType.Sms }); } context.SaveChanges(); } catch (Exception e) { Log.Error(e, "Error when broadcasting to user {user}", user); } } return; } }
public GenericUserAuth( BusinessDbContext context, ValidationSender validation, ILogger <GenericUserAuth> logger, ITokenMemoryCache memoryCache, TokenService tokenService, IHostingEnvironment hostingEnvironment, ExternalServicesConfig config) { _context = context; _validation = validation; _logger = logger; _tokenService = tokenService; _memoryCache = memoryCache; _hostingEnvironment = hostingEnvironment; _config = config; }
public async Task <GenericUser> Process(BusinessDbContext context, ValidationSender validationSender) { var user = GetUser(context); if (user == null) { throw new ProcessEventException(Constants.Messages.UserNotFound); } if (!string.IsNullOrWhiteSpace(Name)) { user.Name = Name; } if (!string.IsNullOrWhiteSpace(Email) && Email != user.Email) { await validationSender.SendValidationToEmail(new TempUser(user)); } if (!string.IsNullOrWhiteSpace(PhoneNumber) && PhoneNumber != user.PhoneNumber) { await validationSender.SendValidationToSms(new TempUser(user)); } if (string.IsNullOrWhiteSpace(NewPassowrd) || string.IsNullOrWhiteSpace(ExistingPassword)) { var check = user.VerifyPassowrd(ExistingPassword); if (check == PasswordVerificationResult.Success) { user.SetPassword(NewPassowrd); } } context.SaveChanges(); return(user); }
public async Task <GenericUser> Process(IHostingEnvironment hostingEnvironment, ITokenMemoryCache memoryCache, BusinessDbContext context, ValidationSender validation, TempUser tempUser) { using (var transaction = context.Database.BeginTransaction()) { tempUser.PhoneNumber = tempUser.PhoneNumber.CleanPhone(); // Check out our users, if we already someone, then no need to validate, its just an error var check = await context.Users.AnyAsync(u => (!string.IsNullOrEmpty(u.PhoneNumber) && (u.PhoneNumber == tempUser.PhoneNumber)) || (!string.IsNullOrEmpty(u.Email) && (u.Email == tempUser.Email))); if (check) { throw new ProcessEventException("You have already signed up. Please login instead"); } // Also need to check if their is any pending validation and in that case we would not take this info var waitingUeser = context.AllUsers.FirstOrDefault( u => ((!string.IsNullOrEmpty(u.PhoneNumber) && u.PhoneNumber == tempUser.PhoneNumber) || (!string.IsNullOrEmpty(u.Email) && u.Email == tempUser.Email)) && u.Enabled == false); if (waitingUeser != null) { try { if (!string.IsNullOrWhiteSpace(tempUser.Email)) { Log.Information("Sending Email Validation to {user}", tempUser); try { await validation.SendValidationToEmail(tempUser); } catch (Exception e) { Console.WriteLine(e); throw new ProcessEventException("Could not send verification to that email. Please use a different email address"); } memoryCache.SetForChallenge(tempUser); } else if (!string.IsNullOrWhiteSpace(tempUser.PhoneNumber)) { Log.Information("Sending SMS Validation to {user}", tempUser); try { await validation.SendValidationToSms(tempUser); } catch (Exception e) { Console.WriteLine(e); throw new ProcessEventException("Could not send a verification message to that phone number. Please use a number able to receive texts."); } memoryCache.SetForChallenge(tempUser); } } catch (Exception e) { Log.Error(e, "Error when sending out a validation email to an incoming user {email} , {phone}", tempUser.Email, tempUser.PhoneNumber); throw new ProcessEventException("Could not send out a validation link to the number or email provided"); } throw new ProcessEventException("Your already signed up, you just need to confirm your information. Check your inbox or text messages."); } // Create a temporary account now, just keep account disabled untill verified. // TODO: background cron job which purges accounts unvalidated after xxx amount of time var addr = new Address(tempUser); context.Address.Add(addr); var user = new GenericUser() { Name = tempUser.Name, Email = tempUser.Email, PhoneNumber = tempUser.PhoneNumber, Address = addr, UserName = string.IsNullOrEmpty(tempUser.Email) ? tempUser.PhoneNumber : tempUser.Email }; // Set our join date and last login user.JoinDate = user.LastLogin = DateTime.Now; context.AllUsers.Add(user); context.SaveChanges(); user.SetPassword(tempUser.Password); context.SaveChanges(); Log.Information("Created a new user, unvalidated {user}", user); // Need to set our tempUser id to our user tempUser.Id = user.Id; // TODO: Remove any chance of this obvious inserurity prior to real production usage via removing the check altogther. if (Constants.Testing.CheckIfOverride(tempUser) && (hostingEnvironment.IsDevelopment() || hostingEnvironment.IsEnvironment("Testing"))) { tempUser.Token = Constants.Testing.TestValidationToken; // Hold our token and tempUser for a while to give our user a chance to validate their info memoryCache.SetForChallenge(tempUser); transaction.Commit(); return(user); } // We prefer to send validation through email but will send through sms if needed if (!string.IsNullOrWhiteSpace(tempUser.Email)) { Log.Information("Sending Email Validation to {user}", user.Email); await validation.SendValidationToEmail(tempUser); memoryCache.SetForChallenge(tempUser); } else if (!string.IsNullOrWhiteSpace(tempUser.PhoneNumber)) { Log.Information("Sending SMS Validation to {user}", user.PhoneNumber); await validation.SendValidationToSms(tempUser); memoryCache.SetForChallenge(tempUser); } transaction.Commit(); return(user); } }
/* [DataMember(Name = "id")] * public long Id { get; set; }*/ public Notification Process(BusinessDbContext context, string adminId, ValidationSender sender, string connectionString, Notification notification) { var queryString = $@" SELECT DISTINCT ON(users.""Id"") users.""Id"", users.""ValidatedEmail"", users.""ValidatedSms"", users.""Enabled"", users.""Email"", users.""PhoneNumber"", users.""EnabledEmail"", users.""EnabledSms"", addr.""GeoLocation"", addr.""UserId"", noti.""AffectedArea"" FROM public.""Address"" addr, public.""AllUsers"" users, public.""Notifications"" noti where ST_Intersects(ST_SetSRID(noti.""AffectedArea"",4326),addr.""GeoLocation"") and users.""Id"" = addr.""UserId"" and noti.""Id"" = {notification.Id} "; var foundUsers = new List <Rows>(); try { using (var connection = context.Database.GetDbConnection()) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = queryString; using (var reader = command.ExecuteReader()) { while (reader.Read()) { foundUsers.Add(new Rows() { Id = (Guid)reader[0], ValidatedEmail = (bool)reader[1], ValidatedSms = (bool)reader[2], Enabled = (bool)reader[3], Email = (reader[4] ?? "").ToString(), PhoneNumber = (reader[5] ?? "").ToString(), EnabledEmail = (bool)reader[6], EnabledSms = (bool)reader[7] }); } } } context.Notifications.Update(notification); notification.Status = NotiStatus.Published; notification.Published = DateTime.Now; notification.PublishedById = new Guid(adminId); context.SaveChanges(); Log.Information("Sending out notification to {count} users", foundUsers.Count); // Use the notification bounds and find the users which fall into those bounds #pragma warning disable 4014 Broadcast(connectionString, sender, foundUsers, notification); #pragma warning restore 4014 // Use their communication information which has been validated to fire off the messages // Collect stats with the broadcasting return(notification); } } catch (Exception e) { Console.WriteLine(e); throw; } }