2023-01-11 07:20:09 -06:00
|
|
|
package team
|
2017-03-27 07:36:28 -05:00
|
|
|
|
2017-04-08 17:55:07 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
2022-08-10 04:56:48 -05:00
|
|
|
|
2023-08-02 03:43:56 -05:00
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2023-05-24 11:13:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/kinds/team"
|
2023-08-02 03:43:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2023-01-26 07:46:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2017-04-08 17:55:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Typed errors
|
|
|
|
var (
|
2020-11-05 06:07:06 -06:00
|
|
|
ErrTeamNotFound = errors.New("team not found")
|
|
|
|
ErrTeamNameTaken = errors.New("team name is taken")
|
|
|
|
ErrTeamMemberNotFound = errors.New("team member not found")
|
|
|
|
ErrLastTeamAdmin = errors.New("not allowed to remove last admin")
|
|
|
|
ErrNotAllowedToUpdateTeam = errors.New("user not allowed to update team")
|
|
|
|
ErrNotAllowedToUpdateTeamInDifferentOrg = errors.New("user not allowed to update team in another org")
|
2023-01-13 02:43:38 -06:00
|
|
|
|
|
|
|
ErrTeamMemberAlreadyAdded = errors.New("user is already added to this team")
|
2017-04-08 17:55:07 -05:00
|
|
|
)
|
2017-03-27 07:36:28 -05:00
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
// Team model
|
|
|
|
type Team struct {
|
2023-01-11 07:20:09 -06:00
|
|
|
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
2023-05-09 12:03:28 -05:00
|
|
|
UID string `json:"uid" xorm:"uid"`
|
2023-01-11 07:20:09 -06:00
|
|
|
OrgID int64 `json:"orgId" xorm:"org_id"`
|
2017-04-09 18:24:16 -05:00
|
|
|
Name string `json:"name"`
|
2017-12-20 14:20:12 -06:00
|
|
|
Email string `json:"email"`
|
2017-03-27 07:36:28 -05:00
|
|
|
|
2017-04-09 18:24:16 -05:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Updated time.Time `json:"updated"`
|
2017-03-27 07:36:28 -05:00
|
|
|
}
|
2017-04-08 17:55:07 -05:00
|
|
|
|
2023-05-24 11:13:44 -05:00
|
|
|
func (t *Team) ToResource() team.K8sResource {
|
|
|
|
r := team.NewK8sResource(t.UID, &team.Spec{
|
|
|
|
Name: t.Name,
|
|
|
|
})
|
|
|
|
r.Metadata.CreationTimestamp = v1.NewTime(t.Created)
|
|
|
|
r.Metadata.SetUpdatedTimestamp(&t.Updated)
|
|
|
|
if t.Email != "" {
|
|
|
|
r.Spec.Email = &t.Email
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:55:07 -05:00
|
|
|
// ---------------------
|
|
|
|
// COMMANDS
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
type CreateTeamCommand struct {
|
2017-04-08 17:55:07 -05:00
|
|
|
Name string `json:"name" binding:"Required"`
|
2017-12-20 14:20:12 -06:00
|
|
|
Email string `json:"email"`
|
2023-01-11 07:20:09 -06:00
|
|
|
OrgID int64 `json:"-"`
|
2017-04-08 17:55:07 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
type UpdateTeamCommand struct {
|
2023-01-11 07:20:09 -06:00
|
|
|
ID int64
|
2017-12-20 14:20:12 -06:00
|
|
|
Name string
|
|
|
|
Email string
|
2023-01-11 07:20:09 -06:00
|
|
|
OrgID int64 `json:"-"`
|
2017-04-18 08:01:05 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
type DeleteTeamCommand struct {
|
2023-01-11 07:20:09 -06:00
|
|
|
OrgID int64
|
|
|
|
ID int64
|
2017-04-08 17:55:07 -05:00
|
|
|
}
|
|
|
|
|
2023-01-11 07:20:09 -06:00
|
|
|
type GetTeamByIDQuery struct {
|
|
|
|
OrgID int64
|
|
|
|
ID int64
|
2023-08-02 03:43:56 -05:00
|
|
|
SignedInUser identity.Requester
|
2020-11-24 05:10:32 -06:00
|
|
|
HiddenUsers map[string]struct{}
|
2017-04-08 17:55:07 -05:00
|
|
|
}
|
|
|
|
|
2022-02-09 06:44:38 -06:00
|
|
|
// FilterIgnoreUser is used in a get / search teams query when the caller does not want to filter teams by user ID / membership
|
|
|
|
const FilterIgnoreUser int64 = 0
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
type GetTeamsByUserQuery struct {
|
2023-01-11 07:20:09 -06:00
|
|
|
OrgID int64
|
|
|
|
UserID int64 `json:"userId"`
|
2023-08-02 03:43:56 -05:00
|
|
|
SignedInUser identity.Requester
|
2017-05-22 03:33:17 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
type SearchTeamsQuery struct {
|
2019-03-11 08:40:57 -05:00
|
|
|
Query string
|
|
|
|
Name string
|
|
|
|
Limit int
|
|
|
|
Page int
|
2023-01-11 07:20:09 -06:00
|
|
|
OrgID int64 `xorm:"org_id"`
|
2023-08-02 03:43:56 -05:00
|
|
|
SignedInUser identity.Requester
|
2020-11-24 05:10:32 -06:00
|
|
|
HiddenUsers map[string]struct{}
|
2017-04-09 18:24:16 -05:00
|
|
|
}
|
|
|
|
|
2018-07-11 13:23:07 -05:00
|
|
|
type TeamDTO struct {
|
2023-01-26 07:46:30 -06:00
|
|
|
ID int64 `json:"id" xorm:"id"`
|
2023-05-09 12:03:28 -05:00
|
|
|
UID string `json:"uid" xorm:"uid"`
|
2023-01-26 07:46:30 -06:00
|
|
|
OrgID int64 `json:"orgId" xorm:"org_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
AvatarURL string `json:"avatarUrl"`
|
|
|
|
MemberCount int64 `json:"memberCount"`
|
|
|
|
Permission dashboards.PermissionType `json:"permission"`
|
|
|
|
AccessControl map[string]bool `json:"accessControl"`
|
2017-12-14 10:22:45 -06:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
type SearchTeamQueryResult struct {
|
2018-07-11 13:23:07 -05:00
|
|
|
TotalCount int64 `json:"totalCount"`
|
|
|
|
Teams []*TeamDTO `json:"teams"`
|
|
|
|
Page int `json:"page"`
|
|
|
|
PerPage int `json:"perPage"`
|
2017-04-08 17:55:07 -05:00
|
|
|
}
|
2019-08-12 13:03:48 -05:00
|
|
|
|
2023-01-13 02:43:38 -06:00
|
|
|
// TeamMember model
|
|
|
|
type TeamMember struct {
|
|
|
|
ID int64 `xorm:"pk autoincr 'id'"`
|
|
|
|
OrgID int64 `xorm:"org_id"`
|
|
|
|
TeamID int64 `xorm:"team_id"`
|
|
|
|
UserID int64 `xorm:"user_id"`
|
|
|
|
External bool // Signals that the membership has been created by an external systems, such as LDAP
|
2023-01-26 07:46:30 -06:00
|
|
|
Permission dashboards.PermissionType
|
2023-01-13 02:43:38 -06:00
|
|
|
|
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// COMMANDS
|
|
|
|
|
|
|
|
type AddTeamMemberCommand struct {
|
2023-01-26 07:46:30 -06:00
|
|
|
UserID int64 `json:"userId" binding:"Required"`
|
|
|
|
OrgID int64 `json:"-"`
|
|
|
|
TeamID int64 `json:"-"`
|
|
|
|
External bool `json:"-"`
|
|
|
|
Permission dashboards.PermissionType `json:"-"`
|
2023-01-13 02:43:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateTeamMemberCommand struct {
|
2023-01-26 07:46:30 -06:00
|
|
|
UserID int64 `json:"-"`
|
|
|
|
OrgID int64 `json:"-"`
|
|
|
|
TeamID int64 `json:"-"`
|
|
|
|
Permission dashboards.PermissionType `json:"permission"`
|
2023-01-13 02:43:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type RemoveTeamMemberCommand struct {
|
|
|
|
OrgID int64 `json:"-"`
|
|
|
|
UserID int64
|
|
|
|
TeamID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
// QUERIES
|
|
|
|
|
|
|
|
type GetTeamMembersQuery struct {
|
|
|
|
OrgID int64
|
|
|
|
TeamID int64
|
2023-05-09 12:03:28 -05:00
|
|
|
TeamUID string
|
2023-01-13 02:43:38 -06:00
|
|
|
UserID int64
|
|
|
|
External bool
|
2023-08-31 02:46:55 -05:00
|
|
|
SignedInUser identity.Requester
|
2023-01-13 02:43:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
// Projections and DTOs
|
|
|
|
|
|
|
|
type TeamMemberDTO struct {
|
2023-01-26 07:46:30 -06:00
|
|
|
OrgID int64 `json:"orgId" xorm:"org_id"`
|
|
|
|
TeamID int64 `json:"teamId" xorm:"team_id"`
|
2023-05-09 12:03:28 -05:00
|
|
|
TeamUID string `json:"teamUID" xorm:"uid"`
|
2023-01-26 07:46:30 -06:00
|
|
|
UserID int64 `json:"userId" xorm:"user_id"`
|
|
|
|
External bool `json:"-"`
|
|
|
|
AuthModule string `json:"auth_module"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
AvatarURL string `json:"avatarUrl" xorm:"avatar_url"`
|
|
|
|
Labels []string `json:"labels"`
|
|
|
|
Permission dashboards.PermissionType `json:"permission"`
|
2023-01-13 02:43:38 -06:00
|
|
|
}
|