mirror of https://github.com/grafana/grafana
parent
1705734435
commit
a9a06ad51d
@ -1 +1 @@ |
||||
Subproject commit e5fd35db343109feec09d339d5d770dd1de1808a |
||||
Subproject commit c65b7d159189f81b0d87ecc5b64be3ffbe332393 |
||||
@ -0,0 +1,48 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/gin-gonic/gin" |
||||
"github.com/torkelo/grafana-pro/pkg/models" |
||||
) |
||||
|
||||
type authContext struct { |
||||
account *models.UserAccount |
||||
userAccount *models.UserAccount |
||||
} |
||||
|
||||
func (auth *authContext) getAccountId() int { |
||||
return auth.account.Id |
||||
} |
||||
|
||||
func (self *HttpServer) authDenied(c *gin.Context) { |
||||
c.Writer.Header().Set("Location", "/login") |
||||
c.Abort(302) |
||||
} |
||||
|
||||
func (self *HttpServer) auth() gin.HandlerFunc { |
||||
return func(c *gin.Context) { |
||||
session, _ := sessionStore.Get(c.Request, "grafana-session") |
||||
|
||||
if c.Request.URL.Path != "/login" && session.Values["userAccountId"] == nil { |
||||
self.authDenied(c) |
||||
return |
||||
} |
||||
|
||||
account, err := self.store.GetAccount(session.Values["userAccountId"].(int)) |
||||
if err != nil { |
||||
self.authDenied(c) |
||||
return |
||||
} |
||||
|
||||
usingAccount, err := self.store.GetAccount(session.Values["usingAccountId"].(int)) |
||||
if err != nil { |
||||
self.authDenied(c) |
||||
return |
||||
} |
||||
|
||||
c.Set("userAccount", account) |
||||
c.Set("usingAccount", usingAccount) |
||||
|
||||
session.Save(c.Request, c.Writer) |
||||
} |
||||
} |
||||
@ -0,0 +1,36 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/gin-gonic/gin" |
||||
"github.com/torkelo/grafana-pro/pkg/models" |
||||
) |
||||
|
||||
type routeHandlerRegisterFn func(self *HttpServer) |
||||
type routeHandlerFn func(c *gin.Context, auth *authContext) |
||||
|
||||
var routeHandlers = make([]routeHandlerRegisterFn, 0) |
||||
|
||||
func getRouteHandlerWrapper(handler routeHandlerFn) gin.HandlerFunc { |
||||
return func(c *gin.Context) { |
||||
authContext := authContext{ |
||||
account: c.MustGet("usingAccount").(*models.UserAccount), |
||||
userAccount: c.MustGet("userAccount").(*models.UserAccount), |
||||
} |
||||
handler(c, &authContext) |
||||
} |
||||
} |
||||
|
||||
func (self *HttpServer) addRoute(method string, path string, handler routeHandlerFn) { |
||||
switch method { |
||||
case "GET": |
||||
self.router.GET(path, self.auth(), getRouteHandlerWrapper(handler)) |
||||
case "POST": |
||||
self.router.POST(path, self.auth(), getRouteHandlerWrapper(handler)) |
||||
case "DELETE": |
||||
self.router.DELETE(path, self.auth(), getRouteHandlerWrapper(handler)) |
||||
} |
||||
} |
||||
|
||||
func addRoutes(fn routeHandlerRegisterFn) { |
||||
routeHandlers = append(routeHandlers, fn) |
||||
} |
||||
@ -0,0 +1,43 @@ |
||||
package models |
||||
|
||||
import ( |
||||
"errors" |
||||
"time" |
||||
) |
||||
|
||||
type CollaboratorLink struct { |
||||
AccountId int |
||||
Role string |
||||
ModifiedOn time.Time |
||||
CreatedOn time.Time |
||||
} |
||||
|
||||
type UserAccount struct { |
||||
Id int `gorethink:"id"` |
||||
UserName string |
||||
Login string |
||||
Email string |
||||
Password string |
||||
NextDashboardId int |
||||
UsingAccountId int |
||||
Collaborators []CollaboratorLink |
||||
CreatedOn time.Time |
||||
ModifiedOn time.Time |
||||
} |
||||
|
||||
func (account *UserAccount) AddCollaborator(accountId int) error { |
||||
for _, collaborator := range account.Collaborators { |
||||
if collaborator.AccountId == accountId { |
||||
return errors.New("Collaborator already exists") |
||||
} |
||||
} |
||||
|
||||
account.Collaborators = append(account.Collaborators, CollaboratorLink{ |
||||
AccountId: accountId, |
||||
Role: "admin", |
||||
CreatedOn: time.Now(), |
||||
ModifiedOn: time.Now(), |
||||
}) |
||||
|
||||
return nil |
||||
} |
||||
Loading…
Reference in new issue