diff --git a/pkg/api/api_login_oauth.go b/pkg/api/api_login_oauth.go index 459b3ec9bb2..5a902a81572 100644 --- a/pkg/api/api_login_oauth.go +++ b/pkg/api/api_login_oauth.go @@ -4,9 +4,10 @@ import ( "errors" "fmt" + "github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/log" "github.com/torkelo/grafana-pro/pkg/middleware" - "github.com/torkelo/grafana-pro/pkg/models" + m "github.com/torkelo/grafana-pro/pkg/models" "github.com/torkelo/grafana-pro/pkg/setting" "github.com/torkelo/grafana-pro/pkg/social" ) @@ -48,21 +49,23 @@ func OAuthLogin(ctx *middleware.Context) { log.Info("login.OAuthLogin(social login): %s", userInfo) - account, err := models.GetAccountByLogin(userInfo.Email) + account, err := m.GetAccountByLogin(userInfo.Email) // create account if missing - if err == models.ErrAccountNotFound { - account = &models.Account{ + if err == m.ErrAccountNotFound { + cmd := &m.CreateAccountCommand{ Login: userInfo.Email, Email: userInfo.Email, Name: userInfo.Name, Company: userInfo.Company, } - if err = models.SaveAccount(account); err != nil { + if err = bus.Dispatch(&cmd); err != nil { ctx.Handle(500, "Failed to create account", err) return } + + account = &cmd.Result } else if err != nil { ctx.Handle(500, "Unexpected error", err) } diff --git a/pkg/api/api_register.go b/pkg/api/api_register.go index 9733f37bab9..265a6038d73 100644 --- a/pkg/api/api_register.go +++ b/pkg/api/api_register.go @@ -1,38 +1,26 @@ package api import ( - "github.com/torkelo/grafana-pro/pkg/log" + "github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/middleware" - "github.com/torkelo/grafana-pro/pkg/models" - "github.com/torkelo/grafana-pro/pkg/utils" + m "github.com/torkelo/grafana-pro/pkg/models" ) -type registerAccountJsonModel struct { - Email string `json:"email" binding:"required"` - Password string `json:"password" binding:"required"` - Password2 bool `json:"remember2"` -} - func CreateAccount(c *middleware.Context) { - var registerModel registerAccountJsonModel + var cmd m.CreateAccountCommand - if !c.JsonBody(®isterModel) { - c.JSON(400, utils.DynMap{"status": "bad request"}) + if !c.JsonBody(&cmd) { + c.JsonApiErr(400, "Validation error", nil) return } - account := models.Account{ - Login: registerModel.Email, - Email: registerModel.Email, - Password: registerModel.Password, - } + cmd.Login = cmd.Email + err := bus.Dispatch(&cmd) - err := models.SaveAccount(&account) if err != nil { - log.Error(2, "Failed to create user account, email: %v, error: %v", registerModel.Email, err) - c.JSON(500, utils.DynMap{"status": "failed to create account"}) + c.JsonApiErr(500, "failed to create account", err) return } - c.JSON(200, utils.DynMap{"status": "ok"}) + c.JsonOK("Account created") } diff --git a/pkg/models/account.go b/pkg/models/account.go index 4e8b9b635b5..ca376f9b8f6 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -6,7 +6,6 @@ import ( ) var ( - SaveAccount func(account *Account) error GetAccountByLogin func(emailOrName string) (*Account, error) GetAccount func(accountId int64) (*Account, error) ) @@ -55,6 +54,16 @@ type AccountDTO struct { Collaborators []*CollaboratorDTO `json:"collaborators"` } +type CreateAccountCommand struct { + Email string `json:"email" binding:"required"` + Login string `json:"login"` + Password string `json:"password" binding:"required"` + Name string `json:"name"` + Company string `json:"company"` + + Result Account `json:"-"` +} + // returns a view projection type GetAccountInfoQuery struct { Id int64 diff --git a/pkg/stores/sqlstore/accounts.go b/pkg/stores/sqlstore/accounts.go index 95d2f0eec61..edb294845cb 100644 --- a/pkg/stores/sqlstore/accounts.go +++ b/pkg/stores/sqlstore/accounts.go @@ -13,6 +13,24 @@ func init() { bus.AddHandler("sql", GetAccountInfo) bus.AddHandler("sql", AddCollaborator) bus.AddHandler("sql", GetOtherAccounts) + bus.AddHandler("sql", CreateAccount) +} + +func CreateAccount(cmd *m.CreateAccountCommand) error { + return inTransaction(func(sess *xorm.Session) error { + + account := m.Account{ + Email: cmd.Email, + Login: cmd.Login, + Password: cmd.Password, + Created: time.Now(), + Updated: time.Now(), + } + + _, err := sess.Insert(&account) + cmd.Result = account + return err + }) } func GetAccountInfo(query *m.GetAccountInfoQuery) error { @@ -55,32 +73,6 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error { }) } -func SaveAccount(account *m.Account) error { - var err error - - sess := x.NewSession() - defer sess.Close() - - if err = sess.Begin(); err != nil { - return err - } - - if account.Id == 0 { - _, err = sess.Insert(account) - } else { - _, err = sess.Id(account.Id).Update(account) - } - - if err != nil { - sess.Rollback() - return err - } else if err = sess.Commit(); err != nil { - return err - } - - return nil -} - func GetAccount(id int64) (*m.Account, error) { var err error diff --git a/pkg/stores/sqlstore/accounts_test.go b/pkg/stores/sqlstore/accounts_test.go index 1457707ae64..cd0b952643d 100644 --- a/pkg/stores/sqlstore/accounts_test.go +++ b/pkg/stores/sqlstore/accounts_test.go @@ -14,27 +14,22 @@ func TestAccountDataAccess(t *testing.T) { InitTestDB(t) Convey("Given two saved accounts", func() { - ac1 := m.Account{ - Login: "ac1", - Email: "ac1@test.com", - Name: "ac1_name", - } - ac2 := m.Account{ - Login: "ac2", - Email: "ac2@test.com", - Name: "ac2_name", - } + ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com"} + ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com"} - err := SaveAccount(&ac1) - err = SaveAccount(&ac2) + err := CreateAccount(&ac1cmd) + err = CreateAccount(&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) So(err, ShouldBeNil) - So(query.Result.Name, ShouldEqual, "ac1_name") + So(query.Result.Email, ShouldEqual, "ac1@test.com") }) Convey("Can add collaborator", func() { diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index f4807b0038d..5c92549f75f 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/pkg/stores/sqlstore/sqlstore.go @@ -35,7 +35,6 @@ func init() { } func Init() { - m.SaveAccount = SaveAccount m.GetAccount = GetAccount m.GetAccountByLogin = GetAccountByLogin m.GetDashboard = GetDashboard