@@ -69,6 +69,29 @@ func signingModeFromStrings(modeStrings []string) []signingMode {
69
69
return returnable
70
70
}
71
71
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
+
72
95
// ErrWontSign explains the first reason why a commit would not be signed
73
96
// There may be other reasons - this is just the first reason found
74
97
type ErrWontSign struct {
@@ -170,14 +193,11 @@ Loop:
170
193
case always :
171
194
break Loop
172
195
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 )
177
197
if err != nil {
178
198
return false , nil , nil , err
179
199
}
180
- if len ( keys ) == 0 {
200
+ if ! hasKeys {
181
201
return false , nil , nil , & ErrWontSign {pubkey }
182
202
}
183
203
case twofa :
@@ -210,14 +230,11 @@ Loop:
210
230
case always :
211
231
break Loop
212
232
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 )
217
234
if err != nil {
218
235
return false , nil , nil , err
219
236
}
220
- if len ( keys ) == 0 {
237
+ if ! hasKeys {
221
238
return false , nil , nil , & ErrWontSign {pubkey }
222
239
}
223
240
case twofa :
@@ -266,14 +283,11 @@ Loop:
266
283
case always :
267
284
break Loop
268
285
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 )
273
287
if err != nil {
274
288
return false , nil , nil , err
275
289
}
276
- if len ( keys ) == 0 {
290
+ if ! hasKeys {
277
291
return false , nil , nil , & ErrWontSign {pubkey }
278
292
}
279
293
case twofa :
@@ -337,14 +351,11 @@ Loop:
337
351
case always :
338
352
break Loop
339
353
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 )
344
355
if err != nil {
345
356
return false , nil , nil , err
346
357
}
347
- if len ( keys ) == 0 {
358
+ if ! hasKeys {
348
359
return false , nil , nil , & ErrWontSign {pubkey }
349
360
}
350
361
case twofa :
0 commit comments