Skip to content

Commit

Permalink
improve: remove gorm txn from assign users to groups
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Sep 2, 2022
1 parent c859837 commit 055e9f4
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions internal/server/data/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func AssignIdentityToGroups(tx GormTxn, user *models.Identity, provider *models.Provider, newGroups []string) error {
db := tx.GormDB()
pu, err := GetProviderUser(tx, provider.ID, user.ID)
if err != nil {
return err
Expand All @@ -24,14 +23,15 @@ func AssignIdentityToGroups(tx GormTxn, user *models.Identity, provider *models.

pu.Groups = newGroups
pu.LastUpdate = time.Now().UTC()
if err := save(tx, pu); err != nil {
if err := UpdateProviderUser(tx, pu); err != nil {
return fmt.Errorf("save: %w", err)
}

// remove user from groups
if len(groupsToBeRemoved) > 0 {
err = db.Exec("delete from identities_groups where identity_id = ? and group_id in (select id from groups where name in (?))", user.ID, groupsToBeRemoved).Error
if err != nil {
stmt := `DELETE FROM identities_groups WHERE identity_id = ? AND group_id in (
SELECT id from groups where name in (?))`
if _, err := tx.Exec(stmt, user.ID, groupsToBeRemoved); err != nil {
return err
}
for _, name := range groupsToBeRemoved {
Expand All @@ -44,13 +44,27 @@ func AssignIdentityToGroups(tx GormTxn, user *models.Identity, provider *models.
}
}

var addIDs []struct {
type idNamePair struct {
ID uid.ID
Name string
}
err = db.Table("groups").Select("id, name").Where("name in (?)", groupsToBeAdded).Scan(&addIDs).Error
var addIDs []idNamePair

stmt := `SELECT id, name FROM groups WHERE name in (?)`
rows, err := tx.Query(stmt, groupsToBeAdded)
if err != nil {
return fmt.Errorf("group ids: %w", err)
return err
}
defer rows.Close()
for rows.Next() {
var item idNamePair
if err := rows.Scan(&item.ID, &item.Name); err != nil {
return err
}
addIDs = append(addIDs, item)
}
if rows.Err() != nil {
return err
}

for _, name := range groupsToBeAdded {
Expand All @@ -77,13 +91,26 @@ func AssignIdentityToGroups(tx GormTxn, user *models.Identity, provider *models.
}

var ids []uid.ID
if err := db.Raw("SELECT identity_id FROM identities_groups WHERE identity_id = ? AND group_id = ?", user.ID, groupID).Scan(&ids).Error; err != nil {
return fmt.Errorf("select: %w", handleError(err))
rows, err := tx.Query("SELECT identity_id FROM identities_groups WHERE identity_id = ? AND group_id = ?", user.ID, groupID)
if err != nil {
return err
}

for rows.Next() {
var item uid.ID
if err := rows.Scan(&item); err != nil {
rows.Close()
return err
}
ids = append(ids, item)
}
if rows.Err() != nil {
return err
}

if len(ids) == 0 {
// add user to group
err = db.Exec("insert into identities_groups (identity_id, group_id) values (?, ?)", user.ID, groupID).Error
_, err = tx.Exec("insert into identities_groups (identity_id, group_id) values (?, ?)", user.ID, groupID)
if err != nil {
return fmt.Errorf("insert: %w", handleError(err))
}
Expand Down

0 comments on commit 055e9f4

Please sign in to comment.