mirror of
https://github.com/grafana/grafana.git
synced 2026-08-18 17:15:08 -05:00
Big refactoring for context.User, and how current user info is fetching, now included collaborator role
This commit is contained in:
@@ -20,6 +20,7 @@ func init() {
|
||||
bus.AddHandler("sql", GetAccountByToken)
|
||||
bus.AddHandler("sql", SearchAccounts)
|
||||
bus.AddHandler("sql", UpdateAccount)
|
||||
bus.AddHandler("sql", GetSignedInUser)
|
||||
}
|
||||
|
||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
@@ -27,6 +28,7 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
|
||||
account := m.Account{
|
||||
Email: cmd.Email,
|
||||
Name: cmd.Name,
|
||||
Login: cmd.Login,
|
||||
Password: cmd.Password,
|
||||
Salt: cmd.Salt,
|
||||
@@ -180,5 +182,38 @@ func SearchAccounts(query *m.SearchAccountsQuery) error {
|
||||
sess.Cols("id", "email", "name", "login", "is_admin")
|
||||
err := sess.Find(&query.Result)
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func GetSignedInUser(query *m.GetSignedInUserQuery) error {
|
||||
var rawSql = `SELECT
|
||||
userAccount.id as account_id,
|
||||
userAccount.is_admin as is_grafana_admin,
|
||||
userAccount.email as user_email,
|
||||
userAccount.name as user_name,
|
||||
userAccount.login as user_login,
|
||||
usingAccount.id as using_account_id,
|
||||
usingAccount.name as using_account_name,
|
||||
collaborator.role as user_role
|
||||
FROM account as userAccount
|
||||
LEFT OUTER JOIN account as usingAccount on usingAccount.id = userAccount.using_account_id
|
||||
LEFT OUTER JOIN collaborator on collaborator.account_id = usingAccount.id AND collaborator.collaborator_id = userAccount.id
|
||||
WHERE userAccount.id=?`
|
||||
|
||||
var user m.SignInUser
|
||||
sess := x.Table("account")
|
||||
has, err := sess.Sql(rawSql, query.AccountId).Get(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return m.ErrAccountNotFound
|
||||
}
|
||||
|
||||
if user.UsingAccountId == 0 || user.UsingAccountId == user.AccountId {
|
||||
user.UsingAccountId = query.AccountId
|
||||
user.UsingAccountName = user.UserName
|
||||
user.UserRole = m.ROLE_OWNER
|
||||
}
|
||||
|
||||
query.Result = &user
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
|
||||
Convey("Given two saved accounts", func() {
|
||||
ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com"}
|
||||
ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com"}
|
||||
ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
||||
ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
||||
|
||||
err := CreateAccount(&ac1cmd)
|
||||
err = CreateAccount(&ac2cmd)
|
||||
@@ -42,7 +42,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
So(query.Result[1].Email, ShouldEqual, "ac2@test.com")
|
||||
})
|
||||
|
||||
Convey("Can add collaborator", func() {
|
||||
Convey("Given an added collaborator", func() {
|
||||
cmd := m.AddCollaboratorCommand{
|
||||
AccountId: ac1.Id,
|
||||
CollaboratorId: ac2.Id,
|
||||
@@ -50,10 +50,25 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
err := AddCollaborator(&cmd)
|
||||
Convey("Saved without error", func() {
|
||||
Convey("Should have been saved without error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Can get logged in user projection", func() {
|
||||
query := m.GetSignedInUserQuery{AccountId: ac2.Id}
|
||||
err := GetSignedInUser(&query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result.AccountId, ShouldEqual, ac2.Id)
|
||||
So(query.Result.UserEmail, ShouldEqual, "ac2@test.com")
|
||||
So(query.Result.UserName, ShouldEqual, "ac2 name")
|
||||
So(query.Result.UserLogin, ShouldEqual, "ac2")
|
||||
So(query.Result.UserRole, ShouldEqual, "Owner")
|
||||
So(query.Result.UsingAccountName, ShouldEqual, "ac2 name")
|
||||
So(query.Result.UsingAccountId, ShouldEqual, ac2.Id)
|
||||
So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
|
||||
})
|
||||
|
||||
Convey("Can get other accounts", func() {
|
||||
query := m.GetOtherAccountsQuery{AccountId: ac2.Id}
|
||||
err := GetOtherAccounts(&query)
|
||||
@@ -66,6 +81,20 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
cmd := m.SetUsingAccountCommand{AccountId: ac2.Id, UsingAccountId: ac1.Id}
|
||||
err := SetUsingAccount(&cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Logged in user query should return correct using account info", func() {
|
||||
query := m.GetSignedInUserQuery{AccountId: ac2.Id}
|
||||
err := GetSignedInUser(&query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result.AccountId, ShouldEqual, ac2.Id)
|
||||
So(query.Result.UserEmail, ShouldEqual, "ac2@test.com")
|
||||
So(query.Result.UserName, ShouldEqual, "ac2 name")
|
||||
So(query.Result.UserLogin, ShouldEqual, "ac2")
|
||||
So(query.Result.UsingAccountName, ShouldEqual, "ac1 name")
|
||||
So(query.Result.UsingAccountId, ShouldEqual, ac1.Id)
|
||||
So(query.Result.UserRole, ShouldEqual, "Viewer")
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user