Example #1
0
        public IActionResult ChangeAccountInfo(ChangeAccountInfoViewModel model)
        {
            var customer = _customerRepository.GetCustomerByUsername(_userManager.GetUserName(User));

            if (customer == null)
            {
                ModelState.AddModelError(string.Empty, "We don't recognize your customer Id. Please log in and try again.");
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                customer.CompanyName  = model.CompanyName ?? customer.CompanyName;
                customer.ContactTitle = model.ContactTitle ?? customer.ContactTitle;
                customer.Address      = model.Address ?? customer.Address;
                customer.City         = model.City ?? customer.City;
                customer.Region       = model.Region ?? customer.Region;
                customer.PostalCode   = model.PostalCode ?? customer.PostalCode;
                customer.Country      = model.Country ?? customer.Country;
                _customerRepository.SaveCustomer(customer);

                model.UpdatedSucessfully = true;
            }

            return(View(model));
        }
Example #2
0
        public ActionResult ChangeInfo()
        {
            Users user = db.Userses.Single(u => u.UserName == User.Identity.Name);
            ChangeAccountInfoViewModel model = new ChangeAccountInfoViewModel()
            {
                FullName    = user.FullName,
                PhoneNumber = user.PhoneNumer
            };

            return(View(model));
        }
Example #3
0
 public ActionResult ChangeInfo(ChangeAccountInfoViewModel model)
 {
     if (ModelState.IsValid)
     {
         Users user = db.Userses.Single(u => u.UserName == User.Identity.Name);
         user.FullName   = model.FullName;
         user.PhoneNumer = model.PhoneNumber;
         db.SaveChanges();
         ViewBag.Success = true;
     }
     return(View(model));
 }
        public async Task <ActionResult> ChangeAccountInfo(ChangeAccountInfoViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // Find the user ID
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            if (user != null)
            {
                // check if the viewmodels entries are not null
                // if the entry is null, do nothing
                // otherwise, update the user info
                if (!string.IsNullOrWhiteSpace(model.FirstName) && !model.FirstName.Equals(user.FirstName, StringComparison.InvariantCulture))
                {
                    user.FirstName = model.FirstName;
                }

                if (!string.IsNullOrWhiteSpace(model.LastName) && !model.LastName.Equals(user.LastName, StringComparison.InvariantCulture))
                {
                    user.LastName = model.LastName;
                }

                // update the user information
                var userResult = await UserManager.UpdateAsync(user);

                var changePswd = true;
                if (!userResult.Succeeded)
                {
                    AddErrors(userResult);
                    changePswd = false;
                }

                var phoneResult = await UserManager.SetPhoneNumberAsync(user.Id, model.PhoneNumber);

                if (!phoneResult.Succeeded)
                {
                    AddErrors(phoneResult);
                    changePswd = false;
                }

                // if we have previous errors, don't change the password
                if (changePswd)
                {
                    var passwordResult = await UserManager.ChangePasswordAsync(user.Id, model.OldPassword, model.NewPassword);

                    if (passwordResult.Succeeded)
                    {
                        return(RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    AddErrors(passwordResult);
                }

                await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);
            }


            return(View(model));
        }
        public async Task <HttpResponseMessage> ChangeAccountInformation([FromUri] int id,
                                                                         [FromBody] ChangeAccountInfoViewModel parameters)
        {
            #region Parameters validation

            // Parameters haven't been initialized.
            if (parameters == null)
            {
                parameters = new ChangeAccountInfoViewModel();
                Validate(parameters);
            }

            // Model is not valid.
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                              FindValidationMessage(ModelState, nameof(parameters))));
            }

            #endregion

            #region Requester identity find

            // Search account which sent the current request.
            var account = _identityService.FindAccount(Request.Properties);

            // Account is invalid.
            if (account == null)
            {
                _log.Error("Cannot find authentication information in the current request. Please try again");
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, HttpMessages.RequestIsUnauthenticated));
            }

            #endregion

            #region Search account

            var accounts = UnitOfWork.RepositoryAccounts.Search();
            accounts = accounts.Where(x => x.Id == id);

            // Search the first account.
            var target = await accounts.FirstOrDefaultAsync();

            if (target == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, HttpMessages.AccountNotFound));
            }

            #endregion

            #region Change account information.

            // Whether information has been changed or not.
            var isInformationPristine = true;

            // Nickname is specified.
            if (!string.IsNullOrEmpty(parameters.Nickname))
            {
                target.Nickname       = parameters.Nickname;
                isInformationPristine = false;
            }

            // Status is specified.
            if (parameters.Status != null)
            {
                target.Status         = parameters.Status.Value;
                isInformationPristine = false;
            }

            // No content should be updated.
            if (isInformationPristine)
            {
                _log.Warn($"No information has been specified for account (index: {target.Id})");
                return(Request.CreateErrorResponse(HttpStatusCode.NotModified,
                                                   HttpMessages.AccountInformationNotModified));
            }

            // Update last modified time.
            target.LastModified = _timeService.DateTimeUtcToUnix(DateTime.UtcNow);

            // Save changes into database.
            await UnitOfWork.CommitAsync();

            #endregion

            // Tell the client about account whose information has been modified.
            return(Request.CreateResponse(HttpStatusCode.OK, target));
        }