@@ -14,6 +14,7 @@ import (
14
14
"code.gitea.io/gitea/modules/timeutil"
15
15
"code.gitea.io/gitea/modules/util"
16
16
17
+ "xorm.io/builder"
17
18
"xorm.io/xorm"
18
19
"xorm.io/xorm/convert"
19
20
)
@@ -240,37 +241,26 @@ func CreateSource(ctx context.Context, source *Source) error {
240
241
return err
241
242
}
242
243
243
- // Sources returns a slice of all login sources found in DB.
244
- func Sources (ctx context.Context ) ([]* Source , error ) {
245
- auths := make ([]* Source , 0 , 6 )
246
- return auths , db .GetEngine (ctx ).Find (& auths )
244
+ type FindSourcesOptions struct {
245
+ IsActive util.OptionalBool
246
+ LoginType Type
247
247
}
248
248
249
- // SourcesByType returns all sources of the specified type
250
- func SourcesByType (ctx context.Context , loginType Type ) ([]* Source , error ) {
251
- sources := make ([]* Source , 0 , 1 )
252
- if err := db .GetEngine (ctx ).Where ("type = ?" , loginType ).Find (& sources ); err != nil {
253
- return nil , err
249
+ func (opts FindSourcesOptions ) ToConds () builder.Cond {
250
+ conds := builder .NewCond ()
251
+ if ! opts .IsActive .IsNone () {
252
+ conds = conds .And (builder.Eq {"is_active" : opts .IsActive .IsTrue ()})
254
253
}
255
- return sources , nil
256
- }
257
-
258
- // AllActiveSources returns all active sources
259
- func AllActiveSources (ctx context.Context ) ([]* Source , error ) {
260
- sources := make ([]* Source , 0 , 5 )
261
- if err := db .GetEngine (ctx ).Where ("is_active = ?" , true ).Find (& sources ); err != nil {
262
- return nil , err
254
+ if opts .LoginType != NoType {
255
+ conds = conds .And (builder.Eq {"`type`" : opts .LoginType })
263
256
}
264
- return sources , nil
257
+ return conds
265
258
}
266
259
267
- // ActiveSources returns all active sources of the specified type
268
- func ActiveSources (ctx context.Context , tp Type ) ([]* Source , error ) {
269
- sources := make ([]* Source , 0 , 1 )
270
- if err := db .GetEngine (ctx ).Where ("is_active = ? and type = ?" , true , tp ).Find (& sources ); err != nil {
271
- return nil , err
272
- }
273
- return sources , nil
260
+ // FindSources returns a slice of login sources found in DB according to given conditions.
261
+ func FindSources (ctx context.Context , opts FindSourcesOptions ) ([]* Source , error ) {
262
+ auths := make ([]* Source , 0 , 6 )
263
+ return auths , db .GetEngine (ctx ).Where (opts .ToConds ()).Find (& auths )
274
264
}
275
265
276
266
// IsSSPIEnabled returns true if there is at least one activated login
@@ -279,7 +269,10 @@ func IsSSPIEnabled(ctx context.Context) bool {
279
269
if ! db .HasEngine {
280
270
return false
281
271
}
282
- sources , err := ActiveSources (ctx , SSPI )
272
+ sources , err := FindSources (ctx , FindSourcesOptions {
273
+ IsActive : util .OptionalBoolTrue ,
274
+ LoginType : SSPI ,
275
+ })
283
276
if err != nil {
284
277
log .Error ("ActiveSources: %v" , err )
285
278
return false
@@ -354,8 +347,8 @@ func UpdateSource(ctx context.Context, source *Source) error {
354
347
}
355
348
356
349
// CountSources returns number of login sources.
357
- func CountSources (ctx context.Context ) int64 {
358
- count , _ := db .GetEngine (ctx ).Count (new (Source ))
350
+ func CountSources (ctx context.Context , opts FindSourcesOptions ) int64 {
351
+ count , _ := db .GetEngine (ctx ).Where ( opts . ToConds ()). Count (new (Source ))
359
352
return count
360
353
}
361
354
0 commit comments