mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Split Create User (#50502)
* Split Create User * Use new create user and User from package user * Add service to wire * Making create user work * Replace user from user pkg * One more * Move Insert to orguser Service/Store * Remove unnecessary conversion * Cleaunp * Fix Get User and add fakes * Fixing get org id for user logic, adding fakes and other adjustments * Add some tests for ourguser service and store * Fix insert org logic * Add comment about deprecation * Fix after merge with main * Move orguser service/store to org service/store * Remove orguser from wire * Unimplement new Create user and use User from pkg user * Fix wire generation * Fix lint * Fix lint - use only User and CrateUserCommand from user pkg * Remove User and CreateUserCommand from models * Fix lint 2
This commit is contained in:
78
pkg/services/org/orgimpl/org.go
Normal file
78
pkg/services/org/orgimpl/org.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package orgimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store store
|
||||
cfg *setting.Cfg
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB, cfg *setting.Cfg) org.Service {
|
||||
return &Service{
|
||||
store: &sqlStore{
|
||||
db: db,
|
||||
dialect: db.GetDialect(),
|
||||
},
|
||||
cfg: cfg,
|
||||
log: log.New("org service"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) GetIDForNewUser(ctx context.Context, cmd org.GetOrgIDForNewUserCommand) (int64, error) {
|
||||
var orga org.Org
|
||||
if cmd.SkipOrgSetup {
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
if setting.AutoAssignOrg && cmd.OrgID != 0 {
|
||||
_, err := s.store.Get(ctx, cmd.OrgID)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return cmd.OrgID, nil
|
||||
}
|
||||
|
||||
orgName := cmd.OrgName
|
||||
if len(orgName) == 0 {
|
||||
orgName = util.StringsFallback2(cmd.Email, cmd.Login)
|
||||
}
|
||||
|
||||
if setting.AutoAssignOrg {
|
||||
orga, err := s.store.Get(ctx, int64(s.cfg.AutoAssignOrgId))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if orga.ID != 0 {
|
||||
return orga.ID, nil
|
||||
}
|
||||
if setting.AutoAssignOrgId != 1 {
|
||||
s.log.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)
|
||||
}
|
||||
orga.Name = MainOrgName
|
||||
orga.ID = int64(setting.AutoAssignOrgId)
|
||||
} else {
|
||||
orga.Name = orgName
|
||||
}
|
||||
orga.Created = time.Now()
|
||||
orga.Updated = time.Now()
|
||||
|
||||
return s.store.Insert(ctx, &orga)
|
||||
}
|
||||
|
||||
func (s *Service) InsertUser(ctx context.Context, orguser *org.OrgUser) (int64, error) {
|
||||
return s.store.InsertUser(ctx, orguser)
|
||||
}
|
||||
72
pkg/services/org/orgimpl/org_test.go
Normal file
72
pkg/services/org/orgimpl/org_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package orgimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestOrgService(t *testing.T) {
|
||||
orgStore := newOrgStoreFake()
|
||||
orgService := Service{
|
||||
store: orgStore,
|
||||
cfg: setting.NewCfg(),
|
||||
}
|
||||
|
||||
t.Run("create org", func(t *testing.T) {
|
||||
_, err := orgService.GetIDForNewUser(context.Background(), org.GetOrgIDForNewUserCommand{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("create org", func(t *testing.T) {
|
||||
_, err := orgService.GetIDForNewUser(context.Background(), org.GetOrgIDForNewUserCommand{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("create org with auto assign org ID", func(t *testing.T) {
|
||||
setting.AutoAssignOrg = true
|
||||
setting.AutoAssignOrgId = 1
|
||||
orgStore.ExpectedOrgID = 1
|
||||
orgStore.ExpectedOrg = &org.Org{}
|
||||
_, err := orgService.GetIDForNewUser(context.Background(), org.GetOrgIDForNewUserCommand{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("create org with auto assign org ID and orgID", func(t *testing.T) {
|
||||
setting.AutoAssignOrg = true
|
||||
setting.AutoAssignOrgId = 1
|
||||
orgStore.ExpectedOrgID = 1
|
||||
orgStore.ExpectedOrg = &org.Org{}
|
||||
_, err := orgService.GetIDForNewUser(context.Background(), org.GetOrgIDForNewUserCommand{OrgID: 1})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
setting.AutoAssignOrg = false
|
||||
setting.AutoAssignOrgId = 0
|
||||
}
|
||||
|
||||
type FakeOrgStore struct {
|
||||
ExpectedOrg *org.Org
|
||||
ExpectedOrgID int64
|
||||
ExpectedUserID int64
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
func newOrgStoreFake() *FakeOrgStore {
|
||||
return &FakeOrgStore{}
|
||||
}
|
||||
|
||||
func (f *FakeOrgStore) Get(ctx context.Context, orgID int64) (*org.Org, error) {
|
||||
return f.ExpectedOrg, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgStore) Insert(ctx context.Context, org *org.Org) (int64, error) {
|
||||
return f.ExpectedOrgID, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgStore) InsertUser(ctx context.Context, org *org.OrgUser) (int64, error) {
|
||||
return f.ExpectedUserID, f.ExpectedError
|
||||
}
|
||||
83
pkg/services/org/orgimpl/store.go
Normal file
83
pkg/services/org/orgimpl/store.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package orgimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
)
|
||||
|
||||
const MainOrgName = "Main Org."
|
||||
|
||||
type store interface {
|
||||
Get(context.Context, int64) (*org.Org, error)
|
||||
Insert(context.Context, *org.Org) (int64, error)
|
||||
InsertUser(context.Context, *org.OrgUser) (int64, error)
|
||||
}
|
||||
|
||||
type sqlStore struct {
|
||||
db db.DB
|
||||
dialect migrator.Dialect
|
||||
}
|
||||
|
||||
func (ss *sqlStore) Get(ctx context.Context, orgID int64) (*org.Org, error) {
|
||||
var orga org.Org
|
||||
err := ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
has, err := sess.Where("id=?", orgID).Get(&orga)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &orga, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) Insert(ctx context.Context, org *org.Org) (int64, error) {
|
||||
var orgID int64
|
||||
var err error
|
||||
err = ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if orgID, err = sess.InsertOne(org); err != nil {
|
||||
return err
|
||||
}
|
||||
if org.ID != 0 {
|
||||
// it sets the setval in the sequence
|
||||
if err := ss.dialect.PostInsertId("org", sess.Session); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
sess.PublishAfterCommit(&events.OrgCreated{
|
||||
Timestamp: org.Created,
|
||||
Id: org.ID,
|
||||
Name: org.Name,
|
||||
})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return orgID, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) InsertUser(ctx context.Context, cmd *org.OrgUser) (int64, error) {
|
||||
var orgID int64
|
||||
var err error
|
||||
err = ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if orgID, err = sess.Insert(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return orgID, nil
|
||||
}
|
||||
73
pkg/services/org/orgimpl/store_test.go
Normal file
73
pkg/services/org/orgimpl/store_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package orgimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntegrationOrgDataAccess(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
|
||||
ss := sqlstore.InitTestDB(t)
|
||||
orgStore := sqlStore{
|
||||
db: ss,
|
||||
dialect: ss.GetDialect(),
|
||||
}
|
||||
|
||||
t.Run("org not found", func(t *testing.T) {
|
||||
_, err := orgStore.Get(context.Background(), 1)
|
||||
require.Error(t, err, org.ErrOrgNotFound)
|
||||
})
|
||||
|
||||
t.Run("org inserted", func(t *testing.T) {
|
||||
_, err := orgStore.Insert(context.Background(), &org.Org{
|
||||
Version: 1,
|
||||
Name: "test1",
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("org inserted with next available org ID", func(t *testing.T) {
|
||||
orgID, err := orgStore.Insert(context.Background(), &org.Org{
|
||||
ID: 55,
|
||||
Version: 1,
|
||||
Name: "test2",
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, err = orgStore.Get(context.Background(), orgID)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationOrgUserDataAccess(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
|
||||
ss := sqlstore.InitTestDB(t)
|
||||
orgUserStore := sqlStore{
|
||||
db: ss,
|
||||
}
|
||||
|
||||
t.Run("org user inserted", func(t *testing.T) {
|
||||
_, err := orgUserStore.InsertUser(context.Background(), &org.OrgUser{
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user