mirror of https://github.com/grafana/grafana
parent
d8e5be5782
commit
f1996a9f1f
@ -0,0 +1,20 @@ |
||||
|
||||
User |
||||
Id |
||||
Name |
||||
Login |
||||
Email |
||||
ActiveAccountId |
||||
|
||||
Account |
||||
Id |
||||
Name |
||||
PaymentDetails |
||||
|
||||
AccountUser |
||||
Id |
||||
AccountId |
||||
UserId |
||||
Role |
||||
|
||||
|
||||
@ -0,0 +1,46 @@ |
||||
package models |
||||
|
||||
import "time" |
||||
|
||||
type User struct { |
||||
Id int64 |
||||
Email string |
||||
Name string |
||||
Login string |
||||
Password string |
||||
Salt string |
||||
|
||||
IsAdmin bool |
||||
AccountId int64 |
||||
|
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
type Account2 struct { |
||||
Id int64 |
||||
Name string |
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
type AccountUser struct { |
||||
AccountId int64 |
||||
UserId int64 |
||||
Role RoleType |
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type CreateUserCommand struct { |
||||
Email string |
||||
Login string |
||||
Password string |
||||
Salt string |
||||
IsAdmin bool |
||||
|
||||
Result User `json:"-"` |
||||
} |
||||
@ -0,0 +1,58 @@ |
||||
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", CreateUser) |
||||
} |
||||
|
||||
func CreateUser(cmd *m.CreateUserCommand) error { |
||||
return inTransaction(func(sess *xorm.Session) error { |
||||
|
||||
// create account
|
||||
account := m.Account2{ |
||||
Name: cmd.Email, |
||||
Created: time.Now(), |
||||
Updated: time.Now(), |
||||
} |
||||
|
||||
if _, err := sess.Insert(&account); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// create user
|
||||
user := m.User{ |
||||
Email: cmd.Email, |
||||
Password: cmd.Password, |
||||
Salt: cmd.Salt, |
||||
IsAdmin: cmd.IsAdmin, |
||||
AccountId: account.Id, |
||||
Created: time.Now(), |
||||
Updated: time.Now(), |
||||
} |
||||
|
||||
sess.UseBool("is_admin") |
||||
if _, err := sess.Insert(&user); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// create account user link
|
||||
_, err := sess.Insert(&m.AccountUser{ |
||||
AccountId: account.Id, |
||||
UserId: user.Id, |
||||
Role: m.ROLE_ADMIN, |
||||
Created: time.Now(), |
||||
Updated: time.Now(), |
||||
}) |
||||
|
||||
cmd.Result = user |
||||
return err |
||||
}) |
||||
} |
||||
@ -0,0 +1,30 @@ |
||||
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() { |
||||
}) |
||||
}) |
||||
}) |
||||
} |
||||
Loading…
Reference in new issue