Пример #1
0
        public IActionResult Delete(int userId)
        {
            // Check if the user is logged into the system and userLevel == 9 if not log them off.
            if (CheckLoggedUserLevel(9) != true)
            {
                return(RedirectToAction("Logoff", "Login"));
            }

            //Delete selected User
            User RetrievedUser = _context.Users.SingleOrDefault(user => user.UserId == userId);

            _context.Users.Remove(RetrievedUser);
            _context.SaveChanges();

            //Redirect Admin to List of all Users.
            return(RedirectToAction("ListUsers", "Login"));
        }
        public IActionResult RegisterUser(RegistrationViewModel newUser)
        {
            HttpContext.Session.Clear();

            if (ModelState.IsValid)
            {
                //Check email is not in use.
                User emailChecker = _context.Users.SingleOrDefault(u => u.Email == newUser.Email);

                if (emailChecker != null)
                {
                    //Error Here
                    ModelState.AddModelError("Email", "Email already in use");
                    return(View("Index"));
                }

                User addUser = new User {
                    Email     = newUser.Email,
                    FirstName = newUser.FirstName,
                    LastName  = newUser.LastName,
                    Password  = newUser.Password
                };

                //Hash that Password
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                addUser.Password = Hasher.HashPassword(addUser, newUser.Password);

                // checked to give SuperAdmin
                List <User> AllUsers = _context.Users.ToList();
                if (AllUsers.Count() == 0)
                {
                    addUser.UserLevel = 9;
                }
                else
                {
                    addUser.UserLevel = 1;
                }

                // add User to Database
                _context.Add(addUser);
                _context.SaveChanges();

                //Save the new User to Session and move them to Userlist
                List <User> thisUser = _context.Users.Where(u => u.Email == addUser.Email).ToList();
                HttpContext.Session.SetInt32("UserId", (int)thisUser[0].UserId);

                //Direct new User to ListUsers

                return(RedirectToAction("Shop", "Shop"));
            }

            return(RedirectToAction("Index", "Login"));
        }