@@ -67,13 +67,26 @@ INSERT INTO affiliations (name, prekey, level)
67
67
DELETE FROM affiliations
68
68
WHERE (name = ?)`
69
69
70
+ deleteAffAndSubAff = `
71
+ DELETE FROM affiliations
72
+ WHERE (name = ? OR name LIKE ?)`
73
+
70
74
getAffiliationQuery = `
71
75
SELECT * FROM affiliations
72
76
WHERE (name = ?)`
73
77
74
78
getAllAffiliationsQuery = `
75
79
SELECT * FROM affiliations
76
- WHERE ((name = ?) OR (name LIKE ?))`
80
+ WHERE (name = ? OR name LIKE ?)`
81
+
82
+ getIDsWithAffiliation = `
83
+ SELECT * FROM users
84
+ WHERE (affiliation = ?)`
85
+
86
+ updateAffiliation = `
87
+ UPDATE affiliations
88
+ SET name = ?, prekey = ?
89
+ WHERE (name = ?)`
77
90
)
78
91
79
92
// UserRecord defines the properties of a user
@@ -358,22 +371,14 @@ func (d *Accessor) deleteAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
358
371
identityRemoval := args [2 ].(bool )
359
372
isRegistar := args [3 ].(bool )
360
373
361
- query := "SELECT * FROM users WHERE (affiliation = ?)"
362
- ids := []UserRecord {}
363
- err = tx .Select (& ids , tx .Rebind (query ), name )
364
- if err != nil {
365
- return nil , newHTTPErr (500 , ErrRemoveAffDB , "Failed to select users with affiliation '%s': %s" , name , err )
366
- }
367
-
368
374
subAffName := name + ".%"
369
- query = "SELECT * FROM users WHERE (affiliation LIKE ?)"
370
- subAffIds := []UserRecord {}
371
- err = tx .Select (& subAffIds , tx .Rebind (query ), subAffName )
375
+ query : = "SELECT * FROM users WHERE (affiliation = ? OR affiliation LIKE ?)"
376
+ ids := []UserRecord {}
377
+ err = tx .Select (& ids , tx .Rebind (query ), name , subAffName )
372
378
if err != nil {
373
379
return nil , newHTTPErr (500 , ErrRemoveAffDB , "Failed to select users with sub-affiliation of '%s': %s" , name , err )
374
380
}
375
381
376
- ids = append (ids , subAffIds ... )
377
382
idNames := []string {}
378
383
for _ , id := range ids {
379
384
idNames = append (idNames , id .Name )
@@ -394,25 +399,24 @@ func (d *Accessor) deleteAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
394
399
}
395
400
}
396
401
397
- aff := AffiliationRecord {}
398
- err = tx .Get ( & aff , tx .Rebind (getAffiliationQuery ), name )
402
+ allAffs := [] AffiliationRecord {}
403
+ err = tx .Select ( & allAffs , tx .Rebind (getAllAffiliationsQuery ), name , subAffName )
399
404
if err != nil {
400
405
return nil , getError (err , "Affiliation" )
401
406
}
402
- // Getting all the sub-affiliations that are going to be deleted
403
- allAffs := []AffiliationRecord {}
404
- err = tx .Select (& allAffs , tx .Rebind ("Select * FROM affiliations where (name LIKE ?)" ), subAffName )
405
- if err != nil {
406
- return nil , newHTTPErr (500 , ErrRemoveAffDB , "Failed to select sub-affiliations of '%s': %s" , allAffs , err )
407
+
408
+ affNames := []string {}
409
+ for _ , aff := range allAffs {
410
+ affNames = append (affNames , aff .Name )
407
411
}
412
+ affNamesStr := strings .Join (affNames , "," )
408
413
409
- if len (allAffs ) > 0 {
414
+ if len (allAffs ) > 1 {
410
415
if ! force {
411
416
// If force option is not specified, only delete affiliation if there are no sub-affiliations
412
- return nil , newAuthErr (ErrUpdateConfigRemoveAff , "Cannot delete affiliation '%s'. The affiliation has the following sub-affiliations: %s. Need to use 'force' to remove affiliation and sub-affiliations" , name , allAffs )
417
+ return nil , newAuthErr (ErrUpdateConfigRemoveAff , "Cannot delete affiliation '%s'. The affiliation has the following sub-affiliations: %s. Need to use 'force' to remove affiliation and sub-affiliations" , name , affNamesStr )
413
418
}
414
419
}
415
- allAffs = append (allAffs , aff )
416
420
417
421
// Now proceed with deletion
418
422
@@ -445,19 +449,12 @@ func (d *Accessor) deleteAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
445
449
446
450
log .Debugf ("All affiliations to be removed: %s" , allAffs )
447
451
448
- // Delete the requested affiliation
449
- _ , err = tx .Exec (tx .Rebind (deleteAffiliation ), name )
452
+ // Delete the requested affiliation and it's subaffiliations
453
+ _ , err = tx .Exec (tx .Rebind (deleteAffAndSubAff ), name , subAffName )
450
454
if err != nil {
451
455
return nil , newHTTPErr (500 , ErrRemoveAffDB , "Failed to delete affiliation '%s': %s" , name , err )
452
456
}
453
457
454
- if len (allAffs ) > 1 {
455
- // Delete all the sub-affiliations
456
- _ , err = tx .Exec (tx .Rebind ("DELETE FROM affiliations where (name LIKE ?)" ), subAffName )
457
- if err != nil {
458
- return nil , newHTTPErr (500 , ErrRemoveAffDB , "Failed to delete affiliations: %s" , err )
459
- }
460
- }
461
458
// Return the identities and affiliations that were removed
462
459
result := d .getResult (ids , allAffs )
463
460
@@ -712,24 +709,13 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
712
709
force := args [2 ].(bool )
713
710
isRegistar := args [3 ].(bool )
714
711
715
- // Get the affiliation record
716
- query := "SELECT name, prekey FROM affiliations WHERE (name = ?)"
717
- var oldAffiliationRecord AffiliationRecord
718
- err := tx .Get (& oldAffiliationRecord , tx .Rebind (query ), oldAffiliation )
719
- if err != nil {
720
- return nil , err
721
- }
722
-
723
- // Get the affiliation records for all sub affiliations
724
- query = "SELECT name, prekey FROM affiliations WHERE (name LIKE ?)"
712
+ // Get the affiliation records including all sub affiliations
725
713
var allOldAffiliations []AffiliationRecord
726
- err = tx .Select (& allOldAffiliations , tx .Rebind (query ) , oldAffiliation + ".%" )
714
+ err : = tx .Select (& allOldAffiliations , tx .Rebind (getAllAffiliationsQuery ), oldAffiliation , oldAffiliation + ".%" )
727
715
if err != nil {
728
716
return nil , err
729
717
}
730
718
731
- allOldAffiliations = append (allOldAffiliations , oldAffiliationRecord )
732
-
733
719
log .Debugf ("Affiliations to be modified %+v" , allOldAffiliations )
734
720
735
721
// Iterate through all the affiliations found and update to use new affiliation path
@@ -743,8 +729,7 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
743
729
log .Debugf ("oldPath: %s, newPath: %s, oldParentPath: %s, newParentPath: %s" , oldPath , newPath , oldParentPath , newParentPath )
744
730
745
731
// Select all users that are using the old affiliation
746
- query = "SELECT * FROM users WHERE (affiliation = ?)"
747
- err = tx .Select (& idsWithOldAff , tx .Rebind (query ), oldPath )
732
+ err = tx .Select (& idsWithOldAff , tx .Rebind (getIDsWithAffiliation ), oldPath )
748
733
if err != nil {
749
734
return nil , err
750
735
}
@@ -820,8 +805,7 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
820
805
}
821
806
822
807
// Update the affiliation record in the database to use new affiliation path
823
- query = "Update affiliations SET name = ?, prekey = ? WHERE (name = ?)"
824
- res := tx .MustExec (tx .Rebind (query ), newPath , newParentPath , oldPath )
808
+ res := tx .MustExec (tx .Rebind (updateAffiliation ), newPath , newParentPath , oldPath )
825
809
numRowsAffected , err := res .RowsAffected ()
826
810
if err != nil {
827
811
return nil , errors .Errorf ("Failed to get number of rows affected" )
@@ -834,7 +818,7 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
834
818
// Generate the result set that has all identities with their new affiliation and all renamed affiliations
835
819
var idsWithNewAff []UserRecord
836
820
if len (idsUpdated ) > 0 {
837
- query = "Select * FROM users WHERE (id IN (?))"
821
+ query : = "Select * FROM users WHERE (id IN (?))"
838
822
inQuery , args , err := sqlx .In (query , idsUpdated )
839
823
if err != nil {
840
824
return nil , errors .Wrapf (err , "Failed to construct query '%s'" , query )
0 commit comments