mirror of
https://github.com/grafana/grafana.git
synced 2025-09-27 01:23:48 +08:00
Small progress on adding collaborator
This commit is contained in:
2
grafana
2
grafana
Submodule grafana updated: 4f798cfe56...aa47eeffb2
@ -53,6 +53,8 @@ func (self *HttpServer) ListenAndServe() {
|
|||||||
// register default route
|
// register default route
|
||||||
self.router.GET("/", self.auth(), self.index)
|
self.router.GET("/", self.auth(), self.index)
|
||||||
self.router.GET("/dashboard/*_", self.auth(), self.index)
|
self.router.GET("/dashboard/*_", self.auth(), self.index)
|
||||||
|
self.router.GET("/admin/*_", self.auth(), self.index)
|
||||||
|
self.router.GET("/account/*_", self.auth(), self.index)
|
||||||
|
|
||||||
self.router.Run(":" + self.port)
|
self.router.Run(":" + self.port)
|
||||||
}
|
}
|
||||||
|
39
pkg/api/api_account.go
Normal file
39
pkg/api/api_account.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
addRoutes(func(self *HttpServer) {
|
||||||
|
self.router.POST("/api/account/collaborators/add", self.auth(), self.addCollaborator)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type addCollaboratorDto struct {
|
||||||
|
Email string `json:"email" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *HttpServer) addCollaborator(c *gin.Context) {
|
||||||
|
var model addCollaboratorDto
|
||||||
|
|
||||||
|
if !c.EnsureBody(&model) {
|
||||||
|
c.JSON(400, gin.H{"status": "bad request"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accountId, _ := c.Get("accountId")
|
||||||
|
account, err := self.store.GetAccount(accountId.(int))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(401, gin.H{"status": "Authentication error"})
|
||||||
|
}
|
||||||
|
|
||||||
|
collaborator, err := self.store.GetUserAccountLogin(model.Email)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(404, gin.H{"status": "Collaborator not found"})
|
||||||
|
}
|
||||||
|
|
||||||
|
account.AddCollaborator(collaborator.Id)
|
||||||
|
|
||||||
|
self.store.SaveUserAccount(account)
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{"status": "Collaborator added"})
|
||||||
|
}
|
@ -36,7 +36,7 @@ func (self *HttpServer) loginPost(c *gin.Context) {
|
|||||||
|
|
||||||
session, _ := sessionStore.Get(c.Request, "grafana-session")
|
session, _ := sessionStore.Get(c.Request, "grafana-session")
|
||||||
session.Values["login"] = loginModel.Email
|
session.Values["login"] = loginModel.Email
|
||||||
session.Values["accountId"] = account.DatabaseId
|
session.Values["accountId"] = account.Id
|
||||||
session.Save(c.Request, c.Writer)
|
session.Save(c.Request, c.Writer)
|
||||||
|
|
||||||
var resp = &LoginResultDto{}
|
var resp = &LoginResultDto{}
|
||||||
@ -54,17 +54,6 @@ func (self *HttpServer) logoutPost(c *gin.Context) {
|
|||||||
c.JSON(200, gin.H{"status": "logged out"})
|
c.JSON(200, gin.H{"status": "logged out"})
|
||||||
}
|
}
|
||||||
|
|
||||||
type GrafanaReqContext struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
type authenticatedAuthRouteFunc func(c *gin.Context, grc GrafanaReqContext)
|
|
||||||
|
|
||||||
func (self *HttpServer) addAuthRoute(route string, handler authenticatedAuthRouteFunc) {
|
|
||||||
self.router.GET(route, self.auth(), func(c *gin.Context) {
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *HttpServer) auth() gin.HandlerFunc {
|
func (self *HttpServer) auth() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
session, _ := sessionStore.Get(c.Request, "grafana-session")
|
session, _ := sessionStore.Get(c.Request, "grafana-session")
|
||||||
|
@ -21,22 +21,22 @@ type Dashboard struct {
|
|||||||
Data map[string]interface{}
|
Data map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserAccountLink struct {
|
type CollaboratorLink struct {
|
||||||
UserId int
|
AccountId int
|
||||||
Role string
|
Role string
|
||||||
ModifiedOn time.Time
|
ModifiedOn time.Time
|
||||||
CreatedOn time.Time
|
CreatedOn time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserAccount struct {
|
type UserAccount struct {
|
||||||
DatabaseId int `gorethink:"id"`
|
Id int `gorethink:"id"`
|
||||||
UserName string
|
UserName string
|
||||||
Login string
|
Login string
|
||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
NextDashboardId int
|
NextDashboardId int
|
||||||
UsingAccountId int
|
UsingAccountId int
|
||||||
GrantedAccess []UserAccountLink
|
Collaborators []CollaboratorLink
|
||||||
CreatedOn time.Time
|
CreatedOn time.Time
|
||||||
ModifiedOn time.Time
|
ModifiedOn time.Time
|
||||||
}
|
}
|
||||||
@ -87,3 +87,12 @@ func (dash *Dashboard) UpdateSlug() {
|
|||||||
re2 := regexp.MustCompile("\\s")
|
re2 := regexp.MustCompile("\\s")
|
||||||
dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
|
dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (account *UserAccount) AddCollaborator(accountId int) {
|
||||||
|
account.Collaborators = append(account.Collaborators, CollaboratorLink{
|
||||||
|
AccountId: accountId,
|
||||||
|
Role: "admin",
|
||||||
|
CreatedOn: time.Now(),
|
||||||
|
ModifiedOn: time.Now(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -31,7 +31,7 @@ func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
account.DatabaseId = accountId
|
account.Id = accountId
|
||||||
|
|
||||||
resp, err := r.Table("accounts").Insert(account).RunWrite(self.session)
|
resp, err := r.Table("accounts").Insert(account).RunWrite(self.session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -61,6 +61,22 @@ func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserA
|
|||||||
return &account, nil
|
return &account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *rethinkStore) GetAccount(id int) (*models.UserAccount, error) {
|
||||||
|
resp, err := r.Table("accounts").Get(id).Run(self.session)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var account models.UserAccount
|
||||||
|
err = resp.One(&account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("Not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &account, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
|
func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
|
||||||
resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{
|
resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{
|
||||||
"NextDashboardId": r.Row.Field("NextDashboardId").Add(1),
|
"NextDashboardId": r.Row.Field("NextDashboardId").Add(1),
|
||||||
|
@ -38,17 +38,17 @@ func TestRethinkStore(t *testing.T) {
|
|||||||
account := &models.UserAccount{UserName: "torkelo", Email: "mupp", Login: "test@test.com"}
|
account := &models.UserAccount{UserName: "torkelo", Email: "mupp", Login: "test@test.com"}
|
||||||
err := store.SaveUserAccount(account)
|
err := store.SaveUserAccount(account)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(account.DatabaseId, ShouldNotEqual, 0)
|
So(account.Id, ShouldNotEqual, 0)
|
||||||
|
|
||||||
read, err := store.GetUserAccountLogin("test@test.com")
|
read, err := store.GetUserAccountLogin("test@test.com")
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(read.DatabaseId, ShouldEqual, account.DatabaseId)
|
So(read.Id, ShouldEqual, account.DatabaseId)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("can get next dashboard id", t, func() {
|
Convey("can get next dashboard id", t, func() {
|
||||||
account := &models.UserAccount{UserName: "torkelo", Email: "mupp"}
|
account := &models.UserAccount{UserName: "torkelo", Email: "mupp"}
|
||||||
err := store.SaveUserAccount(account)
|
err := store.SaveUserAccount(account)
|
||||||
dashId, err := store.getNextDashboardNumber(account.DatabaseId)
|
dashId, err := store.getNextDashboardNumber(account.Id)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(dashId, ShouldEqual, 1)
|
So(dashId, ShouldEqual, 1)
|
||||||
})
|
})
|
||||||
|
@ -11,6 +11,7 @@ type Store interface {
|
|||||||
Query(query string, acccountId int) ([]*models.SearchResult, error)
|
Query(query string, acccountId int) ([]*models.SearchResult, error)
|
||||||
SaveUserAccount(acccount *models.UserAccount) error
|
SaveUserAccount(acccount *models.UserAccount) error
|
||||||
GetUserAccountLogin(emailOrName string) (*models.UserAccount, error)
|
GetUserAccountLogin(emailOrName string) (*models.UserAccount, error)
|
||||||
|
GetAccount(id int) (*models.UserAccount, error)
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user