Skip to content

Commit e358d06

Browse files
authored
feat(deleter): user delete checks for blockers before deleting anything (#2027)
1 parent e1765e7 commit e358d06

15 files changed

Lines changed: 881 additions & 37 deletions

File tree

‎core/deleter/deleter.go‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package deleter
22

3-
import "strings"
3+
import (
4+
"fmt"
5+
"slices"
6+
"strings"
7+
8+
"github.com/raystack/frontier/core/organization"
9+
)
410

511
// Reasons a delete can be blocked. The API error carries
612
// them as violation types, so a client can tell the reasons apart
@@ -10,6 +16,7 @@ const (
1016
BlockerUnpaidInvoice = "UNPAID_INVOICE"
1117
BlockerNegativeTokenBalance = "NEGATIVE_TOKEN_BALANCE"
1218
BlockerResourcesExist = "RESOURCES_EXIST"
19+
BlockerSoleOwnerWithMembers = "SOLE_OWNER_WITH_MEMBERS"
1320
)
1421

1522
// Kinds of entity a Blocker's Subject id refers to.
@@ -18,9 +25,10 @@ const (
1825
SubjectInvoice = "billing_invoice"
1926
SubjectBillingAccount = "billing_account"
2027
SubjectResourceNamespace = "resource_namespace"
28+
SubjectOrganization = "organization"
2129
)
2230

23-
// Blocker is one reason a project or an organization cannot be deleted right now.
31+
// Blocker is one reason a project, an organization or a user cannot be deleted right now.
2432
type Blocker struct {
2533
// Type is one of the Blocker* constants.
2634
Type string
@@ -33,16 +41,61 @@ type Blocker struct {
3341
// Count is the number of blocking resources, for BlockerResourcesExist.
3442
Count int
3543
// Message says what blocks the delete and how to clear it. Empty for
36-
// BlockerResourcesExist.
44+
// BlockerResourcesExist, except in a refused user delete.
3745
Message string
3846
}
3947

48+
type UserDeleteCheck struct {
49+
Blockers []Blocker
50+
OrganizationsToDelete []OrganizationToDelete
51+
OrganizationsToLeave []OrganizationToLeave
52+
PATCount int
53+
}
54+
55+
type OrganizationToDelete struct {
56+
Organization organization.Organization
57+
Blockers []Blocker
58+
}
59+
60+
type OrganizationToLeave struct {
61+
Organization organization.Organization
62+
RoleTitle string
63+
Blockers []Blocker
64+
}
65+
66+
func (c UserDeleteCheck) CanDelete() bool {
67+
return len(c.allBlockers()) == 0
68+
}
69+
70+
func (c UserDeleteCheck) allBlockers() []Blocker {
71+
blockers := slices.Clone(c.Blockers)
72+
for _, o := range c.OrganizationsToDelete {
73+
for _, b := range o.Blockers {
74+
blockers = append(blockers, labelWithOrg(o.Organization.Name, b))
75+
}
76+
}
77+
for _, o := range c.OrganizationsToLeave {
78+
blockers = append(blockers, o.Blockers...)
79+
}
80+
return blockers
81+
}
82+
83+
func labelWithOrg(orgName string, b Blocker) Blocker {
84+
text := b.Message
85+
if text == "" {
86+
text = b.Type
87+
}
88+
b.Message = fmt.Sprintf("organization[%s]: %s", orgName, text)
89+
return b
90+
}
91+
4092
// BlockedError carries every blocker the up-front check found, so the
4193
// caller gets one checklist instead of discovering blockers one retry at
4294
// a time.
4395
type BlockedError struct {
4496
OrgID string
4597
ProjectID string
98+
UserID string
4699
Blockers []Blocker
47100
}
48101

@@ -51,6 +104,9 @@ func (e *BlockedError) Error() string {
51104
if e.ProjectID != "" {
52105
target = "project"
53106
}
107+
if e.UserID != "" {
108+
target = "user"
109+
}
54110
var msgs []string
55111
for _, b := range e.Blockers {
56112
if b.Message != "" {

‎core/deleter/mocks/user_pat_service.go‎

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/deleter/mocks/user_service.go‎

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)