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:
idafurjes
2022-08-10 11:56:48 +02:00
committed by GitHub
parent 46004037e2
commit 6afad51761
278 changed files with 1758 additions and 1543 deletions

View File

@@ -5,6 +5,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/user"
)
type AlertStateType string
@@ -159,7 +160,7 @@ type GetAlertsQuery struct {
PanelId int64
Limit int64
Query string
User *SignedInUser
User *user.SignedInUser
Result []*AlertListItemDTO
}

View File

@@ -5,6 +5,8 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
"github.com/prometheus/client_golang/prometheus"
@@ -12,7 +14,7 @@ import (
type ReqContext struct {
*web.Context
*SignedInUser
*user.SignedInUser
UserToken *UserToken
IsSignedIn bool
@@ -77,11 +79,11 @@ func (ctx *ReqContext) JsonApiErr(status int, message string, err error) {
ctx.JSON(status, resp)
}
func (ctx *ReqContext) HasUserRole(role RoleType) bool {
func (ctx *ReqContext) HasUserRole(role org.RoleType) bool {
return ctx.OrgRole.Includes(role)
}
func (ctx *ReqContext) HasHelpFlag(flag HelpFlags1) bool {
func (ctx *ReqContext) HasHelpFlag(flag user.HelpFlags1) bool {
return ctx.HelpFlags1.HasFlag(flag)
}

View File

@@ -3,6 +3,8 @@ package models
import (
"errors"
"time"
"github.com/grafana/grafana/pkg/services/org"
)
type PermissionType int
@@ -39,9 +41,9 @@ type DashboardACL struct {
OrgID int64 `xorm:"org_id"`
DashboardID int64 `xorm:"dashboard_id"`
UserID int64 `xorm:"user_id"`
TeamID int64 `xorm:"team_id"`
Role *RoleType // pointer to be nullable
UserID int64 `xorm:"user_id"`
TeamID int64 `xorm:"team_id"`
Role *org.RoleType // pointer to be nullable
Permission PermissionType
Created time.Time
@@ -64,7 +66,7 @@ type DashboardACLInfoDTO struct {
TeamEmail string `json:"teamEmail"`
TeamAvatarUrl string `json:"teamAvatarUrl"`
Team string `json:"team"`
Role *RoleType `json:"role,omitempty"`
Role *org.RoleType `json:"role,omitempty"`
Permission PermissionType `json:"permission"`
PermissionName string `json:"permissionName"`
Uid string `json:"uid"`

View File

@@ -3,6 +3,8 @@ package models
import (
"strings"
"time"
"github.com/grafana/grafana/pkg/services/user"
)
type Folder struct {
@@ -91,11 +93,11 @@ type UpdateFolderCommand struct {
//
type HasEditPermissionInFoldersQuery struct {
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
Result bool
}
type HasAdminPermissionInDashboardsOrFoldersQuery struct {
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
Result bool
}

View File

@@ -1,16 +1,8 @@
package models
type HelpFlags1 uint64
const (
HelpFlagGettingStartedPanelDismissed HelpFlags1 = 1 << iota
HelpFlagDashboardHelp1
)
func (f HelpFlags1) HasFlag(flag HelpFlags1) bool { return f&flag != 0 }
func (f *HelpFlags1) AddFlag(flag HelpFlags1) { *f |= flag }
import "github.com/grafana/grafana/pkg/services/user"
type SetUserHelpFlagCommand struct {
HelpFlags1 HelpFlags1
HelpFlags1 user.HelpFlags1
UserId int64
}

View File

@@ -6,6 +6,7 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/services/user"
)
// ChannelPublisher writes data into a channel. Note that permissions are not checked.
@@ -52,10 +53,10 @@ type PublishReply struct {
// ChannelHandler defines the core channel behavior
type ChannelHandler interface {
// OnSubscribe is called when a client wants to subscribe to a channel
OnSubscribe(ctx context.Context, user *SignedInUser, e SubscribeEvent) (SubscribeReply, backend.SubscribeStreamStatus, error)
OnSubscribe(ctx context.Context, user *user.SignedInUser, e SubscribeEvent) (SubscribeReply, backend.SubscribeStreamStatus, error)
// OnPublish is called when a client writes a message to the channel websocket.
OnPublish(ctx context.Context, user *SignedInUser, e PublishEvent) (PublishReply, backend.PublishStreamStatus, error)
OnPublish(ctx context.Context, user *user.SignedInUser, e PublishEvent) (PublishReply, backend.PublishStreamStatus, error)
}
// ChannelHandlerFactory should be implemented by all core features.
@@ -71,10 +72,10 @@ type DashboardActivityChannel interface {
// gitops workflow that knows if the value was saved to the local database or not
// in many cases all direct save requests will fail, but the request should be forwarded
// to any gitops observers
DashboardSaved(orgID int64, user *UserDisplayDTO, message string, dashboard *Dashboard, err error) error
DashboardSaved(orgID int64, user *user.UserDisplayDTO, message string, dashboard *Dashboard, err error) error
// Called when a dashboard is deleted
DashboardDeleted(orgID int64, user *UserDisplayDTO, uid string) error
DashboardDeleted(orgID int64, user *user.UserDisplayDTO, uid string) error
// Experimental! Indicate is GitOps is active. This really means
// someone is subscribed to the `grafana/dashboards/gitops` channel

View File

@@ -3,6 +3,8 @@ package models
import (
"errors"
"time"
"github.com/grafana/grafana/pkg/services/org"
)
// Typed errors
@@ -84,7 +86,7 @@ type OrgDetailsDTO struct {
}
type UserOrgDTO struct {
OrgId int64 `json:"orgId"`
Name string `json:"name"`
Role RoleType `json:"role"`
OrgId int64 `json:"orgId"`
Name string `json:"name"`
Role org.RoleType `json:"role"`
}

View File

@@ -2,9 +2,10 @@ package models
import (
"errors"
"fmt"
"strings"
"time"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
)
// Typed errors
@@ -14,74 +15,11 @@ var (
ErrOrgUserAlreadyAdded = errors.New("user is already added to organization")
)
// swagger:enum RoleType
type RoleType string
const (
ROLE_VIEWER RoleType = "Viewer"
ROLE_EDITOR RoleType = "Editor"
ROLE_ADMIN RoleType = "Admin"
)
func (r RoleType) IsValid() bool {
return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR
}
func (r RoleType) Includes(other RoleType) bool {
if r == ROLE_ADMIN {
return true
}
if r == ROLE_EDITOR {
return other != ROLE_ADMIN
}
return r == other
}
func (r RoleType) Children() []RoleType {
switch r {
case ROLE_ADMIN:
return []RoleType{ROLE_EDITOR, ROLE_VIEWER}
case ROLE_EDITOR:
return []RoleType{ROLE_VIEWER}
default:
return nil
}
}
func (r RoleType) Parents() []RoleType {
switch r {
case ROLE_EDITOR:
return []RoleType{ROLE_ADMIN}
case ROLE_VIEWER:
return []RoleType{ROLE_EDITOR, ROLE_ADMIN}
default:
return nil
}
}
func (r *RoleType) UnmarshalText(data []byte) error {
// make sure "viewer" and "Viewer" are both correct
str := strings.Title(string(data))
*r = RoleType(str)
if !r.IsValid() {
if (*r) != "" {
return fmt.Errorf("invalid role value: %s", *r)
}
*r = ROLE_VIEWER
}
return nil
}
type OrgUser struct {
Id int64
OrgId int64
UserId int64
Role RoleType
Role org.RoleType
Created time.Time
Updated time.Time
}
@@ -97,8 +35,8 @@ type RemoveOrgUserCommand struct {
}
type AddOrgUserCommand struct {
LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
Role RoleType `json:"role" binding:"Required"`
LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
Role org.RoleType `json:"role" binding:"Required"`
OrgId int64 `json:"-"`
UserId int64 `json:"-"`
@@ -108,7 +46,7 @@ type AddOrgUserCommand struct {
}
type UpdateOrgUserCommand struct {
Role RoleType `json:"role" binding:"Required"`
Role org.RoleType `json:"role" binding:"Required"`
OrgId int64 `json:"-"`
UserId int64 `json:"-"`
@@ -125,7 +63,7 @@ type GetOrgUsersQuery struct {
// Flag used to allow oss edition to query users without access control
DontEnforceAccessControl bool
User *SignedInUser
User *user.SignedInUser
Result []*OrgUserDTO
}
@@ -135,7 +73,7 @@ type SearchOrgUsersQuery struct {
Page int
Limit int
User *SignedInUser
User *user.SignedInUser
Result SearchOrgUsersQueryResult
}

View File

@@ -4,6 +4,7 @@ import (
"strings"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/grafana/grafana/pkg/services/user"
)
type SortOption struct {
@@ -22,7 +23,7 @@ type SortOptionFilter interface {
type FindPersistedDashboardsQuery struct {
Title string
OrgId int64
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
IsStarred bool
DashboardIds []int64
DashboardUIDs []string

View File

@@ -3,6 +3,8 @@ package models
import (
"errors"
"time"
"github.com/grafana/grafana/pkg/services/user"
)
// Typed errors
@@ -52,7 +54,7 @@ type DeleteTeamCommand struct {
type GetTeamByIdQuery struct {
OrgId int64
Id int64
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
HiddenUsers map[string]struct{}
Result *TeamDTO
UserIdFilter int64
@@ -65,7 +67,7 @@ type GetTeamsByUserQuery struct {
OrgId int64
UserId int64 `json:"userId"`
Result []*TeamDTO `json:"teams"`
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
}
type SearchTeamsQuery struct {
@@ -75,7 +77,7 @@ type SearchTeamsQuery struct {
Page int
OrgId int64
UserIdFilter int64
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
HiddenUsers map[string]struct{}
Result SearchTeamQueryResult
@@ -100,6 +102,6 @@ type SearchTeamQueryResult struct {
}
type IsAdminOfTeamsQuery struct {
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
Result bool
}

View File

@@ -3,6 +3,8 @@ package models
import (
"errors"
"time"
"github.com/grafana/grafana/pkg/services/user"
)
// Typed errors
@@ -55,7 +57,7 @@ type GetTeamMembersQuery struct {
TeamId int64
UserId int64
External bool
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
Result []*TeamMemberDTO
}

View File

@@ -3,6 +3,8 @@ package models
import (
"errors"
"time"
"github.com/grafana/grafana/pkg/services/org"
)
// Typed errors
@@ -27,7 +29,7 @@ type TempUser struct {
Version int
Email string
Name string
Role RoleType
Role org.RoleType
InvitedByUserId int64
Status TempUserStatus
@@ -50,7 +52,7 @@ type CreateTempUserCommand struct {
InvitedByUserId int64
Status TempUserStatus
Code string
Role RoleType
Role org.RoleType
RemoteAddr string
Result *TempUser
@@ -90,7 +92,7 @@ type TempUserDTO struct {
OrgId int64 `json:"orgId"`
Name string `json:"name"`
Email string `json:"email"`
Role RoleType `json:"role"`
Role org.RoleType `json:"role"`
InvitedByLogin string `json:"invitedByLogin"`
InvitedByEmail string `json:"invitedByEmail"`
InvitedByName string `json:"invitedByName"`

View File

@@ -70,7 +70,7 @@ type GetSignedInUserQuery struct {
Login string
Email string
OrgId int64
Result *SignedInUser
Result *user.SignedInUser
}
type GetUserProfileQuery struct {
@@ -79,7 +79,7 @@ type GetUserProfileQuery struct {
}
type SearchUsersQuery struct {
SignedInUser *SignedInUser
SignedInUser *user.SignedInUser
OrgId int64
Query string
Page int
@@ -104,69 +104,10 @@ type GetUserOrgListQuery struct {
Result []*UserOrgDTO
}
// ------------------------
// DTO & Projections
type SignedInUser struct {
UserId int64
OrgId int64
OrgName string
OrgRole RoleType
ExternalAuthModule string
ExternalAuthId string
Login string
Name string
Email string
ApiKeyId int64
OrgCount int
IsGrafanaAdmin bool
IsAnonymous bool
IsDisabled bool
HelpFlags1 HelpFlags1
LastSeenAt time.Time
Teams []int64
// Permissions grouped by orgID and actions
Permissions map[int64]map[string][]string `json:"-"`
}
func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {
return u.UserId > 0 && time.Since(u.LastSeenAt) > time.Minute*5
}
func (u *SignedInUser) NameOrFallback() string {
if u.Name != "" {
return u.Name
}
if u.Login != "" {
return u.Login
}
return u.Email
}
func (u *SignedInUser) ToUserDisplayDTO() *UserDisplayDTO {
return &UserDisplayDTO{
Id: u.UserId,
Login: u.Login,
Name: u.Name,
}
}
type UpdateUserLastSeenAtCommand struct {
UserId int64
}
func (u *SignedInUser) HasRole(role RoleType) bool {
if u.IsGrafanaAdmin {
return true
}
return u.OrgRole.Includes(role)
}
func (u *SignedInUser) IsRealUser() bool {
return u.UserId != 0
}
type UserProfileDTO struct {
Id int64 `json:"id"`
Email string `json:"email"`
@@ -198,13 +139,6 @@ type UserSearchHitDTO struct {
AuthModule AuthModuleConversion `json:"-"`
}
type UserDisplayDTO struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Login string `json:"login,omitempty"`
AvatarUrl string `json:"avatarUrl"`
}
type UserIdDTO struct {
Id int64 `json:"id"`
Message string `json:"message"`

View File

@@ -3,6 +3,7 @@ package models
import (
"time"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
@@ -31,7 +32,7 @@ type ExternalUserInfo struct {
Login string
Name string
Groups []string
OrgRoles map[int64]RoleType
OrgRoles map[int64]org.RoleType
IsGrafanaAdmin *bool // This is a pointer to know if we should sync this or not (nil = ignore sync)
IsDisabled bool
}