refactor: move Cameo() to separate file

This commit is contained in:
Settel 2024-02-18 10:26:31 +01:00
parent ca4dddec2e
commit ca609fad94
2 changed files with 41 additions and 33 deletions

View File

@ -0,0 +1,41 @@
package handler
import (
"fmt"
"net/http"
"sirlab.de/go/knowyt/user"
)
func (authMux *AuthMux) Cameo(usr *user.User, w http.ResponseWriter, r *http.Request) {
if usr.IsAdmin() {
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
usrCameo, err := authMux.checkCode(r)
if err != nil {
http.SetCookie(w, cookie)
authMux.accessDenied(w, r)
return
}
cookie.Value = usrCameo.GetId()
cookie.MaxAge = 0
http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
return
}
// non-admin: remove cameo cookie
usrCameo := usr.GetCameo()
if usrCameo != nil && usrCameo.IsAdmin() {
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
return
}
authMux.accessDenied(w, r)
}

View File

@ -69,36 +69,3 @@ func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
return usr, nil return usr, nil
} }
func (authMux *AuthMux) Cameo(usr *user.User, w http.ResponseWriter, r *http.Request) {
if usr.IsAdmin() {
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
usrCameo, err := authMux.checkCode(r)
if err != nil {
http.SetCookie(w, cookie)
authMux.accessDenied(w, r)
return
}
cookie.Value = usrCameo.GetId()
cookie.MaxAge = 0
http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
return
}
// non-admin: remove cameo cookie
usrCameo := usr.GetCameo()
if usrCameo != nil && usrCameo.IsAdmin() {
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
return
}
authMux.accessDenied(w, r)
}