Ejemplo n.º 1
0
        public ActionResult UpdateUser(AHP.Web.ViewModel.UserinfoViewModel userInfo)
        {
            GenericAjaxResponse <AHP.Core.DTO.ExternalUserInfo> response = new GenericAjaxResponse <Core.DTO.ExternalUserInfo>();

            try
            {
                if (ModelState.IsValid)
                {
                    if (!userInfo.Role.Equals("admin", StringComparison.OrdinalIgnoreCase) && !userInfo.Role.Equals("user", StringComparison.OrdinalIgnoreCase))
                    {
                        response.Errors.Add("Role can be only admin or user");
                        response.Success = false;
                        return(Json(response));
                    }

                    AHP.Core.DTO.ExternalUserInfo externalUserAccount = new Core.DTO.ExternalUserInfo();
                    //Only subset of properties are to be filled. Since others are maintained internally
                    externalUserAccount.Username              = userInfo.Username;
                    externalUserAccount.Email                 = userInfo.Email;
                    externalUserAccount.Firstname             = userInfo.Firstname;
                    externalUserAccount.Lastname              = userInfo.Lastname;
                    externalUserAccount.Role                  = userInfo.Role;
                    externalUserAccount.SupplierId            = string.Join(",", userInfo.SupplierId.Split(',').Distinct());
                    externalUserAccount.ChangePasswordOnLogon = userInfo.ChangePasswordOnLogon;
                    externalUserAccount.BirthMonth            = short.Parse(userInfo.BirthMonth);
                    externalUserAccount.BirthYear             = short.Parse(userInfo.BirthYear);
                    externalUserAccount.ZipCode               = userInfo.ZipCode;
                    externalUserAccount.Company               = userInfo.Company;
                    response = _restClient.UpdateUser(externalUserAccount);
                    if (response == null)
                    {
                        response         = new GenericAjaxResponse <Core.DTO.ExternalUserInfo>();
                        response.Success = false;
                        response.Errors.Add("An error occurred. Please try again.");
                    }
                }
                else
                {
                    foreach (var modelKey in ModelState.Keys)
                    {
                        response.Errors.AddRange(ModelState[modelKey].Errors.Select(err => err.ErrorMessage));
                    }
                    response.Success = response.Errors.Count == 0;
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error occurred updating the user", ex);
            }
            return(Json(response));
        }