예제 #1
0
        public ActionResult Edit([Bind(Include = "Id,Name,Username,Password,Email,RepeatPassword,Biography,IsPrivate")] User user, HttpPostedFileBase profilePictureFile)
        {
            if (!UserValidations.ValidateEmail(user.Email))
            {
                ViewBag.Error     = "Your email is invalid. Please enter a valid one.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidateUsername(user.Username))
            {
                ViewBag.Error     = "Your username is invalid. It should be between 3 and 50 characters long.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidatePassword(user.Password))
            {
                ViewBag.Error     = "Your password is invalid. It should be at least 8 characters long, contain at least one small and one big letter and one digit.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidateRepeatedPassword(user.Password, user.RepeatPassword))
            {
                ViewBag.Error     = "Your passwords do not match.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (!UserValidations.ValidateProfilePicture(profilePictureFile))
            {
                ViewBag.Error     = "You have not chosen a profile picture.";
                ViewBag.ShowError = true;
                return(View());
                //TODO: Add notification
            }

            if (ModelState.IsValid)
            {
                user.RegisterProfilePicture = PictureUtilities.PictureToByteArray(profilePictureFile);
                repo.Update(user);
                return(RedirectToAction("Details/" + AuthManager.GetAuthenticated().Id, "Users"));
            }
            return(View(user));
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "Id,Name,Username,Password,Email,RepeatPassword,Biography,IsPrivate")] User user, HttpPostedFileBase profilePictureFile)
        {
            if (!UserValidations.ValidateEmail(user.Email))
            {
                ViewBag.Error     = "Your email is invalid. Please enter a valid one.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (UserUtilities.IsEmailTaken(user.Email, db))
            {
                ViewBag.Error     = "This email is already taken. Please register with another one.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidateUsername(user.Username))
            {
                ViewBag.Error     = "Your username is invalid. It should be between 3 and 50 characters long.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (UserUtilities.IsUserExisting(user.Username, db))
            {
                ViewBag.Error     = "This username is already taken. Please register with another one.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidatePassword(user.Password))
            {
                ViewBag.Error     = "Your password is invalid. It should be at least 8 characters long, contain at least one small and one big letter and one digit.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidateRepeatedPassword(user.Password, user.RepeatPassword))
            {
                ViewBag.Error     = "Your passwords do not match.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (!UserValidations.ValidateProfilePicture(profilePictureFile))
            {
                ViewBag.Error     = "You have not chosen a profile picture.";
                ViewBag.ShowError = true;
                return(View());
            }

            if (ModelState.IsValid)
            {
                user.RegisterProfilePicture = PictureUtilities.PictureToByteArray(profilePictureFile);
                db.Users.Add(user);
                db.SaveChanges();
                AuthManager.SetCurrentUser(user.Username, user.Password);
                return(RedirectToAction("Details/" + AuthManager.GetAuthenticated().Id, "Users"));
            }
            return(View(user));
        }