securezone.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/auth"
  31. "git.semlanik.org/semlanik/gostfix/common"
  32. )
  33. func (s *Server) handleSecureZone(w http.ResponseWriter, r *http.Request, user string) {
  34. if user == "" {
  35. log.Printf("User could not be empty. Invalid usage of handleMailRequest")
  36. panic(nil)
  37. }
  38. err, ok := s.authenticator.CheckPrivileges(user, auth.AdminPrivilege)
  39. if err != nil {
  40. log.Printf("Unable to fetch priveleges %s for user %s", err, user)
  41. }
  42. if !ok {
  43. s.error(http.StatusUnauthorized, "Administrator permissions required", w)
  44. return
  45. }
  46. s.error(http.StatusNotImplemented, "Admin panel is not implemented", w)
  47. }
  48. func (s *Server) handleSettings(w http.ResponseWriter, r *http.Request, user string) {
  49. if user == "" {
  50. log.Printf("User could not be empty. Invalid usage of handleMailRequest")
  51. panic(nil)
  52. }
  53. switch r.Method {
  54. case "GET":
  55. info, err := s.storage.GetUserInfo(user)
  56. if err != nil {
  57. s.error(http.StatusInternalServerError, "Unable to obtain user information", w)
  58. return
  59. }
  60. fmt.Fprintf(w, s.templater.ExecuteSettings(&struct {
  61. Version string
  62. FullName string
  63. }{common.Version, info.FullName}))
  64. case "PATCH":
  65. s.handleSettingsUpdate(w, r, user)
  66. }
  67. }
  68. func (s *Server) handleSettingsUpdate(w http.ResponseWriter, r *http.Request, user string) {
  69. if err := r.ParseForm(); err != nil {
  70. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  71. return
  72. }
  73. oldPassword := r.FormValue("oldPassword")
  74. if err := s.authenticator.CheckUser(user, oldPassword); err != nil {
  75. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  76. return
  77. }
  78. password := r.FormValue("password")
  79. fullName := r.FormValue("fullName")
  80. err := s.storage.UpdateUser(user, password, fullName)
  81. if err != nil {
  82. log.Println(err.Error())
  83. s.error(http.StatusInternalServerError, "Unable to update user data", w)
  84. return
  85. }
  86. w.Write([]byte{0})
  87. }