securezone.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package web
  26. import (
  27. "fmt"
  28. "log"
  29. "net/http"
  30. "git.semlanik.org/semlanik/gostfix/common"
  31. )
  32. func (s *Server) handleSecureZone(w http.ResponseWriter, r *http.Request, user string) {
  33. if user == "" {
  34. log.Printf("User could not be empty. Invalid usage of handleMailRequest")
  35. panic(nil)
  36. }
  37. s.error(http.StatusNotImplemented, "Admin panel is not implemented", w)
  38. }
  39. func (s *Server) handleSettings(w http.ResponseWriter, r *http.Request, user string) {
  40. if user == "" {
  41. log.Printf("User could not be empty. Invalid usage of handleMailRequest")
  42. panic(nil)
  43. }
  44. switch r.Method {
  45. case "GET":
  46. info, err := s.storage.GetUserInfo(user)
  47. if err != nil {
  48. s.error(http.StatusInternalServerError, "Unable to obtain user information", w)
  49. return
  50. }
  51. fmt.Fprintf(w, s.templater.ExecuteSettings(&struct {
  52. Version string
  53. FullName string
  54. }{common.Version, info.FullName}))
  55. case "PATCH":
  56. s.handleSettingsUpdate(w, r, user)
  57. }
  58. }
  59. func (s *Server) handleSettingsUpdate(w http.ResponseWriter, r *http.Request, user string) {
  60. if err := r.ParseForm(); err != nil {
  61. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  62. return
  63. }
  64. oldPassword := r.FormValue("oldPassword")
  65. if err := s.authenticator.CheckUser(user, oldPassword); err != nil {
  66. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  67. return
  68. }
  69. password := r.FormValue("password")
  70. fullName := r.FormValue("fullName")
  71. err := s.storage.UpdateUser(user, password, fullName)
  72. if err != nil {
  73. log.Println(err.Error())
  74. s.error(http.StatusInternalServerError, "Unable to update user data", w)
  75. return
  76. }
  77. w.Write([]byte{0})
  78. }