Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Vitanov committed Nov 29, 2023
1 parent ec6cd7f commit 444aa25
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
25 changes: 8 additions & 17 deletions core/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1933,32 +1933,23 @@ func (a *Auth) deleteAccount(context storage.TransactionContext, account model.A
}

func (a *Auth) deleteAccountFromApps(context storage.TransactionContext, account model.Account, fromAppsIDs []string) error {
var orgApps []model.OrgAppMembership
for _, idToDelete := range fromAppsIDs {
orgA, err := a.giveMembershipsByAppsIds(context, account.OrgAppsMemberships, idToDelete)
if err != nil {
return errors.WrapErrorAction(logutils.ActionDelete, model.TypeAccount, nil, err)
// compare the applicationIDs and find the matching IDs for the org_app_memberships
var membershipsIDs []string
for _, a := range account.OrgAppsMemberships {
for _, b := range fromAppsIDs {
if a.AppOrg.Application.ID == b {
membershipsIDs = append(membershipsIDs, a.ID)
}
}
orgApps = orgA
}

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

return nil
}
func (a *Auth) giveMembershipsByAppsIds(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
2 changes: 1 addition & 1 deletion core/auth/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +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
UpdateAccountOrgAppsMembershipsObject(context storage.TransactionContext, accountID string, orgApps []model.OrgAppMembership) error
DeleteOrgAppsMembershipsObject(context storage.TransactionContext, accountID string, membershipsIDs []string) error

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

// UpdateAccountOrgAppsMembershipsObject update the whole account org_apps_memberships object
func (sa *Adapter) UpdateAccountOrgAppsMembershipsObject(context TransactionContext, accountID string, orgApps []model.OrgAppMembership) error {
// DeleteOrgAppsMembershipsObject delete the whole account org_apps_memberships object
func (sa *Adapter) DeleteOrgAppsMembershipsObject(context TransactionContext, accountID string, membershipsIDs []string) error {
//filter
filter := bson.D{
primitive.E{Key: "_id", Value: accountID},
}

// update
update := bson.D{
primitive.E{Key: "$pull", Value: bson.D{
primitive.E{Key: "org_apps_memberships", Value: bson.M{"id": bson.M{"$in": membershipsIDs}}},
}},
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)
Expand Down

0 comments on commit 444aa25

Please sign in to comment.