mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 05:11:41 +01:00 
			
		
		
		
	Backport #31829 by @lunny Fix #31730 This PR rewrote the function `PublicKeysAreExternallyManaged` with a simple test. The new function removed the loop to make it more readable. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		
							parent
							
								
									b6ede69a1b
								
							
						
					
					
						commit
						5fa90ad9bc
					
				| @ -229,35 +229,26 @@ func UpdatePublicKeyUpdated(ctx context.Context, id int64) error { | ||||
| 
 | ||||
| // PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key | ||||
| func PublicKeysAreExternallyManaged(ctx context.Context, keys []*PublicKey) ([]bool, error) { | ||||
| 	sources := make([]*auth.Source, 0, 5) | ||||
| 	sourceCache := make(map[int64]*auth.Source, len(keys)) | ||||
| 	externals := make([]bool, len(keys)) | ||||
| keyloop: | ||||
| 
 | ||||
| 	for i, key := range keys { | ||||
| 		if key.LoginSourceID == 0 { | ||||
| 			externals[i] = false | ||||
| 			continue keyloop | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		var source *auth.Source | ||||
| 
 | ||||
| 	sourceloop: | ||||
| 		for _, s := range sources { | ||||
| 			if s.ID == key.LoginSourceID { | ||||
| 				source = s | ||||
| 				break sourceloop | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if source == nil { | ||||
| 		source, ok := sourceCache[key.LoginSourceID] | ||||
| 		if !ok { | ||||
| 			var err error | ||||
| 			source, err = auth.GetSourceByID(ctx, key.LoginSourceID) | ||||
| 			if err != nil { | ||||
| 				if auth.IsErrSourceNotExist(err) { | ||||
| 					externals[i] = false | ||||
| 					sources[i] = &auth.Source{ | ||||
| 					sourceCache[key.LoginSourceID] = &auth.Source{ | ||||
| 						ID: key.LoginSourceID, | ||||
| 					} | ||||
| 					continue keyloop | ||||
| 					continue | ||||
| 				} | ||||
| 				return nil, err | ||||
| 			} | ||||
|  | ||||
| @ -12,6 +12,8 @@ import ( | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	"code.gitea.io/gitea/models/unittest" | ||||
| 	"code.gitea.io/gitea/modules/setting" | ||||
| 
 | ||||
| 	"github.com/42wim/sshsig" | ||||
| @ -503,3 +505,11 @@ func runErr(t *testing.T, stdin []byte, args ...string) { | ||||
| 		t.Fatal("expected error") | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func Test_PublicKeysAreExternallyManaged(t *testing.T) { | ||||
| 	key1 := unittest.AssertExistsAndLoadBean(t, &PublicKey{ID: 1}) | ||||
| 	externals, err := PublicKeysAreExternallyManaged(db.DefaultContext, []*PublicKey{key1}) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Len(t, externals, 1) | ||||
| 	assert.False(t, externals[0]) | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user