chore: remove CreateUser from sqlstore & replace with userService.CreateUserForTests (#59910)
This commit is contained in:
+94
-107
@@ -4,8 +4,8 @@ package sqlstore
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
const mainOrgName = "Main Org."
|
||||
|
||||
func (ss *SQLStore) getOrgIDForNewUser(sess *DBSession, args user.CreateUserCommand) (int64, error) {
|
||||
if ss.Cfg.AutoAssignOrg && args.OrgID != 0 {
|
||||
if err := verifyExistingOrg(sess, args.OrgID); err != nil {
|
||||
@@ -30,21 +32,19 @@ func (ss *SQLStore) getOrgIDForNewUser(sess *DBSession, args user.CreateUserComm
|
||||
return ss.getOrCreateOrg(sess, orgName)
|
||||
}
|
||||
|
||||
// createUser creates a user in the database
|
||||
// if autoAssignOrg is enabled then args.OrgID will be used
|
||||
// to add to an existing Org with id=args.OrgID
|
||||
// if autoAssignOrg is disabled then args.OrgName will be used
|
||||
// to create a new Org with name=args.OrgName.
|
||||
// If a org already exists with that name, it will error
|
||||
// createUser creates a user in the database. It will also create a default
|
||||
// organization, if none exists. This should only be used by the sqlstore
|
||||
// Reset() function.
|
||||
//
|
||||
// If AutoAssignOrg is enabled then args.OrgID will be used to add to an
|
||||
// existing Org with id=args.OrgID. If AutoAssignOrg is disabled then
|
||||
// args.OrgName will be used to create a new Org with name=args.OrgName. If an
|
||||
// org already exists with that name, it will error.
|
||||
func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args user.CreateUserCommand) (user.User, error) {
|
||||
var usr user.User
|
||||
var orgID int64 = -1
|
||||
if !args.SkipOrgSetup {
|
||||
var err error
|
||||
orgID, err = ss.getOrgIDForNewUser(sess, args)
|
||||
if err != nil {
|
||||
return usr, err
|
||||
}
|
||||
orgID, err := ss.getOrgIDForNewUser(sess, args)
|
||||
if err != nil {
|
||||
return usr, err
|
||||
}
|
||||
|
||||
if args.Email == "" {
|
||||
@@ -68,18 +68,13 @@ func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args user.C
|
||||
|
||||
// create user
|
||||
usr = user.User{
|
||||
Email: args.Email,
|
||||
Name: args.Name,
|
||||
Login: args.Login,
|
||||
Company: args.Company,
|
||||
IsAdmin: args.IsAdmin,
|
||||
IsDisabled: args.IsDisabled,
|
||||
OrgID: orgID,
|
||||
EmailVerified: args.EmailVerified,
|
||||
Created: TimeNow(),
|
||||
Updated: TimeNow(),
|
||||
LastSeenAt: TimeNow().AddDate(-10, 0, 0),
|
||||
IsServiceAccount: args.IsServiceAccount,
|
||||
Email: args.Email,
|
||||
Login: args.Login,
|
||||
IsAdmin: args.IsAdmin,
|
||||
OrgID: orgID,
|
||||
Created: TimeNow(),
|
||||
Updated: TimeNow(),
|
||||
LastSeenAt: TimeNow().AddDate(-10, 0, 0),
|
||||
}
|
||||
|
||||
salt, err := util.GetRandomString(10)
|
||||
@@ -115,95 +110,29 @@ func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args user.C
|
||||
Email: usr.Email,
|
||||
})
|
||||
|
||||
// create org user link
|
||||
if !args.SkipOrgSetup {
|
||||
orgUser := models.OrgUser{
|
||||
OrgId: orgID,
|
||||
UserId: usr.ID,
|
||||
Role: org.RoleAdmin,
|
||||
Created: TimeNow(),
|
||||
Updated: TimeNow(),
|
||||
}
|
||||
orgUser := models.OrgUser{
|
||||
OrgId: orgID,
|
||||
UserId: usr.ID,
|
||||
Role: org.RoleAdmin,
|
||||
Created: TimeNow(),
|
||||
Updated: TimeNow(),
|
||||
}
|
||||
|
||||
if ss.Cfg.AutoAssignOrg && !usr.IsAdmin {
|
||||
if len(args.DefaultOrgRole) > 0 {
|
||||
orgUser.Role = org.RoleType(args.DefaultOrgRole)
|
||||
} else {
|
||||
orgUser.Role = org.RoleType(ss.Cfg.AutoAssignOrgRole)
|
||||
}
|
||||
if ss.Cfg.AutoAssignOrg && !usr.IsAdmin {
|
||||
if len(args.DefaultOrgRole) > 0 {
|
||||
orgUser.Role = org.RoleType(args.DefaultOrgRole)
|
||||
} else {
|
||||
orgUser.Role = org.RoleType(ss.Cfg.AutoAssignOrgRole)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = sess.Insert(&orgUser); err != nil {
|
||||
return usr, err
|
||||
}
|
||||
if _, err = sess.Insert(&orgUser); err != nil {
|
||||
return usr, err
|
||||
}
|
||||
|
||||
return usr, nil
|
||||
}
|
||||
|
||||
// deprecated method, use only for tests
|
||||
func (ss *SQLStore) CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error) {
|
||||
var user user.User
|
||||
createErr := ss.WithTransactionalDbSession(ctx, func(sess *DBSession) (err error) {
|
||||
user, err = ss.createUser(ctx, sess, cmd)
|
||||
return
|
||||
})
|
||||
return &user, createErr
|
||||
}
|
||||
|
||||
func notServiceAccountFilter(ss *SQLStore) string {
|
||||
return fmt.Sprintf("%s.is_service_account = %s",
|
||||
ss.Dialect.Quote("user"),
|
||||
ss.Dialect.BooleanStr(false))
|
||||
}
|
||||
|
||||
func setUsingOrgInTransaction(sess *DBSession, userID int64, orgID int64) error {
|
||||
user := user.User{
|
||||
ID: userID,
|
||||
OrgID: orgID,
|
||||
}
|
||||
|
||||
_, err := sess.ID(userID).Update(&user)
|
||||
return err
|
||||
}
|
||||
|
||||
type byOrgName []*models.UserOrgDTO
|
||||
|
||||
// Len returns the length of an array of organisations.
|
||||
func (o byOrgName) Len() int {
|
||||
return len(o)
|
||||
}
|
||||
|
||||
// Swap swaps two indices of an array of organizations.
|
||||
func (o byOrgName) Swap(i, j int) {
|
||||
o[i], o[j] = o[j], o[i]
|
||||
}
|
||||
|
||||
// Less returns whether element i of an array of organizations is less than element j.
|
||||
func (o byOrgName) Less(i, j int) bool {
|
||||
if strings.ToLower(o[i].Name) < strings.ToLower(o[j].Name) {
|
||||
return true
|
||||
}
|
||||
|
||||
return o[i].Name < o[j].Name
|
||||
}
|
||||
|
||||
func (ss *SQLStore) getUserOrgList(ctx context.Context, query *models.GetUserOrgListQuery) error {
|
||||
return ss.WithDbSession(ctx, func(dbSess *DBSession) error {
|
||||
query.Result = make([]*models.UserOrgDTO, 0)
|
||||
sess := dbSess.Table("org_user")
|
||||
sess.Join("INNER", "org", "org_user.org_id=org.id")
|
||||
sess.Join("INNER", ss.Dialect.Quote("user"), fmt.Sprintf("org_user.user_id=%s.id", ss.Dialect.Quote("user")))
|
||||
sess.Where("org_user.user_id=?", query.UserId)
|
||||
sess.Where(notServiceAccountFilter(ss))
|
||||
sess.Cols("org.name", "org_user.role", "org_user.org_id")
|
||||
sess.OrderBy("org.name")
|
||||
err := sess.Find(&query.Result)
|
||||
sort.Sort(byOrgName(query.Result))
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func UserDeletions() []string {
|
||||
deletes := []string{
|
||||
"DELETE FROM star WHERE user_id = ?",
|
||||
@@ -218,3 +147,61 @@ func UserDeletions() []string {
|
||||
}
|
||||
return deletes
|
||||
}
|
||||
|
||||
func verifyExistingOrg(sess *DBSession, orgId int64) error {
|
||||
var org models.Org
|
||||
has, err := sess.Where("id=?", orgId).Get(&org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrOrgNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, error) {
|
||||
var org models.Org
|
||||
if ss.Cfg.AutoAssignOrg {
|
||||
has, err := sess.Where("id=?", ss.Cfg.AutoAssignOrgId).Get(&org)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if has {
|
||||
return org.Id, nil
|
||||
}
|
||||
|
||||
if ss.Cfg.AutoAssignOrgId != 1 {
|
||||
ss.log.Error("Could not create user: organization ID does not exist", "orgID",
|
||||
ss.Cfg.AutoAssignOrgId)
|
||||
return 0, fmt.Errorf("could not create user: organization ID %d does not exist",
|
||||
ss.Cfg.AutoAssignOrgId)
|
||||
}
|
||||
|
||||
org.Name = mainOrgName
|
||||
org.Id = int64(ss.Cfg.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