Beispiel #1
0
        public async Task <IActionResult> Register(RegisterNewUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userHelper.GetUserByEmailAsync(model.Username);

                if (user == null)
                {
                    var city = await _countryRepository.GetCityAsync(model.CityId);

                    user = new User
                    {
                        FirstName   = model.FirstName,
                        LastName    = model.LastName,
                        Email       = model.Username,
                        UserName    = model.Username,
                        Address     = model.Address,
                        PhoneNumber = model.PhoneNumber,
                        CityId      = model.CityId,
                        City        = city
                    };

                    var result = await _userHelper.AddUserAsync(user, model.Password);

                    if (result != IdentityResult.Success)
                    {
                        this.ModelState.AddModelError(string.Empty, "The user couldn't be created.");
                        return(this.View(model));
                    }
                    await _userHelper.AddUsertoRoleAsync(user, "Client");

                    var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                    var tokenLink = this.Url.Action("ConfirmEmail", "Account", new
                    {
                        userid = user.Id,
                        token  = myToken
                    }, protocol: HttpContext.Request.Scheme);

                    _mailHelper.SendMail(model.Username, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                         $"To allow the user, " +
                                         $"please click in this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>");
                    this.ViewBag.Message = "The instructions to allow your user has been sent to email.";


                    return(this.View(model));
                }

                this.ModelState.AddModelError(string.Empty, "The user already exists.");
            }

            return(View(model));
        }
Beispiel #2
0
        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();


            await _userHelper.CheckRoleAsync("Admin");

            await _userHelper.CheckRoleAsync("Customer");


            if (!_context.Countries.Any())
            {
                var cities = new List <City>();
                cities.Add(new City {
                    Name = "Lisboa"
                });
                cities.Add(new City {
                    Name = "Porto"
                });
                cities.Add(new City {
                    Name = "Coimbra"
                });
                cities.Add(new City {
                    Name = "Faro"
                });


                _context.Countries.Add(new Country
                {
                    Cities = cities,
                    Name   = "Portugal"
                });


                await _context.SaveChangesAsync();
            }



            var user = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new User
                {
                    FirstName   = "Rafael",
                    LastName    = "Santos",
                    Email       = "*****@*****.**",
                    UserName    = "******",
                    PhoneNumber = "223232323",
                    Address     = "Rua Da Luz 23 2D",
                    CityId      = _context.Countries.FirstOrDefault().Cities.FirstOrDefault().Id,
                    City        = _context.Countries.FirstOrDefault().Cities.FirstOrDefault()
                };

                var result = await _userHelper.AddUserAsync(user, "123456");



                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder.");
                }


                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);


                var isInRole = await _userHelper.IsUserInRoleAsync(user, "Admin");

                if (!isInRole)
                {
                    await _userHelper.AddUsertoRoleAsync(user, "Admin");
                }
            }



            if (!_context.Products.Any())
            {
                this.AddProduct("Boné oficial SLB", user);
                this.AddProduct("Cueca oficial SLB", user);
                this.AddProduct("Biquini oficial SLB", user);
                await _context.SaveChangesAsync();
            }
        }
Beispiel #3
0
        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();

            await _userHelper.CheckRoleAsync("Admin");

            await _userHelper.CheckRoleAsync("Client");

            if (!_context.Countries.Any())
            {
                var cities = new List <City>();
                cities.Add(new City {
                    Name = "Lisboa"
                });
                cities.Add(new City {
                    Name = "Porto"
                });
                cities.Add(new City {
                    Name = "Coimbra"
                });
                cities.Add(new City {
                    Name = "Faro"
                });


                _context.Countries.Add(new Country
                {
                    Cities = cities,
                    Name   = "Portugal"
                });


                await _context.SaveChangesAsync();
            }

            var user = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new User
                {
                    FirstName = "Andre ",
                    LastName  = "Pires",
                    Email     = "*****@*****.**",
                    UserName  = "******",
                    Address   = "Rua XXXX",
                    CityId    = _context.Countries.FirstOrDefault().Cities.FirstOrDefault().Id,
                    City      = _context.Countries.FirstOrDefault().Cities.FirstOrDefault()
                };

                var result = await _userHelper.AddUserAsync(user, "123456");


                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder");
                }

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);


                var isInRole = await _userHelper.IsUserInRoleAsync(user, "Admin");

                if (!isInRole)
                {
                    await _userHelper.AddUsertoRoleAsync(user, "Admin");
                }
            }

            if (!_context.Vehicles.Any())
            {
                this.AddVehicle("Renault Megane", "DT-18-DH", "Renault", "4 Doors", "The French car", user);
                this.AddVehicle("Opel Astra", "RJ-10-KF", "Opel", "4 Doors", "Not that small", user);
                this.AddVehicle("Fiat 500", "F6-21-GJ", "Fiat", "2 Doors", "Small and portable", user);
                this.AddVehicle("Other", "N/A", "N/A", "N/A", "N/A", user);
                await _context.SaveChangesAsync();
            }
        }
Beispiel #4
0
        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();


            await _userHelper.CheckRoleAsync("SuperAdmin");

            await _userHelper.CheckRoleAsync("Client");

            await _userHelper.CheckRoleAsync("Reseller");

            if (!_context.Categories.Any())
            {
                AddCategories("Romance");
                AddCategories("Poetry");
                AddCategories("Children's");
                AddCategories("Crime & Thriller");
                AddCategories("Fiction");
                AddCategories("Others");
                await _context.SaveChangesAsync();
            }


            if (!_context.Countries.Any())
            {
                CultureInfo[]     cultures      = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
                List <RegionInfo> countriesList = new List <RegionInfo>();
                var countries = new List <Country>();
                foreach (CultureInfo ci in cultures)
                {
                    RegionInfo regionInfo = new RegionInfo(ci.Name);
                    if (countriesList.Count(x => x.EnglishName == regionInfo.EnglishName) <= 0)
                    {
                        countriesList.Add(regionInfo);
                    }
                }

                foreach (RegionInfo regionInfo in countriesList.OrderBy(x => x.EnglishName))
                {
                    var country = regionInfo.EnglishName;
                    AddCountries(country);

                    await _context.SaveChangesAsync();
                }

                AddCountries("null");
            }


            var user = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new User
                {
                    FirstName   = "Cátia",
                    LastName    = "Oliveira",
                    Email       = "*****@*****.**",
                    UserName    = "******",
                    PhoneNumber = "123456",
                    Address     = "Rua da Luz",
                    City        = "Lisboa",
                    CountryId   = 179
                };

                var result = await _userHelper.AddUserAsync(user, "123456");



                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder.");
                }


                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);


                var isInRole = await _userHelper.IsUserInRoleAsync(user, "SuperAdmin");

                if (!isInRole)
                {
                    await _userHelper.AddUsertoRoleAsync(user, "SuperAdmin");
                }
            }
        }