mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
check if admin exists or create one in one transaction
This commit is contained in:
parent
263572813a
commit
1bd31aa313
@ -143,9 +143,8 @@ func (ss *SqlStore) ensureAdminUser() error {
|
||||
systemUserCountQuery := m.GetSystemUserCountStatsQuery{}
|
||||
|
||||
err := bus.InTransaction(context.Background(), func(ctx context.Context) error {
|
||||
return bus.DispatchCtx(ctx, &systemUserCountQuery)
|
||||
})
|
||||
|
||||
err := bus.DispatchCtx(ctx, &systemUserCountQuery)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not determine if admin user exists: %v", err)
|
||||
}
|
||||
@ -160,13 +159,16 @@ func (ss *SqlStore) ensureAdminUser() error {
|
||||
cmd.Password = setting.AdminPassword
|
||||
cmd.IsAdmin = true
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &cmd); err != nil {
|
||||
return fmt.Errorf("Failed to create admin user: %v", err)
|
||||
}
|
||||
|
||||
ss.log.Info("Created default admin user: %v", setting.AdminUser)
|
||||
ss.log.Info("Created default admin", "user", setting.AdminUser)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ss *SqlStore) buildConnectionString() (string, error) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -30,6 +31,8 @@ func init() {
|
||||
bus.AddHandler("sql", DeleteUser)
|
||||
bus.AddHandler("sql", UpdateUserPermissions)
|
||||
bus.AddHandler("sql", SetUserHelpFlag)
|
||||
|
||||
bus.AddCtxHandler("sql", CreateUserCtx)
|
||||
}
|
||||
|
||||
func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error) {
|
||||
@ -79,8 +82,7 @@ func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error
|
||||
return org.Id, nil
|
||||
}
|
||||
|
||||
func CreateUser(cmd *m.CreateUserCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
func internalCreateUser(sess *DBSession, cmd *m.CreateUserCommand) error {
|
||||
orgId, err := getOrgIdForNewUser(cmd, sess)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -150,6 +152,17 @@ func CreateUser(cmd *m.CreateUserCommand) error {
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateUserCtx(ctx context.Context, cmd *m.CreateUserCommand) error {
|
||||
return inTransactionWithRetryCtx(ctx, func(sess *DBSession) error {
|
||||
return internalCreateUser(sess, cmd)
|
||||
}, 0)
|
||||
}
|
||||
|
||||
func CreateUser(cmd *m.CreateUserCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
return internalCreateUser(sess, cmd)
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user