Skip to content

Commit

Permalink
Delete Membership
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Vitanov committed Nov 28, 2023
1 parent 5ae562c commit 5b4d97e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
21 changes: 20 additions & 1 deletion core/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1933,9 +1933,28 @@ func (a *Auth) deleteAccount(context storage.TransactionContext, account model.A
}

func (a *Auth) deleteAccountFromApps(context storage.TransactionContext, account model.Account, fromAppsIDs []string) error {
//TODO - Stefan
for _, idToDelete := range fromAppsIDs {
orgApps, _ := a.deleteApps(context, account.OrgAppsMemberships, idToDelete)
account.OrgAppsMemberships = orgApps

err := a.storage.DeleteAccountOrgAppsMemberships(context, account.ID, orgApps)
if err != nil {
return errors.WrapErrorAction(logutils.ActionDelete, model.TypeAccount, nil, err)
}
}

return nil
}
func (a *Auth) deleteApps(context storage.TransactionContext, orgApp []model.OrgAppMembership, fromAppsIDs string) ([]model.OrgAppMembership, error) {
for i, membership := range orgApp {
if membership.AppOrg.Application.ID == fromAppsIDs {
// Found the matching ID, remove it from the slice
orgApp = append(orgApp[:i], orgApp[i+1:]...)
break
}
}
return orgApp, nil
}

func (a *Auth) deleteFullAccount(context storage.TransactionContext, account model.Account) error {
//1. delete the account record
Expand Down
1 change: 1 addition & 0 deletions core/auth/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ type Storage interface {
SaveAccount(context storage.TransactionContext, account *model.Account) error
DeleteAccount(context storage.TransactionContext, id string) error
UpdateAccountUsageInfo(context storage.TransactionContext, accountID string, updateLoginTime bool, updateAccessTokenTime bool, clientVersion *string) error
DeleteAccountOrgAppsMemberships(context storage.TransactionContext, accountID string, orgApps []model.OrgAppMembership) error

//Profiles
UpdateAccountProfile(context storage.TransactionContext, profile model.Profile) error
Expand Down
24 changes: 24 additions & 0 deletions driven/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,30 @@ func (sa *Adapter) DeleteAccountRoles(context TransactionContext, accountID stri
return nil
}

// DeleteAccountOrgAppsMemberships deletes account org_apps_membership
func (sa *Adapter) DeleteAccountOrgAppsMemberships(context TransactionContext, accountID string, orgApps []model.OrgAppMembership) error {
//filter
filter := bson.D{
primitive.E{Key: "_id", Value: accountID},
}

update := bson.D{
primitive.E{Key: "$set", Value: bson.D{
primitive.E{Key: "org_apps_memberships", Value: orgApps},
primitive.E{Key: "date_updated", Value: time.Now().UTC()},
}},
}

res, err := sa.db.tenantsAccounts.UpdateOneWithContext(context, filter, update, nil)
if err != nil {
return errors.WrapErrorAction(logutils.ActionUpdate, model.TypeAccount, &logutils.FieldArgs{"_id": accountID}, err)
}
if res.ModifiedCount != 1 {
return errors.ErrorAction(logutils.ActionUpdate, model.TypeAccount, &logutils.FieldArgs{"_id": accountID, "modified": res.ModifiedCount, "expected": 1})
}
return nil
}

// UpdateAccountGroups updates the account groups
func (sa *Adapter) UpdateAccountGroups(context TransactionContext, accountID string, appOrgID string, groups []model.AccountGroup) error {
stgGroups := accountGroupsToStorage(groups)
Expand Down

0 comments on commit 5b4d97e

Please sign in to comment.