diff --git a/handlers/dashboard.go b/handlers/dashboard.go index e5b4abc3..57029505 100644 --- a/handlers/dashboard.go +++ b/handlers/dashboard.go @@ -17,6 +17,7 @@ package handlers import ( "bytes" + "fmt" "github.com/hunterlong/statping/core" "github.com/hunterlong/statping/core/notifier" "github.com/hunterlong/statping/source" @@ -27,7 +28,7 @@ import ( ) func dashboardHandler(w http.ResponseWriter, r *http.Request) { - if !IsFullAuthenticated(r) { + if !IsUser(r) { err := core.ErrorResponse{} ExecuteResponse(w, r, "login.gohtml", err, nil) } else { @@ -47,7 +48,9 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { if auth { session.Values["authenticated"] = true session.Values["user_id"] = user.Id + session.Values["admin"] = user.Admin.Bool session.Save(r, w) + utils.Log(1, fmt.Sprintf("User %v logged in from IP %v", user.Username, r.RemoteAddr)) http.Redirect(w, r, "/dashboard", http.StatusSeeOther) } else { err := core.ErrorResponse{Error: "Incorrect login information submitted, try again."} @@ -58,12 +61,14 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { func logoutHandler(w http.ResponseWriter, r *http.Request) { session, _ := sessionStore.Get(r, cookieKey) session.Values["authenticated"] = false + session.Values["admin"] = false + session.Values["user_id"] = 0 session.Save(r, w) http.Redirect(w, r, "/", http.StatusSeeOther) } func helpHandler(w http.ResponseWriter, r *http.Request) { - if !IsFullAuthenticated(r) { + if !IsUser(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } diff --git a/handlers/handlers.go b/handlers/handlers.go index 8bd1ded2..43365ee4 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -136,6 +136,23 @@ func IsFullAuthenticated(r *http.Request) bool { return true } } + return IsAdmin(r) +} + +// IsAdmin returns true if the user session is an administrator +func IsAdmin(r *http.Request) bool { + session, err := sessionStore.Get(r, cookieKey) + if err != nil { + return false + } + if session.Values["admin"] == nil { + return false + } + return session.Values["admin"].(bool) +} + +// IsUser returns true if the user is registered +func IsUser(r *http.Request) bool { session, err := sessionStore.Get(r, cookieKey) if err != nil { return false @@ -160,6 +177,9 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap "Auth": func() bool { return IsFullAuthenticated(r) }, + "IsUser": func() bool { + return IsUser(r) + }, "VERSION": func() string { return core.VERSION }, diff --git a/handlers/messages.go b/handlers/messages.go index 35de6376..a72bd089 100644 --- a/handlers/messages.go +++ b/handlers/messages.go @@ -26,7 +26,7 @@ import ( ) func messagesHandler(w http.ResponseWriter, r *http.Request) { - if !IsFullAuthenticated(r) { + if !IsUser(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } diff --git a/handlers/services.go b/handlers/services.go index 262ee225..bb98bb27 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -47,7 +47,7 @@ func renderServiceChartsHandler(w http.ResponseWriter, r *http.Request) { } func servicesHandler(w http.ResponseWriter, r *http.Request) { - if !IsFullAuthenticated(r) { + if !IsUser(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } diff --git a/handlers/users.go b/handlers/users.go index c2a875c3..8ab04953 100644 --- a/handlers/users.go +++ b/handlers/users.go @@ -28,7 +28,7 @@ import ( ) func usersHandler(w http.ResponseWriter, r *http.Request) { - if !IsFullAuthenticated(r) { + if !IsUser(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } diff --git a/notifiers/line_notify.go b/notifiers/line_notify.go index d17188cf..e1c1c4e5 100644 --- a/notifiers/line_notify.go +++ b/notifiers/line_notify.go @@ -61,7 +61,7 @@ func (u *lineNotifier) Send(msg interface{}) error { message := msg.(string) v := url.Values{} v.Set("message", message) - headers := []string{fmt.Sprintf("Authorization=Bearer %v", u.GetValue("api_secret"))} + headers := []string{fmt.Sprintf("Authorization=Bearer %v", u.ApiSecret)} _, _, err := utils.HttpRequest("https://notify-api.line.me/api/notify", "POST", "application/x-www-form-urlencoded", headers, strings.NewReader(v.Encode()), time.Duration(10*time.Second)) return err } diff --git a/source/tmpl/help.gohtml b/source/tmpl/help.gohtml index fdbc6bdc..441e4813 100644 --- a/source/tmpl/help.gohtml +++ b/source/tmpl/help.gohtml @@ -1,7 +1,7 @@ {{define "title"}}Statping | Help{{end}} {{define "content"}}