Ejemplo n.º 1
0
        public IHttpActionResult ChangePassword(Models.PasswordChangeViewModel dtoItem)
        {
            Library.DTO.Notification notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };

            // validation
            if (!Helper.CommonHelper.ValidateDTO <Models.PasswordChangeViewModel>(dtoItem, out notification))
            {
                return(Ok(new Library.DTO.ReturnData <Models.PasswordChangeViewModel>()
                {
                    Data = dtoItem, Message = notification
                }));
            }

            // change password
            try
            {
                using (AuthRepository _repo = new AuthRepository())
                {
                    if (dtoItem.NewPassword.Length < 7)
                    {
                        throw new Exception("Password length must be at least 7 chars");
                    }
                    if (dtoItem.NewPassword != dtoItem.Confirmation)
                    {
                        throw new Exception("Password and confirmation do not match!");
                    }

                    _repo.ResetPassword(ControllerContext.GetAuthUserName(), dtoItem.NewPassword);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(Ok(new Library.DTO.ReturnData <DTO.UserMng.UserProfile>()
                {
                    Data = null, Message = notification
                }));
            }

            return(Ok(new Library.DTO.ReturnData <bool>()
            {
                Data = true, Message = notification
            }));
        }