From 79b2974215cf6e61d82ec0ba6c8e02de78976c6c Mon Sep 17 00:00:00 2001 From: Jo Date: Wed, 11 Oct 2023 15:57:03 +0200 Subject: [PATCH] Teams: Allow Services to register deletes (#76325) * add team RegisterDelete * fix-docs --- docs/sources/developers/http_api/team_sync.md | 4 ++-- pkg/services/team/team.go | 1 + pkg/services/team/teamimpl/store.go | 13 +++++++++++-- pkg/services/team/teamimpl/team.go | 6 +++++- pkg/services/team/teamtest/team.go | 3 +++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs/sources/developers/http_api/team_sync.md b/docs/sources/developers/http_api/team_sync.md index 30a787294dc..eb8622d0054 100644 --- a/docs/sources/developers/http_api/team_sync.md +++ b/docs/sources/developers/http_api/team_sync.md @@ -113,7 +113,7 @@ Status Codes: ## Remove External Group -`DELETE /api/teams/:teamId/groups/:groupId` +`DELETE /api/teams/:teamId/groups?groupId=external-group-id` **Required permissions** @@ -126,7 +126,7 @@ See note in the [introduction]({{< ref "#external-group-synchronization-api" >}} **Example Request**: ```http -DELETE /api/teams/1/groups/cn=editors,ou=groups,dc=grafana,dc=org HTTP/1.1 +DELETE /api/teams/1/groups?groupId=cn%3Deditors%2Cou%3Dgroups%2Cdc%3Dgrafana%2Cdc%3Dorg HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Basic YWRtaW46YWRtaW4= diff --git a/pkg/services/team/team.go b/pkg/services/team/team.go index 594b224f60b..22e4c77db59 100644 --- a/pkg/services/team/team.go +++ b/pkg/services/team/team.go @@ -20,4 +20,5 @@ type Service interface { RemoveUsersMemberships(tx context.Context, userID int64) error GetUserTeamMemberships(ctx context.Context, orgID, userID int64, external bool) ([]*TeamMemberDTO, error) GetTeamMembers(ctx context.Context, query *GetTeamMembersQuery) ([]*TeamMemberDTO, error) + RegisterDelete(query string) } diff --git a/pkg/services/team/teamimpl/store.go b/pkg/services/team/teamimpl/store.go index f2f830a5565..c600ecfb443 100644 --- a/pkg/services/team/teamimpl/store.go +++ b/pkg/services/team/teamimpl/store.go @@ -30,11 +30,13 @@ type store interface { RemoveMember(ctx context.Context, cmd *team.RemoveTeamMemberCommand) error GetMemberships(ctx context.Context, orgID, userID int64, external bool) ([]*team.TeamMemberDTO, error) GetMembers(ctx context.Context, query *team.GetTeamMembersQuery) ([]*team.TeamMemberDTO, error) + RegisterDelete(query string) } type xormStore struct { - db db.DB - cfg *setting.Cfg + db db.DB + cfg *setting.Cfg + deletes []string } func getFilteredUsers(signedInUser identity.Requester, hiddenUsers map[string]struct{}) []string { @@ -142,6 +144,8 @@ func (ss *xormStore) Delete(ctx context.Context, cmd *team.DeleteTeamCommand) er "DELETE FROM team_role WHERE org_id=? and team_id = ?", } + deletes = append(deletes, ss.deletes...) + for _, sql := range deletes { _, err := sess.Exec(sql, cmd.OrgID, cmd.ID) if err != nil { @@ -567,3 +571,8 @@ func (ss *xormStore) getTeamMembers(ctx context.Context, query *team.GetTeamMemb } return queryResult, nil } + +// RegisterDelete registers a delete query to be executed when the transaction is committed +func (ss *xormStore) RegisterDelete(query string) { + ss.deletes = append(ss.deletes, query) +} diff --git a/pkg/services/team/teamimpl/team.go b/pkg/services/team/teamimpl/team.go index 5bbef67adb2..5895447bc4b 100644 --- a/pkg/services/team/teamimpl/team.go +++ b/pkg/services/team/teamimpl/team.go @@ -14,7 +14,7 @@ type Service struct { } func ProvideService(db db.DB, cfg *setting.Cfg) team.Service { - return &Service{store: &xormStore{db: db, cfg: cfg}} + return &Service{store: &xormStore{db: db, cfg: cfg, deletes: []string{}}} } func (s *Service) CreateTeam(name, email string, orgID int64) (team.Team, error) { @@ -68,3 +68,7 @@ func (s *Service) GetUserTeamMemberships(ctx context.Context, orgID, userID int6 func (s *Service) GetTeamMembers(ctx context.Context, query *team.GetTeamMembersQuery) ([]*team.TeamMemberDTO, error) { return s.store.GetMembers(ctx, query) } + +func (s *Service) RegisterDelete(query string) { + s.store.RegisterDelete(query) +} diff --git a/pkg/services/team/teamtest/team.go b/pkg/services/team/teamtest/team.go index e0a295d859c..71ae3073036 100644 --- a/pkg/services/team/teamtest/team.go +++ b/pkg/services/team/teamtest/team.go @@ -72,3 +72,6 @@ func (s *FakeService) GetUserTeamMemberships(ctx context.Context, orgID, userID func (s *FakeService) GetTeamMembers(ctx context.Context, query *team.GetTeamMembersQuery) ([]*team.TeamMemberDTO, error) { return s.ExpectedMembers, s.ExpectedError } + +func (s *FakeService) RegisterDelete(query string) { +}