refactoring of PrivateHandleFunc()

This commit is contained in:
Settel 2021-08-05 15:35:39 +02:00
parent 2b3e2c7dc6
commit 55503395a4
6 changed files with 28 additions and 30 deletions

View File

@ -3,9 +3,18 @@ package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) SyncHandler(w http.ResponseWriter, r *http.Request) {
func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *http.Request) {
// usrId :=
// usr := app.GetUserById(usrId)
// if usr.Game != gameRef && usr.IsAdmin() {
// w.WriteHeader(http.StatusForbidden)
// fmt.Fprintf(w, "forbidden")
// return
// }
gameRef := r.URL.Query().Get("g")
gm, err := app.GetGameById(gameRef)
if err != nil {

View File

@ -5,7 +5,7 @@ import (
)
type Game struct {
id string
Players []string `json:"players"`
eng *engine.Engine
id string
Name string `json:"name"`
eng *engine.Engine
}

View File

@ -3,9 +3,11 @@ package handler
import (
"net/http"
"sirlab.de/go/knyt/application"
"sirlab.de/go/knyt/user"
)
type HandlerFunc func(http.ResponseWriter, *http.Request)
type PrivateHandlerFunc func(*user.User, http.ResponseWriter, *http.Request)
type AuthMux struct {
mux *http.ServeMux

View File

@ -6,19 +6,15 @@ import (
"sirlab.de/go/knyt/user"
)
func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc HandlerFunc) {
func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc PrivateHandlerFunc) {
authMux.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
if authMux.isAuthenticated(r) {
handlerFunc(w, r)
usr, err := authMux.getUserFromSession(r)
if err != nil {
authMux.accessDenied(w, r)
return
}
authMux.accessDenied(w, r)
})
}
func (authMux *AuthMux) PrivateHandle(pattern string, handler http.Handler) {
authMux.PrivateHandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r)
handlerFunc(usr, w, r)
})
}
@ -27,16 +23,6 @@ func (authMux *AuthMux) accessDenied(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Forbidden")
}
func (authMux *AuthMux) isAuthenticated(r *http.Request) bool {
_, err := authMux.getUserFromSession(r)
if err != nil {
return false
}
return true
}
func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error) {
authCookie, err := r.Cookie("knyt-auth")
if err != nil {

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
type userLight struct {
@ -12,13 +13,7 @@ type userLight struct {
Game string `json:"game"`
}
func (authMux *AuthMux) GetUserInfo(w http.ResponseWriter, r *http.Request) {
usr, err := authMux.getUserFromSession(r)
if err != nil {
authMux.accessDenied(w, r)
return
}
func (authMux *AuthMux) GetUserInfo(usr *user.User, w http.ResponseWriter, r *http.Request) {
usrLight := userLight{
Name: usr.Name,
Role: usr.Role,

View File

@ -0,0 +1,6 @@
package statement
type Statement struct {
Id string `json:"id"`
Statement string `json:"statement"`
}