mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
get other accounts works
This commit is contained in:
@@ -29,9 +29,9 @@ type CollaboratorLink struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type OtherAccount struct {
|
type OtherAccount struct {
|
||||||
Id int `gorethink:"id"`
|
Id int64
|
||||||
Name string
|
Email string
|
||||||
Role string
|
Role string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
|
|||||||
@@ -27,11 +27,11 @@ type CollaboratorInfo struct {
|
|||||||
Email string
|
Email string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCollaborator(accountId int64, forAccountId int64) *Collaborator {
|
func NewCollaborator(accountId int64, forAccountId int64, role RoleType) *Collaborator {
|
||||||
return &Collaborator{
|
return &Collaborator{
|
||||||
AccountId: accountId,
|
AccountId: accountId,
|
||||||
ForAccountId: forAccountId,
|
ForAccountId: forAccountId,
|
||||||
Role: ROLE_READ,
|
Role: role,
|
||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
Updated: time.Now(),
|
Updated: time.Now(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,8 +49,7 @@ func AddCollaborator(c *middleware.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var collaborator = models.NewCollaborator(accountToAdd.Id, c.UserAccount.Id)
|
var collaborator = models.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, models.ROLE_READ_WRITE)
|
||||||
collaborator.Role = models.ROLE_READ_WRITE
|
|
||||||
|
|
||||||
err = models.AddCollaborator(collaborator)
|
err = models.AddCollaborator(collaborator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -60,3 +59,31 @@ func AddCollaborator(c *middleware.Context) {
|
|||||||
|
|
||||||
c.Status(204)
|
c.Status(204)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOtherAccounts(c *middleware.Context) {
|
||||||
|
|
||||||
|
otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, utils.DynMap{"message": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []*dtos.OtherAccount
|
||||||
|
result = append(result, &dtos.OtherAccount{
|
||||||
|
Id: c.UserAccount.Id,
|
||||||
|
Role: "owner",
|
||||||
|
IsUsing: c.UserAccount.Id == c.UserAccount.UsingAccountId,
|
||||||
|
Name: c.UserAccount.Email,
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, other := range otherAccounts {
|
||||||
|
result = append(result, &dtos.OtherAccount{
|
||||||
|
Id: other.Id,
|
||||||
|
Role: other.Role,
|
||||||
|
Name: other.Email,
|
||||||
|
IsUsing: other.Id == c.UserAccount.UsingAccountId,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, result)
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,6 +25,13 @@ type AccountInfo struct {
|
|||||||
Collaborators []*Collaborator `json:"collaborators"`
|
Collaborators []*Collaborator `json:"collaborators"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OtherAccount struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Role string `json:"role"`
|
||||||
|
IsUsing bool `json:"isUsing"`
|
||||||
|
}
|
||||||
|
|
||||||
type Collaborator struct {
|
type Collaborator struct {
|
||||||
AccountId int64 `json:"accountId"`
|
AccountId int64 `json:"accountId"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ func Register(m *macaron.Macaron) {
|
|||||||
m.Get("/account/", auth, Index)
|
m.Get("/account/", auth, Index)
|
||||||
m.Get("/api/account/", auth, api.GetAccount)
|
m.Get("/api/account/", auth, api.GetAccount)
|
||||||
m.Post("/api/account/collaborators/add", auth, api.AddCollaborator)
|
m.Post("/api/account/collaborators/add", auth, api.AddCollaborator)
|
||||||
|
m.Get("/api/account/others", auth, api.GetOtherAccounts)
|
||||||
|
|
||||||
// user register
|
// user register
|
||||||
m.Get("/register/*_", Index)
|
m.Get("/register/*_", Index)
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ func Init() {
|
|||||||
models.CreateAccount = CreateAccount
|
models.CreateAccount = CreateAccount
|
||||||
models.GetAccount = GetAccount
|
models.GetAccount = GetAccount
|
||||||
models.GetAccountByLogin = GetAccountByLogin
|
models.GetAccountByLogin = GetAccountByLogin
|
||||||
|
models.GetOtherAccountsFor = GetOtherAccountsFor
|
||||||
models.GetDashboard = GetDashboard
|
models.GetDashboard = GetDashboard
|
||||||
models.SaveDashboard = SaveDashboard
|
models.SaveDashboard = SaveDashboard
|
||||||
models.SearchQuery = SearchQuery
|
models.SearchQuery = SearchQuery
|
||||||
|
|||||||
@@ -60,9 +60,12 @@ func GetAccountByLogin(emailOrLogin string) (*models.Account, error) {
|
|||||||
|
|
||||||
func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, error) {
|
func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, error) {
|
||||||
collaborators := make([]*models.CollaboratorInfo, 0)
|
collaborators := make([]*models.CollaboratorInfo, 0)
|
||||||
|
|
||||||
sess := x.Table("Collaborator")
|
sess := x.Table("Collaborator")
|
||||||
sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id")
|
sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id")
|
||||||
|
sess.Where("Collaborator.for_account_id=?", accountId)
|
||||||
err := sess.Find(&collaborators)
|
err := sess.Find(&collaborators)
|
||||||
|
|
||||||
return collaborators, err
|
return collaborators, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,3 +88,12 @@ func AddCollaborator(collaborator *models.Collaborator) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOtherAccountsFor(accountId int64) ([]*models.OtherAccount, error) {
|
||||||
|
collaborators := make([]*models.OtherAccount, 0)
|
||||||
|
sess := x.Table("Collaborator")
|
||||||
|
sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id")
|
||||||
|
sess.Where("Collaborator.account_id=?", accountId)
|
||||||
|
err := sess.Find(&collaborators)
|
||||||
|
return collaborators, err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user