diff --git a/grafana b/grafana index 2423f470ef6..4b5eadf7b59 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit 2423f470ef62145c26e74511121eac01e554c363 +Subproject commit 4b5eadf7b59898e6622a75e0a57081103dd78b2a diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 6b207eaf9ac..a0d58d7ec71 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -17,7 +17,6 @@ import ( "github.com/torkelo/grafana-pro/pkg/routes" "github.com/torkelo/grafana-pro/pkg/setting" "github.com/torkelo/grafana-pro/pkg/social" - "github.com/torkelo/grafana-pro/pkg/stores/rethink" "github.com/torkelo/grafana-pro/pkg/stores/sqlstore" ) @@ -66,7 +65,7 @@ func mapStatic(m *macaron.Macaron, dir string, prefix string) { func runWeb(*cli.Context) { setting.NewConfigContext() setting.InitServices() - rethink.Init() + sqlstore.Init() social.NewOAuthService() // init database diff --git a/pkg/middleware/auth.go b/pkg/middleware/auth.go index 3693e22b853..8c337714847 100644 --- a/pkg/middleware/auth.go +++ b/pkg/middleware/auth.go @@ -9,7 +9,7 @@ import ( "github.com/torkelo/grafana-pro/pkg/models" ) -func authGetRequestAccountId(c *Context, sess session.Store) (int, error) { +func authGetRequestAccountId(c *Context, sess session.Store) (int64, error) { accountId := sess.Get("accountId") urlQuery := c.Req.URL.Query() @@ -23,7 +23,7 @@ func authGetRequestAccountId(c *Context, sess session.Store) (int, error) { return -1, errors.New("Auth: session account id not found") } - return accountId.(int), nil + return accountId.(int64), nil } func authDenied(c *Context) { diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 619f5200bd8..c519a4a1b68 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -21,7 +21,7 @@ type Context struct { IsSigned bool } -func (c *Context) GetAccountId() int { +func (c *Context) GetAccountId() int64 { return c.Account.Id } diff --git a/pkg/models/account.go b/pkg/models/account.go index c468e057f08..dde55b3459d 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -9,8 +9,8 @@ var ( CreateAccount func(acccount *Account) error UpdateAccount func(acccount *Account) error GetAccountByLogin func(emailOrName string) (*Account, error) - GetAccount func(accountId int) (*Account, error) - GetOtherAccountsFor func(accountId int) ([]*OtherAccount, error) + GetAccount func(accountId int64) (*Account, error) + GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error) ) // Typed errors @@ -19,7 +19,7 @@ var ( ) type CollaboratorLink struct { - AccountId int + AccountId int64 Role string Email string ModifiedOn time.Time @@ -33,20 +33,20 @@ type OtherAccount struct { } type Account struct { - Id int `gorethink:"id"` - Version int - Login string - Email string - AccountName string + Id int64 + Login string `xorm:"UNIQUE NOT NULL"` + Email string `xorm:"UNIQUE NOT NULL"` + Name string `xorm:"UNIQUE NOT NULL"` + FullName string Password string - Name string + IsAdmin bool + Salt string `xorm:"VARCHAR(10)"` Company string NextDashboardId int - UsingAccountId int - Collaborators []CollaboratorLink - CreatedOn time.Time - ModifiedOn time.Time - LastLoginOn time.Time + UsingAccountId int64 + Collaborators []CollaboratorLink `xorm:"-"` + Created time.Time `xorm:"CREATED"` + Updated time.Time `xorm:"UPDATED"` } func (account *Account) AddCollaborator(newCollaborator *Account) error { @@ -67,7 +67,7 @@ func (account *Account) AddCollaborator(newCollaborator *Account) error { return nil } -func (account *Account) RemoveCollaborator(accountId int) { +func (account *Account) RemoveCollaborator(accountId int64) { list := account.Collaborators for i, collaborator := range list { if collaborator.AccountId == accountId { @@ -77,7 +77,7 @@ func (account *Account) RemoveCollaborator(accountId int) { } } -func (account *Account) HasCollaborator(accountId int) bool { +func (account *Account) HasCollaborator(accountId int64) bool { for _, collaborator := range account.Collaborators { if collaborator.AccountId == accountId { return true diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 802e0e156ec..54d033afaa3 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -2,6 +2,7 @@ package models import ( "encoding/json" + "errors" "io" "regexp" "strings" @@ -9,19 +10,24 @@ import ( ) var ( - GetDashboard func(slug string, accountId int) (*Dashboard, error) + GetDashboard func(slug string, accountId int64) (*Dashboard, error) SaveDashboard func(dash *Dashboard) error - DeleteDashboard func(slug string, accountId int) error - SearchQuery func(query string, acccountId int) ([]*SearchResult, error) + DeleteDashboard func(slug string, accountId int64) error + SearchQuery func(query string, acccountId int64) ([]*SearchResult, error) +) + +// Typed errors +var ( + ErrDashboardNotFound = errors.New("Account not found") ) type Dashboard struct { - Id string `gorethink:"id,omitempty"` - Slug string - AccountId int - LastModifiedByUserId string - LastModifiedByDate time.Time - CreatedDate time.Time + Id int64 + Slug string `xorm:"index(IX_AccountIdSlug)"` + AccountId int64 `xorm:"index(IX_AccountIdSlug)"` + + Created time.Time `xorm:"CREATED"` + Updated time.Time `xorm:"UPDATED"` Title string Tags []string @@ -36,10 +42,7 @@ type SearchResult struct { func NewDashboard(title string) *Dashboard { dash := &Dashboard{} - dash.Id = "" - dash.LastModifiedByDate = time.Now() - dash.CreatedDate = time.Now() - dash.LastModifiedByUserId = "123" + dash.Id = 0 dash.Data = make(map[string]interface{}) dash.Data["title"] = title dash.Title = title diff --git a/pkg/routes/api/api_dashboard.go b/pkg/routes/api/api_dashboard.go index 2fdd47bd9c9..c71c2530875 100644 --- a/pkg/routes/api/api_dashboard.go +++ b/pkg/routes/api/api_dashboard.go @@ -69,7 +69,7 @@ func PostDashboard(c *middleware.Context) { dashboard.UpdateSlug() if dashboard.Data["id"] != nil { - dashboard.Id = dashboard.Data["id"].(string) + dashboard.Id = int64(dashboard.Data["id"].(float64)) } err := models.SaveDashboard(dashboard) diff --git a/pkg/routes/api/api_render.go b/pkg/routes/api/api_render.go index 16657b207e7..833e1067268 100644 --- a/pkg/routes/api/api_render.go +++ b/pkg/routes/api/api_render.go @@ -12,7 +12,7 @@ import ( func RenderToPng(c *middleware.Context) { accountId := c.GetAccountId() queryReader := utils.NewUrlQueryReader(c.Req.URL) - queryParams := "?render&accountId=" + strconv.Itoa(accountId) + "&" + c.Req.URL.RawQuery + queryParams := "?render&accountId=" + strconv.FormatInt(accountId, 10) + "&" + c.Req.URL.RawQuery renderOpts := &renderer.RenderOpts{ Url: c.Params("*") + queryParams, diff --git a/pkg/routes/index.go b/pkg/routes/index.go index 9839d7fd38c..50e4d6edb82 100644 --- a/pkg/routes/index.go +++ b/pkg/routes/index.go @@ -6,7 +6,6 @@ import ( "github.com/torkelo/grafana-pro/pkg/routes/api" "github.com/torkelo/grafana-pro/pkg/routes/apimodel" "github.com/torkelo/grafana-pro/pkg/routes/login" - "github.com/torkelo/grafana-pro/pkg/stores/sqlstore" ) func Register(m *macaron.Macaron) { @@ -33,8 +32,6 @@ func Register(m *macaron.Macaron) { } func Index(ctx *middleware.Context) { - sqlstore.GetAccounts() - ctx.Data["User"] = apimodel.NewCurrentUserDto(ctx.UserAccount) ctx.HTML(200, "index") } diff --git a/pkg/routes/login/login_oauth.go b/pkg/routes/login/login_oauth.go index 0fac31ff088..83f3412b7a9 100644 --- a/pkg/routes/login/login_oauth.go +++ b/pkg/routes/login/login_oauth.go @@ -63,6 +63,8 @@ func OAuthLogin(ctx *middleware.Context) { ctx.Handle(500, "Failed to create account", err) return } + } else if err != nil { + ctx.Handle(500, "Unexpected error", err) } // login diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index 90b07dbf92c..1b02c8d8faa 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/pkg/stores/sqlstore/sqlstore.go @@ -6,9 +6,11 @@ import ( "path" "strings" + "github.com/torkelo/grafana-pro/pkg/models" + "github.com/torkelo/grafana-pro/pkg/setting" + "github.com/go-xorm/xorm" _ "github.com/mattn/go-sqlite3" - "github.com/torkelo/grafana-pro/pkg/setting" ) var ( @@ -24,14 +26,14 @@ var ( UseSQLite3 bool ) -type AccountDto struct { - Id int64 - Email string `xorm:"UNIQUE NOT NULL"` - Passwd string `xorm:"NOT NULL"` -} +func Init() { + tables = append(tables, new(models.Account), new(models.Dashboard)) -func init() { - tables = append(tables, new(AccountDto)) + models.CreateAccount = CreateAccount + models.GetAccount = GetAccount + models.GetAccountByLogin = GetAccountByLogin + models.GetDashboard = GetDashboard + models.SaveDashboard = SaveDashboard } func LoadModelsConfig() { diff --git a/pkg/stores/sqlstore/sqlstore_accounts.go b/pkg/stores/sqlstore/sqlstore_accounts.go index 7a7b833ff73..60ea2034004 100644 --- a/pkg/stores/sqlstore/sqlstore_accounts.go +++ b/pkg/stores/sqlstore/sqlstore_accounts.go @@ -1,8 +1,10 @@ package sqlstore -import "github.com/torkelo/grafana-pro/pkg/log" +import ( + "github.com/torkelo/grafana-pro/pkg/models" +) -func SaveAccount() error { +func CreateAccount(account *models.Account) error { var err error sess := x.NewSession() @@ -12,12 +14,7 @@ func SaveAccount() error { return err } - u := &AccountDto{ - Email: "asdasdas", - Passwd: "MyPassWd", - } - - if _, err = sess.Insert(u); err != nil { + if _, err = sess.Insert(account); err != nil { sess.Rollback() return err } else if err = sess.Commit(); err != nil { @@ -27,14 +24,32 @@ func SaveAccount() error { return nil } -func GetAccounts() { - var resp = make([]*AccountDto, 1) - err := x.Find(&resp) +func GetAccount(id int64) (*models.Account, error) { + var err error + + account := &models.Account{Id: id} + has, err := x.Get(account) + if err != nil { - log.Error(4, "Error", err) + return nil, err + } else if has == false { + return nil, models.ErrAccountNotFound } - for _, i := range resp { - log.Info("Item %v", i) + return account, nil +} + +func GetAccountByLogin(emailOrLogin string) (*models.Account, error) { + var err error + + account := &models.Account{Login: emailOrLogin} + has, err := x.Get(account) + + if err != nil { + return nil, err + } else if has == false { + return nil, models.ErrAccountNotFound } + + return account, nil } diff --git a/pkg/stores/sqlstore/sqlstore_dashboards.go b/pkg/stores/sqlstore/sqlstore_dashboards.go new file mode 100644 index 00000000000..27e148d0a29 --- /dev/null +++ b/pkg/stores/sqlstore/sqlstore_dashboards.go @@ -0,0 +1,38 @@ +package sqlstore + +import ( + "github.com/torkelo/grafana-pro/pkg/models" +) + +func SaveDashboard(dash *models.Dashboard) error { + var err error + + sess := x.NewSession() + defer sess.Close() + + if err = sess.Begin(); err != nil { + return err + } + + if _, err = sess.Insert(dash); err != nil { + sess.Rollback() + return err + } else if err = sess.Commit(); err != nil { + return err + } + + return nil +} + +func GetDashboard(slug string, accountId int64) (*models.Dashboard, error) { + + dashboard := models.Dashboard{Slug: slug, AccountId: accountId} + has, err := x.Get(&dashboard) + if err != nil { + return nil, err + } else if has == false { + return nil, models.ErrDashboardNotFound + } + + return &dashboard, nil +}