From 3427f712ecd8aea10fc7137bd93faf5698ca767b Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Sun, 18 Mar 2018 06:17:58 +0100 Subject: [PATCH] Improved handling of user roles --- main.go | 10 ---------- templates/_navigation.html | 6 ++++-- util.go | 11 +++++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 8a9b7cc..1cf5c77 100644 --- a/main.go +++ b/main.go @@ -8,16 +8,6 @@ import ( "fmt" ) -func headerWrapper(handler http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - println(r.URL.Path) - for key, value := range r.Header { - fmt.Printf("%s: %s\n", key, value) - } - handler.ServeHTTP(w, r) - }) -} - func main() { config := NewConfigFromEnv() diff --git a/templates/_navigation.html b/templates/_navigation.html index d0aba7d..d5d31c9 100644 --- a/templates/_navigation.html +++ b/templates/_navigation.html @@ -2,14 +2,16 @@ <nav> <ul> <li class="title"><a href="/">i.k8r</a></li> - {{if .Id}} + {{if .hasRole "imghost:user" }} <li><a href="/upload">Upload</a></li> {{end}} <li class="spacer"></li> - {{if .Id}} + {{if .hasRole "imghost:user" }} <li><a href="/me/images">My Images</a></li> <li><a href="/me/albums">My Albums</a></li> <li class="me"><a href="https://accounts.kuschku.de/profile">{{.Name}}</a></li> + {{else if .Id }} + <li class="me"><a href="https://accounts.kuschku.de/profile">{{.Name}}</a></li> {{else}} <li><a href="/me/images">Login</a></li> {{end}} diff --git a/util.go b/util.go index 54521ce..f8b9c37 100644 --- a/util.go +++ b/util.go @@ -13,6 +13,16 @@ type UserInfo struct { Id string Name string Email string + Roles []string +} + +func (info *UserInfo) hasRole(role string) bool { + for _, r := range info.Roles { + if r == role { + return true + } + } + return false } type PageContext struct { @@ -43,6 +53,7 @@ func parseUser(r *http.Request) UserInfo { r.Header.Get("X-Auth-Subject"), r.Header.Get("X-Auth-Username"), r.Header.Get("X-Auth-Email"), + r.Header.Get("X-Auth-Roles"), } } -- GitLab