refactor: move Cameo() to separate file
This commit is contained in:
parent
ca4dddec2e
commit
ca609fad94
41
server/src/handler/cameo.go
Normal file
41
server/src/handler/cameo.go
Normal 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)
|
||||
}
|
@ -69,36 +69,3 @@ func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
|
||||
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user