Esempio n. 1
0
        public IActionResult Register(User newUser)
        {
            if (ModelState.IsValid)
            {
                // If any user already exists with email.
                if (db.Users.Any(u => u.Email == newUser.Email))
                {
                    ModelState.AddModelError("Email", "is taken.");
                }
            }

            // If we added an error above.
            if (ModelState.IsValid == false)
            {
                // Show form again to display errors.
                return(View("Index"));
            }

            PasswordHasher <User> hasher = new PasswordHasher <User>();

            newUser.Password = hasher.HashPassword(newUser, newUser.Password);

            db.Users.Add(newUser);
            db.SaveChanges();

            HttpContext.Session.SetInt32("UserId", newUser.UserId);
            HttpContext.Session.SetString("FullName", newUser.FullName());

            return(RedirectToAction("All", "Trips"));
        }
Esempio n. 2
0
        public async Task Seed()
        {
            if (!_context.Roles.Any())
            {
                _context.Roles.Add(new UserRole {
                    Name = "admin", NormalizedName = "admin"
                });
                _context.Roles.Add(new UserRole {
                    Name = "manager", NormalizedName = "manager"
                });
            }

            var admin = new TravelUser
            {
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                EmailConfirmed  = true,
                LockoutEnabled  = true,
                CreationDate    = DateTime.Now,
                SecurityStamp   = Guid.NewGuid().ToString()
            };
            var manager = new TravelUser
            {
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                EmailConfirmed  = true,
                LockoutEnabled  = true,
                CreationDate    = DateTime.Now,
                SecurityStamp   = Guid.NewGuid().ToString()
            };
            var normal = new TravelUser
            {
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                EmailConfirmed  = true,
                LockoutEnabled  = true,
                CreationDate    = DateTime.Now,
                SecurityStamp   = Guid.NewGuid().ToString()
            };

            if (!_context.Users.Any())
            {
                var password = new PasswordHasher <TravelUser>();
                var hashed   = password.HashPassword(admin, "password");
                admin.PasswordHash   = hashed;
                manager.PasswordHash = hashed;
                normal.PasswordHash  = hashed;
                var userStore = new TravelUserStore(_usersReadService, _usersWriteService, _rolesReadService);
                await userStore.CreateAsync(admin, new CancellationToken());

                await userStore.CreateAsync(manager, new CancellationToken());

                await userStore.CreateAsync(normal, new CancellationToken());

                await userStore.AddToRoleAsync(admin, "admin", new CancellationToken());

                await userStore.AddToRoleAsync(manager, "manager", new CancellationToken());
            }
            _context.SaveChanges();
        }
Esempio n. 3
0
        public IActionResult Create(Trip newTrip)
        {
            if (!isLoggedIn)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (!ModelState.IsValid)
            {
                // Display form errors.
                return(View("New"));
            }

            newTrip.UserId = (int)uid;
            db.Trips.Add(newTrip);
            db.SaveChanges();
            return(RedirectToAction("All"));
        }
        public IActionResult Create(Trip newTrip)
        {
            if (!ModelState.IsValid)
            {
                // To display validation errors.
                return(View("New"));
            }


            // WILL GET THIS ERROR if FK is not assigned:
            // "foreign key constraint fails"
            newTrip.UserId = (int)uid;
            db.Trips.Add(newTrip);
            db.SaveChanges(); // after this newTrip has it's TripId from DB.

            /*
             * WHENEVER REDIRECTING to a method that has params, you must pass in
             * a 'new' dictionary: new { paramName = valueForParam }
             */
            return(RedirectToAction("Details", new { tripId = newTrip.TripId }));
        }
Esempio n. 5
0
        public IActionResult Create(DestinationMedia newLocationMedia)
        {
            if (!isLoggedIn)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (!ModelState.IsValid)
            {
                // Display form errors.
                return(View("New"));
            }

            newLocationMedia.UserId = (int)uid;
            db.DestinationMedias.Add(newLocationMedia);
            db.SaveChanges();
            return(RedirectToAction("All"));
        }
        public IActionResult Create(DestinationMedia newDestinationMedia)
        {
            if (!ModelState.IsValid)
            {
                // To display validation errors.
                return(View("New"));
            }


            // WILL GET THIS ERROR if FK is not assigned:
            // "foreign key constraint fails"
            newDestinationMedia.UserId = (int)uid;
            db.DestinationMedia.Add(newDestinationMedia);
            db.SaveChanges();

            /*
             * WHENEVER REDIRECTING to a method that has params, you must pass in
             * a 'new' dictionary: new { paramName = valueForParam }
             */

            return(RedirectToAction("Details", new { destinationMediaId = newDestinationMedia.DestinationMediaId }));
        }
Esempio n. 7
0
        public IActionResult Register(User newUser)
        {
            if (ModelState.IsValid)
            {
                bool existingUser = db.Users.Any(u => u.Email == newUser.Email);

                if (existingUser)
                {
                    // Normally you don't want to reveal info like this b/c hackers can use it.
                    // But for testing purposes, we will make our errors descriptive.
                    ModelState.AddModelError("Email", "is taken.");
                }
            }

            /*
             * We could potentially have multiple conditions that invalidate the
             * model state above so we have a catch-all check so we can display
             * all error messages at once.
             */
            if (ModelState.IsValid == false)
            {
                // So error messages will be displayed.
                return(View("Index"));
            }

            // hash the password
            PasswordHasher <User> hasher = new PasswordHasher <User>();

            newUser.Password = hasher.HashPassword(newUser, newUser.Password);

            db.Users.Add(newUser);
            db.SaveChanges();

            HttpContext.Session.SetInt32("UserId", newUser.UserId);
            HttpContext.Session.SetString("FullName", newUser.FullName());
            return(RedirectToAction("All", "Trips"));
        }
Esempio n. 8
0
 public Trip CreateTrip(Trip trip)
 {
     trip = _context.Trips.Add(trip).Entity;
     _context.SaveChanges();
     return(trip);
 }
 public TravelUser CreateUser(TravelUser user)
 {
     user = _context.Users.Add(user).Entity;
     _context.SaveChanges();
     return(user);
 }
Esempio n. 10
0
 public UserRole CreateRole(UserRole role)
 {
     role = _context.Roles.Add(role).Entity;
     _context.SaveChanges();
     return(role);
 }