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

This commit is contained in:
Torkel Ödegaard
2015-01-27 15:14:53 +01:00
parent 257519490a
commit 757b185398
8 changed files with 105 additions and 33 deletions
+46 -16
View File
@@ -8,6 +8,8 @@ import (
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
"github.com/torkelo/grafana-pro/pkg/util"
)
func init() {
@@ -21,48 +23,76 @@ func init() {
bus.AddHandler("sql", GetUserAccounts)
}
func getAccountIdForNewUser(userEmail string, sess *xorm.Session) (int64, error) {
var account m.Account
if setting.SingleAccountMode {
has, err := sess.Where("name=?", setting.DefaultAccountName).Get(&account)
if err != nil {
return 0, err
}
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 {
return 0, err
}
return account.Id, nil
}
func CreateUser(cmd *m.CreateUserCommand) error {
return inTransaction(func(sess *xorm.Session) error {
// create account
account := m.Account{
Name: cmd.Email,
Created: time.Now(),
Updated: time.Now(),
}
if _, err := sess.Insert(&account); err != nil {
accountId, err := getAccountIdForNewUser(cmd.Email, sess)
if err != nil {
return err
}
// create user
user := m.User{
Email: cmd.Email,
Password: cmd.Password,
Name: cmd.Name,
Login: cmd.Login,
Company: cmd.Company,
Salt: cmd.Salt,
Rands: cmd.Rands,
IsAdmin: cmd.IsAdmin,
AccountId: account.Id,
AccountId: accountId,
Created: 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")
if _, err := sess.Insert(&user); err != nil {
return err
}
// create account user link
_, err := sess.Insert(&m.AccountUser{
AccountId: account.Id,
accountUser := m.AccountUser{
AccountId: accountId,
UserId: user.Id,
Role: m.ROLE_ADMIN,
Created: time.Now(),
Updated: time.Now(),
})
}
if setting.SingleAccountMode {
accountUser.Role = m.RoleType(setting.DefaultAccountRole)
}
_, err = sess.Insert(&accountUser)
cmd.Result = user
return err