Commit 757b1853 by Torkel Ödegaard

Worked on ease of use for non multi tenant scenarios, Closes #20

parent 25751949
...@@ -40,16 +40,28 @@ session_id_hashkey = ...@@ -40,16 +40,28 @@ session_id_hashkey =
admin_user = admin admin_user = admin
; default admin password, can be changed before first start of grafana, or in profile settings ; default admin password, can be changed before first start of grafana, or in profile settings
admin_password = admin admin_password = admin
; used for sig ; used for signing
secret_key = !#@FDEWREWR&*( secret_key = SW2YcwTIb9zpOOhoPsMm
; Auto-login remember days ; Auto-login remember days
login_remember_days = 7 login_remember_days = 7
cookie_username = grafana_user cookie_username = grafana_user
cookie_remember_name = grafana_remember cookie_remember_name = grafana_remember
[auth] [account.single]
anonymous = false ; Enable this feature to auto assign new users to a single account, suitable for NON multi tenant setups
anonymous_account_id = enabled = true
; Name of default account
account_name = main
; Default role new users will be automatically assigned
default_role = Editor
[auth.anonymous]
; enable anonymous access
enabled = false
; specify account name that should be used for unauthenticated users
account = main
; specify role for unauthenticated users
role = Viewer
[auth.github] [auth.github]
enabled = false enabled = false
......
...@@ -4,16 +4,12 @@ import ( ...@@ -4,16 +4,12 @@ import (
"github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/util"
) )
// POST /api/account/signup // POST /api/user/signup
func SignUp(c *middleware.Context, cmd m.CreateUserCommand) { func SignUp(c *middleware.Context, cmd m.CreateUserCommand) {
cmd.Login = cmd.Email cmd.Login = cmd.Email
cmd.Salt = util.GetRandomString(10)
cmd.Rands = util.GetRandomString(10)
cmd.Password = util.EncodePassword(cmd.Password, cmd.Salt)
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "failed to create user", err) c.JsonApiErr(500, "failed to create user", err)
......
...@@ -36,8 +36,6 @@ type CreateUserCommand struct { ...@@ -36,8 +36,6 @@ type CreateUserCommand struct {
Name string `json:"name"` Name string `json:"name"`
Company string `json:"compay"` Company string `json:"compay"`
Password string `json:"password" binding:"Required"` Password string `json:"password" binding:"Required"`
Salt string `json:"-"`
Rands string `json:"-"`
IsAdmin bool `json:"-"` IsAdmin bool `json:"-"`
Result User `json:"-"` Result User `json:"-"`
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
) )
func TestAccountDataAccess(t *testing.T) { func TestAccountDataAccess(t *testing.T) {
...@@ -13,7 +14,34 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -13,7 +14,34 @@ func TestAccountDataAccess(t *testing.T) {
Convey("Testing Account DB Access", t, func() { Convey("Testing Account DB Access", t, func() {
InitTestDB(t) InitTestDB(t)
Convey("Given single account mode", func() {
setting.SingleAccountMode = true
setting.DefaultAccountName = "test"
setting.DefaultAccountRole = "Viewer"
Convey("Users should be added to default account", 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"}
err := CreateUser(&ac1cmd)
So(err, ShouldBeNil)
err = CreateUser(&ac2cmd)
So(err, ShouldBeNil)
q1 := m.GetUserAccountsQuery{UserId: ac1cmd.Result.Id}
q2 := m.GetUserAccountsQuery{UserId: ac2cmd.Result.Id}
GetUserAccounts(&q1)
GetUserAccounts(&q2)
So(q1.Result[0].AccountId, ShouldEqual, q2.Result[0].AccountId)
So(q1.Result[0].Role, ShouldEqual, "Viewer")
})
})
Convey("Given two saved users", func() { Convey("Given two saved users", func() {
setting.SingleAccountMode = false
setting.DefaultAccountName = "test"
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"} 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} ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
func InitTestDB(t *testing.T) { func InitTestDB(t *testing.T) {
t.Log("InitTestDB")
x, err := xorm.NewEngine(sqlutil.TestDB_Sqlite3.DriverName, sqlutil.TestDB_Sqlite3.ConnStr) x, err := xorm.NewEngine(sqlutil.TestDB_Sqlite3.DriverName, sqlutil.TestDB_Sqlite3.ConnStr)
//x, err := xorm.NewEngine(sqlutil.TestDB_Mysql.DriverName, sqlutil.TestDB_Mysql.ConnStr) //x, err := xorm.NewEngine(sqlutil.TestDB_Mysql.DriverName, sqlutil.TestDB_Mysql.ConnStr)
//x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, sqlutil.TestDB_Postgres.ConnStr) //x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, sqlutil.TestDB_Postgres.ConnStr)
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/services/sqlstore/migrator" "github.com/torkelo/grafana-pro/pkg/services/sqlstore/migrator"
"github.com/torkelo/grafana-pro/pkg/setting" "github.com/torkelo/grafana-pro/pkg/setting"
"github.com/torkelo/grafana-pro/pkg/util"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
...@@ -40,9 +39,7 @@ func EnsureAdminUser() { ...@@ -40,9 +39,7 @@ func EnsureAdminUser() {
cmd := m.CreateUserCommand{} cmd := m.CreateUserCommand{}
cmd.Login = setting.AdminUser cmd.Login = setting.AdminUser
cmd.Email = setting.AdminUser + "@localhost" cmd.Email = setting.AdminUser + "@localhost"
cmd.Salt = util.GetRandomString(10) cmd.Password = setting.AdminPassword
cmd.Rands = util.GetRandomString(10)
cmd.Password = util.EncodePassword(setting.AdminPassword, cmd.Salt)
cmd.IsAdmin = true cmd.IsAdmin = true
if err = bus.Dispatch(&cmd); err != nil { if err = bus.Dispatch(&cmd); err != nil {
......
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/bus"
m "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/util"
) )
func init() { func init() {
...@@ -21,48 +23,76 @@ func init() { ...@@ -21,48 +23,76 @@ func init() {
bus.AddHandler("sql", GetUserAccounts) bus.AddHandler("sql", GetUserAccounts)
} }
func CreateUser(cmd *m.CreateUserCommand) error { func getAccountIdForNewUser(userEmail string, sess *xorm.Session) (int64, error) {
return inTransaction(func(sess *xorm.Session) error { var account m.Account
// create account if setting.SingleAccountMode {
account := m.Account{ has, err := sess.Where("name=?", setting.DefaultAccountName).Get(&account)
Name: cmd.Email, if err != nil {
Created: time.Now(), return 0, err
Updated: time.Now(),
} }
if has {
return account.Id, nil
} else {
account.Name = setting.DefaultAccountName
}
} else {
account.Name = userEmail
}
account.Created = time.Now()
account.Updated = time.Now()
if _, err := sess.Insert(&account); err != nil { if _, err := sess.Insert(&account); err != nil {
return 0, err
}
return account.Id, nil
}
func CreateUser(cmd *m.CreateUserCommand) error {
return inTransaction(func(sess *xorm.Session) error {
accountId, err := getAccountIdForNewUser(cmd.Email, sess)
if err != nil {
return err return err
} }
// create user // create user
user := m.User{ user := m.User{
Email: cmd.Email, Email: cmd.Email,
Password: cmd.Password,
Name: cmd.Name, Name: cmd.Name,
Login: cmd.Login, Login: cmd.Login,
Company: cmd.Company, Company: cmd.Company,
Salt: cmd.Salt,
Rands: cmd.Rands,
IsAdmin: cmd.IsAdmin, IsAdmin: cmd.IsAdmin,
AccountId: account.Id, AccountId: accountId,
Created: time.Now(), Created: time.Now(),
Updated: time.Now(), Updated: time.Now(),
} }
user.Salt = util.GetRandomString(10)
user.Rands = util.GetRandomString(10)
user.Password = util.EncodePassword(cmd.Password, user.Salt)
sess.UseBool("is_admin") sess.UseBool("is_admin")
if _, err := sess.Insert(&user); err != nil { if _, err := sess.Insert(&user); err != nil {
return err return err
} }
// create account user link // create account user link
_, err := sess.Insert(&m.AccountUser{ accountUser := m.AccountUser{
AccountId: account.Id, AccountId: accountId,
UserId: user.Id, UserId: user.Id,
Role: m.ROLE_ADMIN, Role: m.ROLE_ADMIN,
Created: time.Now(), Created: time.Now(),
Updated: time.Now(), Updated: time.Now(),
}) }
if setting.SingleAccountMode {
accountUser.Role = m.RoleType(setting.DefaultAccountRole)
}
_, err = sess.Insert(&accountUser)
cmd.Result = user cmd.Result = user
return err return err
......
...@@ -64,6 +64,11 @@ var ( ...@@ -64,6 +64,11 @@ var (
CookieUserName string CookieUserName string
CookieRememberName string CookieRememberName string
// single account
SingleAccountMode bool
DefaultAccountName string
DefaultAccountRole string
// Http auth // Http auth
AdminUser string AdminUser string
AdminPassword string AdminPassword string
...@@ -190,7 +195,12 @@ func NewConfigContext() { ...@@ -190,7 +195,12 @@ func NewConfigContext() {
CookieUserName = security.Key("cookie_username").String() CookieUserName = security.Key("cookie_username").String()
CookieRememberName = security.Key("cookie_remember_name").String() CookieRememberName = security.Key("cookie_remember_name").String()
// Http auth // single account
SingleAccountMode = Cfg.Section("account.single").Key("enabled").MustBool(false)
DefaultAccountName = Cfg.Section("account.single").Key("account_name").MustString("main")
DefaultAccountRole = Cfg.Section("account.single").Key("default_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
// admin
AdminUser = security.Key("admin_user").String() AdminUser = security.Key("admin_user").String()
AdminPassword = security.Key("admin_password").String() AdminPassword = security.Key("admin_password").String()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment