clean up duplicated user creation code (#50178)
* clean up duplicated user creation code * remove unused duplicate getOrCreateOrg function * fix up tests
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@@ -317,49 +316,3 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro
|
||||
|
||||
return org.Id, nil
|
||||
}
|
||||
|
||||
func getOrCreateOrg(sess *DBSession, orgName string) (int64, error) {
|
||||
var org models.Org
|
||||
if setting.AutoAssignOrg {
|
||||
has, err := sess.Where("id=?", setting.AutoAssignOrgId).Get(&org)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if has {
|
||||
return org.Id, nil
|
||||
}
|
||||
|
||||
if setting.AutoAssignOrgId != 1 {
|
||||
sqlog.Error("Could not create user: organization ID does not exist", "orgID",
|
||||
setting.AutoAssignOrgId)
|
||||
return 0, fmt.Errorf("could not create user: organization ID %d does not exist",
|
||||
setting.AutoAssignOrgId)
|
||||
}
|
||||
|
||||
org.Name = MainOrgName
|
||||
org.Id = int64(setting.AutoAssignOrgId)
|
||||
} else {
|
||||
org.Name = orgName
|
||||
}
|
||||
|
||||
org.Created = time.Now()
|
||||
org.Updated = time.Now()
|
||||
|
||||
if org.Id != 0 {
|
||||
if _, err := sess.InsertId(&org); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
} else {
|
||||
if _, err := sess.InsertOne(&org); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
sess.publishAfterCommit(&events.OrgCreated{
|
||||
Timestamp: org.Created,
|
||||
Id: org.Id,
|
||||
Name: org.Name,
|
||||
})
|
||||
|
||||
return org.Id, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user