mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add Get User Profile to user and Get User Org List to org service (#53788)
* Remove delete suer from store interface * Remove get signed in user with cache ctx from store interface * Support options when setting up access control tests * Fix broken tests * Fix lint * Add user fake to middleware * Fix middleware tests, remove usertest being initialised twice * Chore: Add Get User Profile to user and Get User Org List to org service Co-authored-by: Karl Persson <kalle.persson@grafana.com>
This commit is contained in:
parent
d1df896962
commit
e3501dfa4d
@ -61,6 +61,16 @@ type GetOrgIDForNewUserCommand struct {
|
||||
SkipOrgSetup bool
|
||||
}
|
||||
|
||||
type GetUserOrgListQuery struct {
|
||||
UserID int64
|
||||
}
|
||||
|
||||
type UserOrgDTO struct {
|
||||
OrgID int64 `json:"orgId"`
|
||||
Name string `json:"name"`
|
||||
Role RoleType `json:"role"`
|
||||
}
|
||||
|
||||
func (r RoleType) IsValid() bool {
|
||||
return r == RoleViewer || r == RoleAdmin || r == RoleEditor
|
||||
}
|
||||
|
@ -8,4 +8,5 @@ type Service interface {
|
||||
GetIDForNewUser(context.Context, GetOrgIDForNewUserCommand) (int64, error)
|
||||
InsertOrgUser(context.Context, *OrgUser) (int64, error)
|
||||
DeleteUserFromAll(context.Context, int64) error
|
||||
GetUserOrgList(context.Context, *GetUserOrgListQuery) ([]*UserOrgDTO, error)
|
||||
}
|
||||
|
@ -6,7 +6,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"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/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
@ -16,6 +18,8 @@ type Service struct {
|
||||
store store
|
||||
cfg *setting.Cfg
|
||||
log log.Logger
|
||||
// TODO remove sqlstore
|
||||
sqlStore *sqlstore.SQLStore
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB, cfg *setting.Cfg) org.Service {
|
||||
@ -80,3 +84,23 @@ func (s *Service) InsertOrgUser(ctx context.Context, orguser *org.OrgUser) (int6
|
||||
func (s *Service) DeleteUserFromAll(ctx context.Context, userID int64) error {
|
||||
return s.store.DeleteUserFromAll(ctx, userID)
|
||||
}
|
||||
|
||||
// TODO: remove wrapper around sqlstore
|
||||
func (s *Service) GetUserOrgList(ctx context.Context, query *org.GetUserOrgListQuery) ([]*org.UserOrgDTO, error) {
|
||||
q := &models.GetUserOrgListQuery{
|
||||
UserId: query.UserID,
|
||||
}
|
||||
err := s.sqlStore.GetUserOrgList(ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []*org.UserOrgDTO
|
||||
for _, orga := range q.Result {
|
||||
result = append(result, &org.UserOrgDTO{
|
||||
OrgID: orga.OrgId,
|
||||
Name: orga.Name,
|
||||
Role: orga.Role,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
@ -7,8 +7,9 @@ import (
|
||||
)
|
||||
|
||||
type FakeOrgService struct {
|
||||
ExpectedOrgUserID int64
|
||||
ExpectedError error
|
||||
ExpectedOrgUserID int64
|
||||
ExpectedError error
|
||||
ExpectedUserOrgDTO []*org.UserOrgDTO
|
||||
}
|
||||
|
||||
func NewOrgServiceFake() *FakeOrgService {
|
||||
@ -30,3 +31,7 @@ func (f *FakeOrgService) InsertOrgUser(ctx context.Context, cmd *org.OrgUser) (i
|
||||
func (f *FakeOrgService) DeleteUserFromAll(ctx context.Context, userID int64) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgService) GetUserOrgList(ctx context.Context, query *org.GetUserOrgListQuery) ([]*org.UserOrgDTO, error) {
|
||||
return f.ExpectedUserOrgDTO, f.ExpectedError
|
||||
}
|
||||
|
@ -135,6 +135,27 @@ type UserSearchHitDTO struct {
|
||||
AuthModule AuthModuleConversion `json:"-"`
|
||||
}
|
||||
|
||||
type GetUserProfileQuery struct {
|
||||
UserID int64
|
||||
}
|
||||
|
||||
type UserProfileDTO struct {
|
||||
ID int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Login string `json:"login"`
|
||||
Theme string `json:"theme"`
|
||||
OrgID int64 `json:"orgId,omitempty"`
|
||||
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
|
||||
IsDisabled bool `json:"isDisabled"`
|
||||
IsExternal bool `json:"isExternal"`
|
||||
AuthLabels []string `json:"authLabels"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
AvatarUrl string `json:"avatarUrl"`
|
||||
AccessControl map[string]bool `json:"accessControl,omitempty"`
|
||||
}
|
||||
|
||||
// implement Conversion interface to define custom field mapping (xorm feature)
|
||||
type AuthModuleConversion []string
|
||||
|
||||
|
@ -21,4 +21,5 @@ type Service interface {
|
||||
BatchDisableUsers(context.Context, *BatchDisableUsersCommand) error
|
||||
UpdatePermissions(int64, bool) error
|
||||
SetUserHelpFlag(context.Context, *SetUserHelpFlagCommand) error
|
||||
GetUserProfile(context.Context, *GetUserProfileQuery) (UserProfileDTO, error)
|
||||
}
|
||||
|
@ -410,3 +410,31 @@ func (s *Service) SetUserHelpFlag(ctx context.Context, cmd *user.SetUserHelpFlag
|
||||
}
|
||||
return s.sqlStore.SetUserHelpFlag(ctx, c)
|
||||
}
|
||||
|
||||
// TODO: remove wrapper around sqlstore
|
||||
func (s *Service) GetUserProfile(ctx context.Context, query *user.GetUserProfileQuery) (user.UserProfileDTO, error) {
|
||||
q := &models.GetUserProfileQuery{
|
||||
UserId: query.UserID,
|
||||
}
|
||||
err := s.sqlStore.GetUserProfile(ctx, q)
|
||||
if err != nil {
|
||||
return user.UserProfileDTO{}, err
|
||||
}
|
||||
result := user.UserProfileDTO{
|
||||
ID: q.Result.Id,
|
||||
Email: q.Result.Email,
|
||||
Name: q.Result.Name,
|
||||
Login: q.Result.Login,
|
||||
Theme: q.Result.Theme,
|
||||
OrgID: q.Result.OrgId,
|
||||
IsGrafanaAdmin: q.Result.IsGrafanaAdmin,
|
||||
IsDisabled: q.Result.IsDisabled,
|
||||
IsExternal: q.Result.IsExternal,
|
||||
AuthLabels: q.Result.AuthLabels,
|
||||
UpdatedAt: q.Result.UpdatedAt,
|
||||
CreatedAt: q.Result.CreatedAt,
|
||||
AvatarUrl: q.Result.AvatarUrl,
|
||||
AccessControl: q.Result.AccessControl,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ type FakeUserService struct {
|
||||
ExpectedError error
|
||||
ExpectedSetUsingOrgError error
|
||||
ExpectedSearchUsers user.SearchUserQueryResult
|
||||
ExpectedUSerProfileDTO user.UserProfileDTO
|
||||
}
|
||||
|
||||
func NewUserServiceFake() *FakeUserService {
|
||||
@ -84,3 +85,7 @@ func (f *FakeUserService) UpdatePermissions(userID int64, isAdmin bool) error {
|
||||
func (f *FakeUserService) SetUserHelpFlag(ctx context.Context, cmd *user.SetUserHelpFlagCommand) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeUserService) GetUserProfile(ctx context.Context, query *user.GetUserProfileQuery) (user.UserProfileDTO, error) {
|
||||
return f.ExpectedUSerProfileDTO, f.ExpectedError
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user