@@ -69,6 +69,29 @@ func signingModeFromStrings(modeStrings []string) []signingMode {
6969 return returnable
7070}
7171
72+ func userHasPubkeysGPG (ctx context.Context , userID int64 ) (bool , error ) {
73+ return db .Exist [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
74+ OwnerID : userID ,
75+ IncludeSubKeys : true ,
76+ }.ToConds ())
77+ }
78+
79+ func userHasPubkeysSSH (ctx context.Context , userID int64 ) (bool , error ) {
80+ return db .Exist [asymkey_model.PublicKey ](ctx , asymkey_model.FindPublicKeyOptions {
81+ OwnerID : userID ,
82+ NotKeytype : asymkey_model .KeyTypePrincipal ,
83+ }.ToConds ())
84+ }
85+
86+ // userHasPubkeys checks if a user has any public keys (GPG or SSH)
87+ func userHasPubkeys (ctx context.Context , userID int64 ) (bool , error ) {
88+ has , err := userHasPubkeysGPG (ctx , userID )
89+ if has || err != nil {
90+ return has , err
91+ }
92+ return userHasPubkeysSSH (ctx , userID )
93+ }
94+
7295// ErrWontSign explains the first reason why a commit would not be signed
7396// There may be other reasons - this is just the first reason found
7497type ErrWontSign struct {
@@ -170,14 +193,11 @@ Loop:
170193 case always :
171194 break Loop
172195 case pubkey :
173- keys , err := db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
174- OwnerID : u .ID ,
175- IncludeSubKeys : true ,
176- })
196+ hasKeys , err := userHasPubkeys (ctx , u .ID )
177197 if err != nil {
178198 return false , nil , nil , err
179199 }
180- if len ( keys ) == 0 {
200+ if ! hasKeys {
181201 return false , nil , nil , & ErrWontSign {pubkey }
182202 }
183203 case twofa :
@@ -210,14 +230,11 @@ Loop:
210230 case always :
211231 break Loop
212232 case pubkey :
213- keys , err := db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
214- OwnerID : u .ID ,
215- IncludeSubKeys : true ,
216- })
233+ hasKeys , err := userHasPubkeys (ctx , u .ID )
217234 if err != nil {
218235 return false , nil , nil , err
219236 }
220- if len ( keys ) == 0 {
237+ if ! hasKeys {
221238 return false , nil , nil , & ErrWontSign {pubkey }
222239 }
223240 case twofa :
@@ -266,14 +283,11 @@ Loop:
266283 case always :
267284 break Loop
268285 case pubkey :
269- keys , err := db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
270- OwnerID : u .ID ,
271- IncludeSubKeys : true ,
272- })
286+ hasKeys , err := userHasPubkeys (ctx , u .ID )
273287 if err != nil {
274288 return false , nil , nil , err
275289 }
276- if len ( keys ) == 0 {
290+ if ! hasKeys {
277291 return false , nil , nil , & ErrWontSign {pubkey }
278292 }
279293 case twofa :
@@ -337,14 +351,11 @@ Loop:
337351 case always :
338352 break Loop
339353 case pubkey :
340- keys , err := db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
341- OwnerID : u .ID ,
342- IncludeSubKeys : true ,
343- })
354+ hasKeys , err := userHasPubkeys (ctx , u .ID )
344355 if err != nil {
345356 return false , nil , nil , err
346357 }
347- if len ( keys ) == 0 {
358+ if ! hasKeys {
348359 return false , nil , nil , & ErrWontSign {pubkey }
349360 }
350361 case twofa :
0 commit comments