Move SignedInUser to user service and RoleType and Roles to org (#53445)
* Move SignedInUser to user service and RoleType and Roles to org * Use go naming convention for roles * Fix some imports and leftovers * Fix ldap debug test * Fix lint * Fix lint 2 * Fix lint 3 * Fix type and not needed conversion * Clean up messages in api tests * Clean up api tests 2
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,7 +25,7 @@ func isValidAction(action string) bool {
|
||||
}
|
||||
|
||||
type storageAuthService interface {
|
||||
newGuardian(ctx context.Context, user *models.SignedInUser, prefix string) fileGuardian
|
||||
newGuardian(ctx context.Context, user *user.SignedInUser, prefix string) fileGuardian
|
||||
}
|
||||
|
||||
type fileGuardian interface {
|
||||
@@ -39,7 +39,7 @@ type fileGuardian interface {
|
||||
|
||||
type pathFilterFileGuardian struct {
|
||||
ctx context.Context
|
||||
user *models.SignedInUser
|
||||
user *user.SignedInUser
|
||||
prefix string
|
||||
pathFilterByAction map[string]filestorage.PathFilter
|
||||
log log.Logger
|
||||
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/rendering"
|
||||
"github.com/grafana/grafana/pkg/services/store/sanitizer"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
func (s *standardStorageService) sanitizeContents(ctx context.Context, user *models.SignedInUser, req *UploadRequest, storagePath string) ([]byte, error) {
|
||||
func (s *standardStorageService) sanitizeContents(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) ([]byte, error) {
|
||||
if req.EntityType == EntityTypeImage {
|
||||
ext := filepath.Ext(req.Path)
|
||||
if ext == ".svg" {
|
||||
@@ -36,7 +36,7 @@ func (s *standardStorageService) sanitizeContents(ctx context.Context, user *mod
|
||||
return req.Contents, nil
|
||||
}
|
||||
|
||||
func (s *standardStorageService) sanitizeUploadRequest(ctx context.Context, user *models.SignedInUser, req *UploadRequest, storagePath string) (*filestorage.UpsertFileCommand, error) {
|
||||
func (s *standardStorageService) sanitizeUploadRequest(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) (*filestorage.UpsertFileCommand, error) {
|
||||
contents, err := s.sanitizeContents(ctx, user, req, storagePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -12,12 +12,12 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
@@ -41,8 +41,8 @@ const brandingStorage = "branding"
|
||||
const SystemBrandingStorage = "system/" + brandingStorage
|
||||
|
||||
var (
|
||||
SystemBrandingReader = &models.SignedInUser{OrgId: ac.GlobalOrgID}
|
||||
SystemBrandingAdmin = &models.SignedInUser{OrgId: ac.GlobalOrgID}
|
||||
SystemBrandingReader = &user.SignedInUser{OrgId: ac.GlobalOrgID}
|
||||
SystemBrandingAdmin = &user.SignedInUser{OrgId: ac.GlobalOrgID}
|
||||
)
|
||||
|
||||
const MAX_UPLOAD_SIZE = 1 * 1024 * 1024 // 3MB
|
||||
@@ -63,23 +63,23 @@ type StorageService interface {
|
||||
RegisterHTTPRoutes(routing.RouteRegister)
|
||||
|
||||
// List folder contents
|
||||
List(ctx context.Context, user *models.SignedInUser, path string) (*StorageListFrame, error)
|
||||
List(ctx context.Context, user *user.SignedInUser, path string) (*StorageListFrame, error)
|
||||
|
||||
// Read raw file contents out of the store
|
||||
Read(ctx context.Context, user *models.SignedInUser, path string) (*filestorage.File, error)
|
||||
Read(ctx context.Context, user *user.SignedInUser, path string) (*filestorage.File, error)
|
||||
|
||||
Upload(ctx context.Context, user *models.SignedInUser, req *UploadRequest) error
|
||||
Upload(ctx context.Context, user *user.SignedInUser, req *UploadRequest) error
|
||||
|
||||
Delete(ctx context.Context, user *models.SignedInUser, path string) error
|
||||
Delete(ctx context.Context, user *user.SignedInUser, path string) error
|
||||
|
||||
DeleteFolder(ctx context.Context, user *models.SignedInUser, cmd *DeleteFolderCmd) error
|
||||
DeleteFolder(ctx context.Context, user *user.SignedInUser, cmd *DeleteFolderCmd) error
|
||||
|
||||
CreateFolder(ctx context.Context, user *models.SignedInUser, cmd *CreateFolderCmd) error
|
||||
CreateFolder(ctx context.Context, user *user.SignedInUser, cmd *CreateFolderCmd) error
|
||||
|
||||
validateUploadRequest(ctx context.Context, user *models.SignedInUser, req *UploadRequest, storagePath string) validationResult
|
||||
validateUploadRequest(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) validationResult
|
||||
|
||||
// sanitizeUploadRequest sanitizes the upload request and converts it into a command accepted by the FileStorage API
|
||||
sanitizeUploadRequest(ctx context.Context, user *models.SignedInUser, req *UploadRequest, storagePath string) (*filestorage.UpsertFileCommand, error)
|
||||
sanitizeUploadRequest(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) (*filestorage.UpsertFileCommand, error)
|
||||
}
|
||||
|
||||
type standardStorageService struct {
|
||||
@@ -182,7 +182,7 @@ func ProvideService(
|
||||
|
||||
globalRoots = append(globalRoots, initializeOrgStorages(ac.GlobalOrgID)...)
|
||||
|
||||
authService := newStaticStorageAuthService(func(ctx context.Context, user *models.SignedInUser, storageName string) map[string]filestorage.PathFilter {
|
||||
authService := newStaticStorageAuthService(func(ctx context.Context, user *user.SignedInUser, storageName string) map[string]filestorage.PathFilter {
|
||||
// Public is OK to read regardless of user settings
|
||||
if storageName == RootPublicStatic {
|
||||
return map[string]filestorage.PathFilter{
|
||||
@@ -268,7 +268,7 @@ func (s *standardStorageService) Run(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getOrgId(user *models.SignedInUser) int64 {
|
||||
func getOrgId(user *user.SignedInUser) int64 {
|
||||
if user == nil {
|
||||
return ac.GlobalOrgID
|
||||
}
|
||||
@@ -276,12 +276,12 @@ func getOrgId(user *models.SignedInUser) int64 {
|
||||
return user.OrgId
|
||||
}
|
||||
|
||||
func (s *standardStorageService) List(ctx context.Context, user *models.SignedInUser, path string) (*StorageListFrame, error) {
|
||||
func (s *standardStorageService) List(ctx context.Context, user *user.SignedInUser, path string) (*StorageListFrame, error) {
|
||||
guardian := s.authService.newGuardian(ctx, user, getFirstSegment(path))
|
||||
return s.tree.ListFolder(ctx, getOrgId(user), path, guardian.getPathFilter(ActionFilesRead))
|
||||
}
|
||||
|
||||
func (s *standardStorageService) Read(ctx context.Context, user *models.SignedInUser, path string) (*filestorage.File, error) {
|
||||
func (s *standardStorageService) Read(ctx context.Context, user *user.SignedInUser, path string) (*filestorage.File, error) {
|
||||
guardian := s.authService.newGuardian(ctx, user, getFirstSegment(path))
|
||||
if !guardian.canView(path) {
|
||||
return nil, ErrAccessDenied
|
||||
@@ -300,7 +300,7 @@ type UploadRequest struct {
|
||||
OverwriteExistingFile bool
|
||||
}
|
||||
|
||||
func (s *standardStorageService) Upload(ctx context.Context, user *models.SignedInUser, req *UploadRequest) error {
|
||||
func (s *standardStorageService) Upload(ctx context.Context, user *user.SignedInUser, req *UploadRequest) error {
|
||||
if err := s.checkFileQuota(ctx, req.Path); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -369,7 +369,7 @@ func (s *standardStorageService) checkFileQuota(ctx context.Context, path string
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *standardStorageService) DeleteFolder(ctx context.Context, user *models.SignedInUser, cmd *DeleteFolderCmd) error {
|
||||
func (s *standardStorageService) DeleteFolder(ctx context.Context, user *user.SignedInUser, cmd *DeleteFolderCmd) error {
|
||||
guardian := s.authService.newGuardian(ctx, user, getFirstSegment(cmd.Path))
|
||||
if !guardian.canDelete(cmd.Path) {
|
||||
return ErrAccessDenied
|
||||
@@ -390,7 +390,7 @@ func (s *standardStorageService) DeleteFolder(ctx context.Context, user *models.
|
||||
return root.Store().DeleteFolder(ctx, storagePath, &filestorage.DeleteFolderOptions{Force: cmd.Force, AccessFilter: guardian.getPathFilter(ActionFilesDelete)})
|
||||
}
|
||||
|
||||
func (s *standardStorageService) CreateFolder(ctx context.Context, user *models.SignedInUser, cmd *CreateFolderCmd) error {
|
||||
func (s *standardStorageService) CreateFolder(ctx context.Context, user *user.SignedInUser, cmd *CreateFolderCmd) error {
|
||||
if err := s.checkFileQuota(ctx, cmd.Path); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -416,7 +416,7 @@ func (s *standardStorageService) CreateFolder(ctx context.Context, user *models.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *standardStorageService) Delete(ctx context.Context, user *models.SignedInUser, path string) error {
|
||||
func (s *standardStorageService) Delete(ctx context.Context, user *user.SignedInUser, path string) error {
|
||||
guardian := s.authService.newGuardian(ctx, user, getFirstSegment(path))
|
||||
if !guardian.canDelete(path) {
|
||||
return ErrAccessDenied
|
||||
@@ -438,7 +438,7 @@ func (s *standardStorageService) Delete(ctx context.Context, user *models.Signed
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *standardStorageService) write(ctx context.Context, user *models.SignedInUser, req *WriteValueRequest) (*WriteValueResponse, error) {
|
||||
func (s *standardStorageService) write(ctx context.Context, user *user.SignedInUser, req *WriteValueRequest) (*WriteValueResponse, error) {
|
||||
guardian := s.authService.newGuardian(ctx, user, getFirstSegment(req.Path))
|
||||
if !guardian.canWrite(req.Path) {
|
||||
return nil, ErrAccessDenied
|
||||
@@ -481,7 +481,7 @@ type optionInfo struct {
|
||||
Workflows []workflowInfo `json:"workflows"`
|
||||
}
|
||||
|
||||
func (s *standardStorageService) getWorkflowOptions(ctx context.Context, user *models.SignedInUser, path string) (optionInfo, error) {
|
||||
func (s *standardStorageService) getWorkflowOptions(ctx context.Context, user *user.SignedInUser, path string) (optionInfo, error) {
|
||||
options := optionInfo{
|
||||
Path: path,
|
||||
Workflows: make([]workflowInfo, 0),
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/experimental"
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/testdatasource"
|
||||
"github.com/stretchr/testify/mock"
|
||||
@@ -27,15 +27,15 @@ var (
|
||||
htmlBytes, _ = ioutil.ReadFile("testdata/page.html")
|
||||
jpgBytes, _ = ioutil.ReadFile("testdata/image.jpg")
|
||||
svgBytes, _ = ioutil.ReadFile("testdata/image.svg")
|
||||
dummyUser = &models.SignedInUser{OrgId: 1}
|
||||
allowAllAuthService = newStaticStorageAuthService(func(ctx context.Context, user *models.SignedInUser, storageName string) map[string]filestorage.PathFilter {
|
||||
dummyUser = &user.SignedInUser{OrgId: 1}
|
||||
allowAllAuthService = newStaticStorageAuthService(func(ctx context.Context, user *user.SignedInUser, storageName string) map[string]filestorage.PathFilter {
|
||||
return map[string]filestorage.PathFilter{
|
||||
ActionFilesDelete: allowAllPathFilter,
|
||||
ActionFilesWrite: allowAllPathFilter,
|
||||
ActionFilesRead: allowAllPathFilter,
|
||||
}
|
||||
})
|
||||
denyAllAuthService = newStaticStorageAuthService(func(ctx context.Context, user *models.SignedInUser, storageName string) map[string]filestorage.PathFilter {
|
||||
denyAllAuthService = newStaticStorageAuthService(func(ctx context.Context, user *user.SignedInUser, storageName string) map[string]filestorage.PathFilter {
|
||||
return map[string]filestorage.PathFilter{
|
||||
ActionFilesDelete: denyAllPathFilter,
|
||||
ActionFilesWrite: denyAllPathFilter,
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type createPathFilterByAction func(ctx context.Context, user *models.SignedInUser, storageName string) map[string]filestorage.PathFilter
|
||||
type createPathFilterByAction func(ctx context.Context, user *user.SignedInUser, storageName string) map[string]filestorage.PathFilter
|
||||
|
||||
func newStaticStorageAuthService(createPathFilterByAction createPathFilterByAction) storageAuthService {
|
||||
return &staticStorageAuth{
|
||||
@@ -24,7 +24,7 @@ type staticStorageAuth struct {
|
||||
createPathFilterByAction createPathFilterByAction
|
||||
}
|
||||
|
||||
func (a *staticStorageAuth) newGuardian(ctx context.Context, user *models.SignedInUser, storageName string) fileGuardian {
|
||||
func (a *staticStorageAuth) newGuardian(ctx context.Context, user *user.SignedInUser, storageName string) fileGuardian {
|
||||
pathFilter := a.createPathFilterByAction(ctx, user, storageName)
|
||||
|
||||
if pathFilter == nil {
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"gocloud.dev/blob"
|
||||
)
|
||||
@@ -339,15 +339,15 @@ func (s *rootStorageGit) Write(ctx context.Context, cmd *WriteValueRequest) (*Wr
|
||||
if msg == "" {
|
||||
msg = "changes from grafana ui"
|
||||
}
|
||||
user := cmd.User
|
||||
if user == nil {
|
||||
user = &models.SignedInUser{}
|
||||
usr := cmd.User
|
||||
if usr == nil {
|
||||
usr = &user.SignedInUser{}
|
||||
}
|
||||
|
||||
hash, err := w.Commit(msg, &git.CommitOptions{
|
||||
Author: &object.Signature{
|
||||
Name: firstRealString(user.Name, user.Login, user.Email, "?"),
|
||||
Email: firstRealString(user.Email, user.Login, user.Name, "?"),
|
||||
Name: firstRealString(usr.Name, usr.Login, usr.Email, "?"),
|
||||
Email: firstRealString(usr.Email, usr.Login, usr.Name, "?"),
|
||||
When: time.Now(),
|
||||
},
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
type WriteValueWorkflow = string
|
||||
@@ -18,7 +18,7 @@ var (
|
||||
)
|
||||
|
||||
type WriteValueRequest struct {
|
||||
User *models.SignedInUser
|
||||
User *user.SignedInUser
|
||||
Path string // added from URL
|
||||
EntityType EntityType `json:"kind,omitempty"` // for now only dashboard
|
||||
Body json.RawMessage `json:"body,omitempty"`
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/filestorage"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
issvg "github.com/grafana/grafana/pkg/services/store/go-is-svg"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -50,7 +50,7 @@ func fail(reason string) validationResult {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *standardStorageService) detectMimeType(ctx context.Context, user *models.SignedInUser, uploadRequest *UploadRequest) string {
|
||||
func (s *standardStorageService) detectMimeType(ctx context.Context, user *user.SignedInUser, uploadRequest *UploadRequest) string {
|
||||
if strings.HasSuffix(uploadRequest.Path, ".svg") {
|
||||
if issvg.IsSVG(uploadRequest.Contents) {
|
||||
return "image/svg+xml"
|
||||
@@ -60,7 +60,7 @@ func (s *standardStorageService) detectMimeType(ctx context.Context, user *model
|
||||
return http.DetectContentType(uploadRequest.Contents)
|
||||
}
|
||||
|
||||
func (s *standardStorageService) validateImage(ctx context.Context, user *models.SignedInUser, uploadRequest *UploadRequest) validationResult {
|
||||
func (s *standardStorageService) validateImage(ctx context.Context, user *user.SignedInUser, uploadRequest *UploadRequest) validationResult {
|
||||
ext := filepath.Ext(uploadRequest.Path)
|
||||
if !allowedImageExtensions[ext] {
|
||||
return fail(fmt.Sprintf("unsupported extension: %s", ext))
|
||||
@@ -74,7 +74,7 @@ func (s *standardStorageService) validateImage(ctx context.Context, user *models
|
||||
return success()
|
||||
}
|
||||
|
||||
func (s *standardStorageService) validateUploadRequest(ctx context.Context, user *models.SignedInUser, req *UploadRequest, storagePath string) validationResult {
|
||||
func (s *standardStorageService) validateUploadRequest(ctx context.Context, user *user.SignedInUser, req *UploadRequest, storagePath string) validationResult {
|
||||
// TODO: validateSize
|
||||
// TODO: validateProperties
|
||||
|
||||
|
||||
Reference in New Issue
Block a user