Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions core/deleter/mocks/membership_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 0 additions & 62 deletions core/deleter/mocks/organization_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions core/deleter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/google/uuid"
"github.com/raystack/frontier/core/invitation"
"github.com/raystack/frontier/core/membership"

"github.com/raystack/frontier/core/policy"
"github.com/raystack/frontier/core/role"
Expand All @@ -44,7 +45,6 @@ type OrganizationService interface {
Get(ctx context.Context, id string) (organization.Organization, error)
DeleteModel(ctx context.Context, id string) error
RemoveUsers(ctx context.Context, orgID string, userIDs []string) error
ListByUser(ctx context.Context, principal authenticate.Principal, f organization.Filter) ([]organization.Organization, error)
}

type RoleService interface {
Expand All @@ -71,6 +71,7 @@ type GroupService interface {

type MembershipService interface {
OnGroupDeleted(ctx context.Context, groupID string) error
ListResourcesByPrincipal(ctx context.Context, principal authenticate.Principal, resourceType string, filter membership.ResourceFilter) ([]string, error)
}

type InvitationService interface {
Expand Down Expand Up @@ -378,17 +379,19 @@ func (d Service) RemoveUsersFromOrg(ctx context.Context, orgID string, userIDs [
return d.orgService.RemoveUsers(ctx, orgID, userIDs)
}

// DeleteUser visits every org the user has a policy on (disabled orgs too),
// otherwise userService.Delete would leave orphan policy rows behind.
func (d Service) DeleteUser(ctx context.Context, userID string) error {
userOrgs, err := d.orgService.ListByUser(ctx, authenticate.Principal{
orgIDs, err := d.membershipService.ListResourcesByPrincipal(ctx, authenticate.Principal{
ID: userID,
Type: schema.UserPrincipal,
}, organization.Filter{})
}, schema.OrganizationNamespace, membership.ResourceFilter{})
if err != nil {
return err
}
for _, org := range userOrgs {
if err = d.RemoveUsersFromOrg(ctx, org.ID, []string{userID}); err != nil {
return fmt.Errorf("failed to delete user from org[%s]: %w", org.Name, err)
for _, orgID := range orgIDs {
if err = d.RemoveUsersFromOrg(ctx, orgID, []string{userID}); err != nil {
return fmt.Errorf("failed to delete user from org[%s]: %w", orgID, err)
}
}
return d.userService.Delete(ctx, userID)
Expand Down
3 changes: 2 additions & 1 deletion core/deleter/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/raystack/frontier/core/resource"
"github.com/raystack/frontier/core/role"
"github.com/raystack/frontier/core/serviceuser"
"github.com/raystack/frontier/internal/bootstrap/schema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -259,7 +260,7 @@ func TestDeleteUser(t *testing.T) {
t.Run("removes user from all orgs then deletes", func(t *testing.T) {
orgSvc, projSvc, resSvc, grpSvc, mbrSvc, polSvc, roleSvc, invSvc, usrSvc, suSvc, custSvc, subSvc, invocSvc := newMocks(t)

orgSvc.EXPECT().ListByUser(mock.Anything, mock.Anything, mock.Anything).
mbrSvc.EXPECT().ListResourcesByPrincipal(mock.Anything, mock.Anything, schema.OrganizationNamespace, mock.Anything).
Return(nil, nil)
usrSvc.EXPECT().Delete(mock.Anything, "user-1").Return(nil)

Expand Down
151 changes: 151 additions & 0 deletions core/domain/mocks/membership_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading