Esempio n. 1
0
        public async void SendPasswordRecoveryEmail(User userParam, AppSettings settings)
        {
            var builder = new DbContextOptionsBuilder <VehicleHistoryContext>();

            builder.UseSqlServer(settings.ConnectionString);
            using (var context = new VehicleHistoryContext(builder.Options))
            {
                var generatedPassword = Crypto.GenerateRendomPassword();
                Crypto.CreatePasswordHash(generatedPassword, out var generatedPassHash, out var generatedPassSalt);
                var user = context.Users.FirstOrDefault(x => x.Id.ToString() == userParam.Id.ToString());
                if (user == null)
                {
                    throw new AppException("An error occured when trying to get the user object from the database.");
                }
                user.PasswordHash           = generatedPassHash;
                user.PasswordSalt           = generatedPassSalt;
                user.PasswordRecoveryActive = true;

                var emailSubject = "Your Password has been reset";
                var emailBody    = $"Use the following password the next time you log in: <b>{generatedPassword}</b>. " +
                                   $"You will be prompted to change the password when you log in.";

                await _emailSender.SendEmailAsync(user.Email, emailSubject, emailBody);

                await context.SaveChangesAsync();
            }
        }
Esempio n. 2
0
        public async void AddEmployee(User user, AppSettings settings)
        {
            if (_context.Users.Any(x => x.Email == user.Email && !x.Archival))
            {
                throw new AppException("Specified E-Mail address is already taken");
            }

            var generatedPassword = Crypto.GenerateRendomPassword();

            Crypto.CreatePasswordHash(generatedPassword, out var generatedPassHash, out var generatedPassSalt);
            user.PasswordHash           = generatedPassHash;
            user.PasswordSalt           = generatedPassSalt;
            user.PasswordRecoveryActive = true;

            _context.Users.Add(user);
            _context.SaveChanges();

            var builder = new DbContextOptionsBuilder <VehicleHistoryContext>();

            builder.UseSqlServer(settings.ConnectionString);
            using (var context = new VehicleHistoryContext(builder.Options))
            {
                var emailSubject = "Your Account has been created";
                var emailBody    = $"Use the following password the first time you log in: <b>{generatedPassword}</b>. " +
                                   $"You will be prompted to change the password when you log in.";

                var client  = new SendGridClient(settings.SendGridKey);
                var message = new SendGridMessage
                {
                    From        = new EmailAddress("*****@*****.**", "Vehicle History Account Management"),
                    Subject     = emailSubject,
                    HtmlContent = emailBody
                };

                message.AddTo(new EmailAddress(user.Email));
                await client.SendEmailAsync(message);

                await context.SaveChangesAsync();
            }
        }
Esempio n. 3
0
 public VehicleRecordsService(VehicleHistoryContext context)
 {
     _context = context;
 }
Esempio n. 4
0
 public DictionaryService(VehicleHistoryContext context)
 {
     AllItems = context.DictionaryItems.ToList();
 }
Esempio n. 5
0
        public async void HandleApplication(bool accepted, string id, AppSettings settings)
        {
            var application = _context.LocationApplications.FirstOrDefault(x => x.Id == Guid.Parse(id));

            if (application == null)
            {
                throw new AppException("Application not found");
            }

            application.Status = accepted ? 1 : -1;

            _context.SaveChanges();

            if (accepted)
            {
                var newLocation = new Location
                {
                    ApartmentNumber = application.ApartmentNumber,
                    Line1           = application.Line1,
                    Line2           = application.Line2,
                    LocationType    = application.LocationType,
                    Name            = application.Name,
                    ZipCode         = application.ZipCode
                };
                _context.Locations.Add(newLocation);
                var generatedPassword = Crypto.GenerateRendomPassword();
                Crypto.CreatePasswordHash(generatedPassword, out var generatedPassHash, out var generatedPassSalt);
                var builder = new DbContextOptionsBuilder <VehicleHistoryContext>();
                builder.UseSqlServer(settings.ConnectionString);
                using (var context = new VehicleHistoryContext(builder.Options))
                {
                    context.Users.Add(new User
                    {
                        Location               = newLocation,
                        FirstName              = "undefined",
                        LastName               = "undefined",
                        Email                  = application.Email,
                        Group                  = UserGroups.ShopOwner,
                        PasswordHash           = generatedPassHash,
                        PasswordSalt           = generatedPassSalt,
                        PasswordRecoveryActive = true
                    });

                    var emailSubject = "Your application has been accepted";
                    var emailBody    = $"Use the following password the next time you log in: <b>{generatedPassword}</b>. " +
                                       $"You will be prompted to change the password when you log in.";

                    var client  = new SendGridClient(settings.SendGridKey);
                    var message = new SendGridMessage
                    {
                        From        = new EmailAddress("*****@*****.**", "Vehicle History Account Management"),
                        Subject     = emailSubject,
                        HtmlContent = emailBody
                    };

                    message.AddTo(new EmailAddress(application.Email));
                    await client.SendEmailAsync(message);

                    await context.SaveChangesAsync();
                }
            }
            else
            {
                var builder = new DbContextOptionsBuilder <VehicleHistoryContext>();
                builder.UseSqlServer(settings.ConnectionString);
                using (var context = new VehicleHistoryContext(builder.Options))
                {
                    var emailSubject = "Your application has been rejected";
                    var emailBody    = $"You can try applying again or contacting the administrators if you believe this decision is unwarranted.";

                    var client  = new SendGridClient(settings.SendGridKey);
                    var message = new SendGridMessage
                    {
                        From        = new EmailAddress("*****@*****.**", "Vehicle History Account Management"),
                        Subject     = emailSubject,
                        HtmlContent = emailBody
                    };

                    message.AddTo(new EmailAddress(application.Email));
                    await client.SendEmailAsync(message);

                    await context.SaveChangesAsync();
                }
            }
        }
Esempio n. 6
0
 public LocationsService(VehicleHistoryContext context)
 {
     _context = context;
 }
Esempio n. 7
0
 public UsersService(VehicleHistoryContext context, IEmailSender emailSender)
 {
     _context     = context;
     _emailSender = emailSender;
 }