Exemple #1
0
        //executes the creation of a user and saving it to database.
        public IActionResult CreateUpdateResult(CreateOrUpdateViewModel vm)
        {
            //flag to show in view if saving succeded.
            ViewBag.Success = true;

            //flag to show if new user was created, or a pre existed user was just updated.
            ViewBag.NewUser = true;

            //this set up an if() that will stop _login view validation to show up
            //when a guest try to register using this action.
            TempData["Validation"] = "true";

            //get current user.
            var currentUser = _um.GetUser();

            //if user exist, remove username from validation checks.
            if (currentUser != null)
            {
                ModelState.Remove("UserName");
            }

            //returns to CreateOrUpdate view if form was not completed.
            if (!ModelState.IsValid)
            {
                //check each property, on faild validation create Tempdata with the propery name
                //as the key.
                foreach (var item in ModelState.Keys)
                {
                    if (ModelState.GetValidationState(item) == ModelValidationState.Invalid)
                    {
                        //this will be used to check which properties failed validation
                        //ad a span with red asterisk will be created in the view besides the
                        //relevent input element.
                        TempData[item] = "Show*";
                    }
                }
                return(View("CreateOrUpdate", vm));
            }

            //Create or update User in db.
            if (currentUser != null)
            {
                //Updating an existing user.
                ViewBag.NewUser = false;

                var userToUpdate = _db.UserRepository.Get(currentUser.Id);

                //encrypts the new password.
                vm.Password = EncryptionHelper.ComputeHash(vm.Password, "SHA512", null);
                _um.UpdateUser(userToUpdate, vm);

                //tries to save data to database.
                if (!_db.Save())
                {
                    //if failed in saving, show that in the view.
                    ViewBag.Success = false;
                }
            }
            else
            {
                //creats a User class from the viewmodel sent in the form.
                var user = _um.CreateNewUserFromViewModel(vm);

                //encrypts the password.
                user.Password = EncryptionHelper.ComputeHash(user.Password, "SHA512", null);

                //tires to save user in the database.
                if (!_db.UserRepository.Create(user))
                {
                    //if failed in saving, show that in the view.
                    ViewBag.Success = false;
                }
                else
                {
                    //Creatin a new user.
                    ViewBag.NewUser = true;

                    //after saving, create a new log entry in the database.
                    _um.CreateCurrentUserCookie(user);
                    LogEntry entry = new LogEntry()
                    {
                        Content   = $"user: {user.UserName} created",
                        TimeStamp = DateTime.Now
                    };
                    _db.LogEntryRepository.Create(entry);
                }
            }

            return(View(vm));
        }