1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package web
- import (
- "fmt"
- "log"
- "net/http"
- "git.semlanik.org/semlanik/gostfix/common"
- )
- func (s *Server) handleSecureZone(w http.ResponseWriter, r *http.Request) {
- user, token := s.extractAuth(w, r)
- if !s.authenticator.Verify(user, token) {
- s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
- return
- }
- switch r.URL.Path {
- case "/settings":
- s.handleSettings(w, user)
- case "/update":
- s.handleUpdate(w, r, user)
-
-
-
-
-
-
-
- }
- }
- func (s *Server) handleSettings(w http.ResponseWriter, user string) {
- info, err := s.storage.GetUserInfo(user)
- if err != nil {
- s.error(http.StatusInternalServerError, "Unable to obtain user information", w)
- return
- }
- fmt.Fprintf(w, s.templater.ExecuteSettings(&struct {
- Version string
- FullName string
- }{common.Version, info.FullName}))
- }
- func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request, user string) {
- if err := r.ParseForm(); err != nil {
- s.error(http.StatusUnauthorized, "Password entered is invalid", w)
- return
- }
- oldPassword := r.FormValue("oldPassword")
- if err := s.authenticator.CheckUser(user, oldPassword); err != nil {
- s.error(http.StatusUnauthorized, "Password entered is invalid", w)
- return
- }
- password := r.FormValue("password")
- fullName := r.FormValue("fullName")
- err := s.storage.UpdateUser(user, password, fullName)
- if err != nil {
- log.Println(err.Error())
- s.error(http.StatusInternalServerError, "Unable to update user data", w)
- return
- }
- w.Write([]byte{0})
- }
|