Teams: Allow Services to register deletes (#76325)

* add team RegisterDelete

* fix-docs
This commit is contained in:
Jo 2023-10-11 15:57:03 +02:00 committed by GitHub
parent 72a1de855c
commit 79b2974215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 5 deletions

View File

@ -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=

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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) {
}