@@ -14,6 +14,7 @@ import (
1414	"code.gitea.io/gitea/modules/timeutil" 
1515	"code.gitea.io/gitea/modules/util" 
1616
17+ 	"xorm.io/builder" 
1718	"xorm.io/xorm" 
1819	"xorm.io/xorm/convert" 
1920)
@@ -240,37 +241,26 @@ func CreateSource(ctx context.Context, source *Source) error {
240241	return  err 
241242}
242243
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 
247247}
248248
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 ()})
254253	}
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 })
263256	}
264- 	return  sources ,  nil 
257+ 	return  conds 
265258}
266259
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 )
274264}
275265
276266// IsSSPIEnabled returns true if there is at least one activated login 
@@ -279,7 +269,10 @@ func IsSSPIEnabled(ctx context.Context) bool {
279269	if  ! db .HasEngine  {
280270		return  false 
281271	}
282- 	sources , err  :=  ActiveSources (ctx , SSPI )
272+ 	sources , err  :=  FindSources (ctx , FindSourcesOptions {
273+ 		IsActive :  util .OptionalBoolTrue ,
274+ 		LoginType : SSPI ,
275+ 	})
283276	if  err  !=  nil  {
284277		log .Error ("ActiveSources: %v" , err )
285278		return  false 
@@ -354,8 +347,8 @@ func UpdateSource(ctx context.Context, source *Source) error {
354347}
355348
356349// 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 ))
359352	return  count 
360353}
361354
0 commit comments