clean up duplicated user creation code (#50178) (#50327)

* clean up duplicated user creation code

* remove unused duplicate getOrCreateOrg function

* fix up tests

(cherry picked from commit 9350ab781c)
This commit is contained in:
Dan Cech
2022-06-13 07:12:00 -04:00
committed by GitHub
parent 5390fa57e9
commit bc2f941004
12 changed files with 49 additions and 217 deletions
-47
View File
@@ -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
}