User / Account model split, User and account now seperate entities, collaborators are now AccountUsers

This commit is contained in:
Torkel Ödegaard
2015-01-19 18:01:04 +01:00
parent f1996a9f1f
commit 90925273a0
29 changed files with 592 additions and 705 deletions

View File

@@ -0,0 +1,51 @@
package sqlstore
import (
"time"
"github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func init() {
bus.AddHandler("sql", AddAccountUser)
bus.AddHandler("sql", RemoveAccountUser)
bus.AddHandler("sql", GetAccountUsers)
}
func AddAccountUser(cmd *m.AddAccountUserCommand) error {
return inTransaction(func(sess *xorm.Session) error {
entity := m.AccountUser{
AccountId: cmd.AccountId,
UserId: cmd.UserId,
Role: cmd.Role,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(&entity)
return err
})
}
func GetAccountUsers(query *m.GetAccountUsersQuery) error {
query.Result = make([]*m.AccountUserDTO, 0)
sess := x.Table("account_user")
sess.Join("INNER", "user", "account_user.user_id=user.id")
sess.Where("account_user.account_id=?", query.AccountId)
sess.Cols("account_user.account_id", "account_user.user_id", "user.email", "user.login")
err := sess.Find(&query.Result)
return err
}
func RemoveAccountUser(cmd *m.RemoveAccountUserCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM account_user WHERE account_id=? and user_id=?"
_, err := sess.Exec(rawSql, cmd.AccountId, cmd.UserId)
return err
})
}

View File

