api_auth.go 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/torkelo/grafana-pro/pkg/models"
  5. )
  6. type authContext struct {
  7. account *models.Account
  8. userAccount *models.Account
  9. }
  10. func (auth *authContext) getAccountId() int {
  11. return auth.account.Id
  12. }
  13. func (self *HttpServer) authDenied(c *gin.Context) {
  14. c.Writer.Header().Set("Location", "/login")
  15. c.Abort(302)
  16. }
  17. func (self *HttpServer) auth() gin.HandlerFunc {
  18. return func(c *gin.Context) {
  19. session, _ := sessionStore.Get(c.Request, "grafana-session")
  20. if c.Request.URL.Path != "/login" && session.Values["userAccountId"] == nil {
  21. self.authDenied(c)
  22. return
  23. }
  24. account, err := self.store.GetAccount(session.Values["userAccountId"].(int))
  25. if err != nil {
  26. self.authDenied(c)
  27. return
  28. }
  29. usingAccount, err := self.store.GetAccount(session.Values["usingAccountId"].(int))
  30. if err != nil {
  31. self.authDenied(c)
  32. return
  33. }
  34. c.Set("userAccount", account)
  35. c.Set("usingAccount", usingAccount)
  36. session.Save(c.Request, c.Writer)
  37. }
  38. }