@@ -1,7 +1,6 @@
package sqlstore
import (
"strings"
"time"
"github.com/go-xorm/xorm"
@@ -11,33 +10,20 @@ import (
)
func init() {
bus.AddHandler("sql", GetAccountInfo)
bus.AddHandler("sql", GetOtherAccounts)
bus.AddHandler("sql", CreateAccount)
bus.AddHandler("sql", SetUsingAccount)
bus.AddHandler("sql", GetAccountById)
bus.AddHandler("sql", GetAccountByLogin)
bus.AddHandler("sql", SearchAccounts)
bus.AddHandler("sql", UpdateAccount)
bus.AddHandler("sql", GetSignedInUser)
}
func CreateAccount(cmd *m.CreateAccountCommand) error {
return inTransaction(func(sess *xorm.Session) error {
account := m.Account{
Email: cmd.Email,
Name: cmd.Name,
Login: cmd.Login,
Password: cmd.Password,
Salt: cmd.Salt,
IsAdmin: cmd.IsAdmin,
Created: time.Now(),
Updated: time.Now(),
Name: cmd.Name,
Created: time.Now(),
Updated: time.Now(),
}
sess.UseBool("is_admin")
_, err := sess.Insert(&account)
cmd.Result = account
return err
@@ -48,8 +34,6 @@ func UpdateAccount(cmd *m.UpdateAccountCommand) error {
return inTransaction(func(sess *xorm.Session) error {
account := m.Account{
Email: cmd.Email,
Login: cmd.Login,
Name: cmd.Name,
Updated: time.Now(),
}
@@ -58,137 +42,3 @@ func UpdateAccount(cmd *m.UpdateAccountCommand) error {
return err
})
}
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
return inTransaction(func(sess *xorm.Session) error {
account := m.Account{}
sess.Id(cmd.AccountId).Get(&account)
account.UsingAccountId = cmd.UsingAccountId
_, err := sess.Id(account.Id).Update(&account)
return err
})
}
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
var account m.Account
has, err := x.Id(query.Id).Get(&account)
if err != nil {
return err
} else if has == false {
return m.ErrAccountNotFound
}
query.Result = m.AccountDTO{
Name: account.Name,
Email: account.Email,
Login: account.Login,
}
return err
}
func GetAccountById(query *m.GetAccountByIdQuery) error {
var err error
var account m.Account
has, err := x.Id(query.Id).Get(&account)
if err != nil {
return err
} else if has == false {
return m.ErrAccountNotFound
}
if account.UsingAccountId == 0 {
account.UsingAccountId = account.Id
}
query.Result = &account
return nil
}
func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
if query.LoginOrEmail == "" {
return m.ErrAccountNotFound
}
account := new(m.Account)
if strings.Contains(query.LoginOrEmail, "@") {
account = &m.Account{Email: query.LoginOrEmail}
} else {
account = &m.Account{Login: strings.ToLower(query.LoginOrEmail)}
}
has, err := x.Get(account)
if err != nil {
return err
} else if has == false {
return m.ErrAccountNotFound
}
if account.UsingAccountId == 0 {
account.UsingAccountId = account.Id
}
query.Result = account
return nil
}
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
query.Result = make([]*m.OtherAccountDTO, 0)
sess := x.Table("collaborator")
sess.Join("INNER", "account", "collaborator.account_id=account.id")
sess.Where("collaborator_id=?", query.AccountId)
sess.Cols("collaborator.account_id", "collaborator.role", "account.email")
err := sess.Find(&query.Result)
return err
}
func SearchAccounts(query *m.SearchAccountsQuery) error {
query.Result = make([]*m.AccountSearchHitDTO, 0)
sess := x.Table("account")
sess.Where("email LIKE ?", query.Query+"%")
sess.Limit(query.Limit, query.Limit*query.Page)
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.SignedInUser
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
}

View File

@@ -13,87 +13,85 @@ func TestAccountDataAccess(t *testing.T) {
Convey("Testing Account DB Access", t, func() {
InitTestDB(t)
Convey("Given two saved accounts", func() {
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}
Convey("Given two saved users", func() {
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
err := CreateAccount(&ac1cmd)
err = CreateAccount(&ac2cmd)
err := CreateUser(&ac1cmd)
err = CreateUser(&ac2cmd)
So(err, ShouldBeNil)
ac1 := ac1cmd.Result
ac2 := ac2cmd.Result
Convey("Should be able to read account info projection", func() {
query := m.GetAccountInfoQuery{Id: ac1.Id}
err = GetAccountInfo(&query)
Convey("Should be able to read user info projection", func() {
query := m.GetUserInfoQuery{UserId: ac1.Id}
err = GetUserInfo(&query)
So(err, ShouldBeNil)
So(query.Result.Email, ShouldEqual, "ac1@test.com")
So(query.Result.Login, ShouldEqual, "ac1")
})
Convey("Can search accounts", func() {
query := m.SearchAccountsQuery{Query: ""}
err := SearchAccounts(&query)
Convey("Can search users", func() {
query := m.SearchUsersQuery{Query: ""}
err := SearchUsers(&query)
So(err, ShouldBeNil)
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
So(query.Result[1].Email, ShouldEqual, "ac2@test.com")
})
Convey("Given an added collaborator", func() {
cmd := m.AddCollaboratorCommand{
AccountId: ac1.Id,
CollaboratorId: ac2.Id,
Role: m.ROLE_VIEWER,
Convey("Given an added account user", func() {
cmd := m.AddAccountUserCommand{
AccountId: ac1.AccountId,
UserId: ac2.Id,
Role: m.ROLE_VIEWER,
}
err := AddCollaborator(&cmd)
err := AddAccountUser(&cmd)
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}
query := m.GetSignedInUserQuery{UserId: 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.AccountId, ShouldEqual, ac2.AccountId)
So(query.Result.Email, ShouldEqual, "ac2@test.com")
So(query.Result.Name, ShouldEqual, "ac2 name")
So(query.Result.Login, ShouldEqual, "ac2")
So(query.Result.AccountRole, ShouldEqual, "Admin")
So(query.Result.AccountName, ShouldEqual, "ac2@test.com")
So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
})
Convey("Can get other accounts", func() {
query := m.GetOtherAccountsQuery{AccountId: ac2.Id}
err := GetOtherAccounts(&query)
Convey("Can get user accounts", func() {
query := m.GetUserAccountsQuery{UserId: ac2.Id}
err := GetUserAccounts(&query)
So(err, ShouldBeNil)
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
So(len(query.Result), ShouldEqual, 2)
})
Convey("Can set using account", func() {
cmd := m.SetUsingAccountCommand{AccountId: ac2.Id, UsingAccountId: ac1.Id}
cmd := m.SetUsingAccountCommand{UserId: ac2.Id, AccountId: 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}
query := m.GetSignedInUserQuery{UserId: 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")
So(query.Result.AccountId, ShouldEqual, ac1.Id)
So(query.Result.Email, ShouldEqual, "ac2@test.com")
So(query.Result.Name, ShouldEqual, "ac2 name")
So(query.Result.Login, ShouldEqual, "ac2")
So(query.Result.AccountName, ShouldEqual, "ac1@test.com")
So(query.Result.AccountRole, ShouldEqual, "Viewer")
})
})
})

View File

@@ -1,51 +0,0 @@
package sqlstore
import (
"time"
"github.com/go-xorm/xorm"
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func init() {
bus.AddHandler("sql", AddCollaborator)
bus.AddHandler("sql", RemoveCollaborator)
bus.AddHandler("sql", GetCollaborators)
}
func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
return inTransaction(func(sess *xorm.Session) error {
entity := m.Collaborator{
AccountId: cmd.AccountId,
CollaboratorId: cmd.CollaboratorId,
Role: cmd.Role,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(&entity)
return err
})
}
func GetCollaborators(query *m.GetCollaboratorsQuery) error {
query.Result = make([]*m.CollaboratorDTO, 0)
sess := x.Table("collaborator")
sess.Join("INNER", "account", "collaborator.collaborator_id=account.id")
sess.Where("collaborator.account_id=?", query.AccountId)
sess.Cols("collaborator.collaborator_id", "collaborator.role", "account.email", "account.login")
err := sess.Find(&query.Result)
return err
}
func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
_, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId)
return err
})
}

View File

@@ -30,10 +30,15 @@ func AddMigrations(mg *Migrator) {
&Column{Name: "created", Type: DB_DateTime, Nullable: false},
&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
))
//------- user table indexes ------------------
mg.AddMigration("add unique index UIX_user.login", new(AddIndexMigration).
Name("UIX_user_login").Table("user").Columns("login"))
mg.AddMigration("add unique index UIX_user.email", new(AddIndexMigration).
Name("UIX_user_email").Table("user").Columns("email"))
//------- account2 table -------------------
mg.AddMigration("create account2 table", new(AddTableMigration).
Name("account2").WithColumns(
//------- account table -------------------
mg.AddMigration("create account table", new(AddTableMigration).
Name("account").WithColumns(
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
&Column{Name: "name", Type: DB_NVarchar, Length: 255},
&Column{Name: "created", Type: DB_DateTime, Nullable: false},
@@ -41,7 +46,7 @@ func AddMigrations(mg *Migrator) {
))
mg.AddMigration("add unique index UIX_account.name", new(AddIndexMigration).
Name("UIX_account_name").Table("account2").Columns("name"))
Name("UIX_account_name").Table("account").Columns("name"))
//------- account_user table -------------------
mg.AddMigration("create account_user table", new(AddTableMigration).
@@ -57,27 +62,6 @@ func AddMigrations(mg *Migrator) {
mg.AddMigration("add unique index UIX_account_user", new(AddIndexMigration).
Name("UIX_account_user").Table("account_user").Columns("account_id", "user_id"))
//------- account table -------------------
mg.AddMigration("create account table", new(AddTableMigration).
Name("account").WithColumns(
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
&Column{Name: "login", Type: DB_NVarchar, Length: 255},
&Column{Name: "email", Type: DB_NVarchar, Length: 255},
&Column{Name: "name", Type: DB_NVarchar, Length: 255},
&Column{Name: "password", Type: DB_NVarchar, Length: 50},
&Column{Name: "salt", Type: DB_NVarchar, Length: 50},
&Column{Name: "company", Type: DB_NVarchar, Length: 255},
&Column{Name: "using_account_id", Type: DB_BigInt},
&Column{Name: "is_admin", Type: DB_Bool},
&Column{Name: "created", Type: DB_DateTime},
&Column{Name: "updated", Type: DB_DateTime},
))
//------- account table indexes ------------------
mg.AddMigration("add unique index UIX_account.login", new(AddIndexMigration).
Name("UIX_account_login").Table("account").Columns("login"))
mg.AddMigration("add unique index UIX_account.email", new(AddIndexMigration).
Name("UIX_account_email").Table("account").Columns("email"))
}
type MigrationLog struct {

View File

@@ -32,24 +32,18 @@ var (
UseSQLite3 bool
)
type TestTable struct {
Id1 int `xorm:"pk"`
Id2 int `xorm:"pk"`
}
func init() {
tables = make([]interface{}, 0)
tables = append(tables, new(m.Dashboard),
new(m.Collaborator), new(m.DataSource), new(DashboardTag),
new(m.Token), new(TestTable))
tables = append(tables, new(m.Dashboard), new(m.DataSource), new(DashboardTag),
new(m.Token))
}
func EnsureAdminUser() {
adminQuery := m.GetAccountByLoginQuery{LoginOrEmail: setting.AdminUser}
adminQuery := m.GetUserByLoginQuery{LoginOrEmail: setting.AdminUser}
if err := bus.Dispatch(&adminQuery); err == m.ErrAccountNotFound {
cmd := m.CreateAccountCommand{}
if err := bus.Dispatch(&adminQuery); err == m.ErrUserNotFound {
cmd := m.CreateUserCommand{}
cmd.Login = setting.AdminUser
cmd.Email = setting.AdminUser + "@localhost"
cmd.Salt = util.GetRandomString(10)

View File

@@ -1,6 +1,7 @@
package sqlstore
import (
"strings"
"time"
"github.com/go-xorm/xorm"
@@ -11,13 +12,19 @@ import (
func init() {
bus.AddHandler("sql", CreateUser)
bus.AddHandler("sql", GetUserByLogin)
bus.AddHandler("sql", SetUsingAccount)
bus.AddHandler("sql", GetUserInfo)
bus.AddHandler("sql", GetSignedInUser)
bus.AddHandler("sql", SearchUsers)
bus.AddHandler("sql", GetUserAccounts)
}
func CreateUser(cmd *m.CreateUserCommand) error {
return inTransaction(func(sess *xorm.Session) error {
// create account
account := m.Account2{
account := m.Account{
Name: cmd.Email,
Created: time.Now(),
Updated: time.Now(),
@@ -31,6 +38,9 @@ func CreateUser(cmd *m.CreateUserCommand) error {
user := m.User{
Email: cmd.Email,
Password: cmd.Password,
Name: cmd.Name,
Login: cmd.Login,
Company: cmd.Company,
Salt: cmd.Salt,
IsAdmin: cmd.IsAdmin,
AccountId: account.Id,
@@ -56,3 +66,106 @@ func CreateUser(cmd *m.CreateUserCommand) error {
return err
})
}
func GetUserByLogin(query *m.GetUserByLoginQuery) error {
if query.LoginOrEmail == "" {
return m.ErrAccountNotFound
}
user := new(m.User)
if strings.Contains(query.LoginOrEmail, "@") {
user = &m.User{Email: query.LoginOrEmail}
} else {
user = &m.User{Login: strings.ToLower(query.LoginOrEmail)}
}
has, err := x.Get(user)
if err != nil {
return err
} else if has == false {
return m.ErrUserNotFound
}
query.Result = user
return nil
}
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
return inTransaction(func(sess *xorm.Session) error {
user := m.User{}
sess.Id(cmd.UserId).Get(&user)
user.AccountId = cmd.AccountId
_, err := sess.Id(user.Id).Update(&user)
return err
})
}
func GetUserInfo(query *m.GetUserInfoQuery) error {
var user m.User
has, err := x.Id(query.UserId).Get(&user)
if err != nil {
return err
} else if has == false {
return m.ErrUserNotFound
}
query.Result = m.UserDTO{
Name: user.Name,
Email: user.Email,
Login: user.Login,
}
return err
}
func GetUserAccounts(query *m.GetUserAccountsQuery) error {
query.Result = make([]*m.UserAccountDTO, 0)
sess := x.Table("account_user")
sess.Join("INNER", "account", "account_user.account_id=account.id")
sess.Where("account_user.user_id=?", query.UserId)
sess.Cols("account.name", "account_user.role", "account_user.account_id")
err := sess.Find(&query.Result)
return err
}
func GetSignedInUser(query *m.GetSignedInUserQuery) error {
var rawSql = `SELECT
user.id as user_id,
user.is_admin as is_grafana_admin,
user.email as email,
user.login as login,
user.name as name,
account.name as account_name,
account_user.role as account_role,
account.id as account_id
FROM user
LEFT OUTER JOIN account_user on account_user.account_id = user.account_id and account_user.user_id = user.id
LEFT OUTER JOIN account on account.id = user.account_id
WHERE user.id=?`
var user m.SignedInUser
sess := x.Table("user")
has, err := sess.Sql(rawSql, query.UserId).Get(&user)
if err != nil {
return err
} else if !has {
return m.ErrUserNotFound
}
query.Result = &user
return err
}
func SearchUsers(query *m.SearchUsersQuery) error {
query.Result = make([]*m.UserSearchHitDTO, 0)
sess := x.Table("user")
sess.Where("email LIKE ?", query.Query+"%")
sess.Limit(query.Limit, query.Limit*query.Page)
sess.Cols("id", "email", "name", "login", "is_admin")
err := sess.Find(&query.Result)
return err
}

View File

@@ -1,30 +0,0 @@
package sqlstore
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
m "github.com/torkelo/grafana-pro/pkg/models"
)
func TestUserDataAccess(t *testing.T) {
Convey("Testing User DB", t, func() {
InitTestDB(t)
Convey("When creating a user", func() {
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com"}
err := CreateUser(&ac1cmd)
So(err, ShouldBeNil)
ac1 := ac1cmd.Result
fmt.Printf("%v", ac1)
Convey("Should be able to read account info projection", func() {
})
})
})
}