@@ -12,7 +12,7 @@ import (
1212 "code.gitea.io/gitea/modules/util"
1313 "code.gitea.io/gitea/services/auth/source/ldap"
1414
15- "github.com/urfave/cli/v2 "
15+ "github.com/urfave/cli/v3 "
1616)
1717
1818type (
2424 }
2525)
2626
27- var (
28- commonLdapCLIFlags = []cli.Flag {
27+ func commonLdapCLIFlags () []cli. Flag {
28+ return []cli.Flag {
2929 & cli.StringFlag {
3030 Name : "name" ,
3131 Usage : "Authentication name." ,
@@ -103,8 +103,10 @@ var (
103103 Usage : "The attribute of the user’s LDAP record containing the user’s avatar." ,
104104 },
105105 }
106+ }
106107
107- ldapBindDnCLIFlags = append (commonLdapCLIFlags ,
108+ func ldapBindDnCLIFlags () []cli.Flag {
109+ return append (commonLdapCLIFlags (),
108110 & cli.StringFlag {
109111 Name : "bind-dn" ,
110112 Usage : "The DN to bind to the LDAP server with when searching for the user." ,
@@ -157,49 +159,59 @@ var (
157159 Name : "group-team-map-removal" ,
158160 Usage : "Remove users from synchronized teams if user does not belong to corresponding LDAP group" ,
159161 })
162+ }
160163
161- ldapSimpleAuthCLIFlags = append (commonLdapCLIFlags ,
164+ func ldapSimpleAuthCLIFlags () []cli.Flag {
165+ return append (commonLdapCLIFlags (),
162166 & cli.StringFlag {
163167 Name : "user-dn" ,
164168 Usage : "The user's DN." ,
165169 })
170+ }
166171
167- microcmdAuthAddLdapBindDn = & cli.Command {
172+ func microcmdAuthAddLdapBindDn () * cli.Command {
173+ return & cli.Command {
168174 Name : "add-ldap" ,
169175 Usage : "Add new LDAP (via Bind DN) authentication source" ,
170- Action : func (c * cli.Context ) error {
171- return newAuthService ().addLdapBindDn (c )
176+ Action : func (ctx context. Context , cmd * cli.Command ) error {
177+ return newAuthService ().addLdapBindDn (ctx , cmd )
172178 },
173- Flags : ldapBindDnCLIFlags ,
179+ Flags : ldapBindDnCLIFlags () ,
174180 }
181+ }
175182
176- microcmdAuthUpdateLdapBindDn = & cli.Command {
183+ func microcmdAuthUpdateLdapBindDn () * cli.Command {
184+ return & cli.Command {
177185 Name : "update-ldap" ,
178186 Usage : "Update existing LDAP (via Bind DN) authentication source" ,
179- Action : func (c * cli.Context ) error {
180- return newAuthService ().updateLdapBindDn (c )
187+ Action : func (ctx context. Context , cmd * cli.Command ) error {
188+ return newAuthService ().updateLdapBindDn (ctx , cmd )
181189 },
182- Flags : append ([]cli.Flag {idFlag }, ldapBindDnCLIFlags ... ),
190+ Flags : append ([]cli.Flag {idFlag () }, ldapBindDnCLIFlags () ... ),
183191 }
192+ }
184193
185- microcmdAuthAddLdapSimpleAuth = & cli.Command {
194+ func microcmdAuthAddLdapSimpleAuth () * cli.Command {
195+ return & cli.Command {
186196 Name : "add-ldap-simple" ,
187197 Usage : "Add new LDAP (simple auth) authentication source" ,
188- Action : func (c * cli.Context ) error {
189- return newAuthService ().addLdapSimpleAuth (c )
198+ Action : func (ctx context. Context , cmd * cli.Command ) error {
199+ return newAuthService ().addLdapSimpleAuth (ctx , cmd )
190200 },
191- Flags : ldapSimpleAuthCLIFlags ,
201+ Flags : ldapSimpleAuthCLIFlags () ,
192202 }
203+ }
193204
194- microcmdAuthUpdateLdapSimpleAuth = & cli.Command {
205+ func microcmdAuthUpdateLdapSimpleAuth () * cli.Command {
206+ return & cli.Command {
195207 Name : "update-ldap-simple" ,
196208 Usage : "Update existing LDAP (simple auth) authentication source" ,
197- Action : func (c * cli.Context ) error {
198- return newAuthService ().updateLdapSimpleAuth (c )
209+ Action : func (ctx context. Context , cmd * cli.Command ) error {
210+ return newAuthService ().updateLdapSimpleAuth (ctx , cmd )
199211 },
200- Flags : append ([]cli.Flag {idFlag }, ldapSimpleAuthCLIFlags ... ),
212+ Flags : append ([]cli.Flag {idFlag () }, ldapSimpleAuthCLIFlags () ... ),
201213 }
202- )
214+ }
203215
204216// newAuthService creates a service with default functions.
205217func newAuthService () * authService {
@@ -212,7 +224,7 @@ func newAuthService() *authService {
212224}
213225
214226// parseAuthSourceLdap assigns values on authSource according to command line flags.
215- func parseAuthSourceLdap (c * cli.Context , authSource * auth.Source ) {
227+ func parseAuthSourceLdap (c * cli.Command , authSource * auth.Source ) {
216228 if c .IsSet ("name" ) {
217229 authSource .Name = c .String ("name" )
218230 }
@@ -232,7 +244,7 @@ func parseAuthSourceLdap(c *cli.Context, authSource *auth.Source) {
232244}
233245
234246// parseLdapConfig assigns values on config according to command line flags.
235- func parseLdapConfig (c * cli.Context , config * ldap.Source ) error {
247+ func parseLdapConfig (c * cli.Command , config * ldap.Source ) error {
236248 if c .IsSet ("name" ) {
237249 config .Name = c .String ("name" )
238250 }
@@ -245,7 +257,7 @@ func parseLdapConfig(c *cli.Context, config *ldap.Source) error {
245257 if c .IsSet ("security-protocol" ) {
246258 p , ok := findLdapSecurityProtocolByName (c .String ("security-protocol" ))
247259 if ! ok {
248- return fmt .Errorf ("Unknown security protocol name: %s" , c .String ("security-protocol" ))
260+ return fmt .Errorf ("unknown security protocol name: %s" , c .String ("security-protocol" ))
249261 }
250262 config .SecurityProtocol = p
251263 }
@@ -337,32 +349,27 @@ func findLdapSecurityProtocolByName(name string) (ldap.SecurityProtocol, bool) {
337349
338350// getAuthSource gets the login source by its id defined in the command line flags.
339351// It returns an error if the id is not set, does not match any source or if the source is not of expected type.
340- func (a * authService ) getAuthSource (ctx context.Context , c * cli.Context , authType auth.Type ) (* auth.Source , error ) {
352+ func (a * authService ) getAuthSource (ctx context.Context , c * cli.Command , authType auth.Type ) (* auth.Source , error ) {
341353 if err := argsSet (c , "id" ); err != nil {
342354 return nil , err
343355 }
344-
345356 authSource , err := a .getAuthSourceByID (ctx , c .Int64 ("id" ))
346357 if err != nil {
347358 return nil , err
348359 }
349360
350361 if authSource .Type != authType {
351- return nil , fmt .Errorf ("Invalid authentication type. expected: %s, actual: %s" , authType .String (), authSource .Type .String ())
362+ return nil , fmt .Errorf ("invalid authentication type. expected: %s, actual: %s" , authType .String (), authSource .Type .String ())
352363 }
353364
354365 return authSource , nil
355366}
356367
357368// addLdapBindDn adds a new LDAP via Bind DN authentication source.
358- func (a * authService ) addLdapBindDn (c * cli.Context ) error {
369+ func (a * authService ) addLdapBindDn (ctx context. Context , c * cli.Command ) error {
359370 if err := argsSet (c , "name" , "security-protocol" , "host" , "port" , "user-search-base" , "user-filter" , "email-attribute" ); err != nil {
360371 return err
361372 }
362-
363- ctx , cancel := installSignals ()
364- defer cancel ()
365-
366373 if err := a .initDB (ctx ); err != nil {
367374 return err
368375 }
@@ -384,10 +391,7 @@ func (a *authService) addLdapBindDn(c *cli.Context) error {
384391}
385392
386393// updateLdapBindDn updates a new LDAP via Bind DN authentication source.
387- func (a * authService ) updateLdapBindDn (c * cli.Context ) error {
388- ctx , cancel := installSignals ()
389- defer cancel ()
390-
394+ func (a * authService ) updateLdapBindDn (ctx context.Context , c * cli.Command ) error {
391395 if err := a .initDB (ctx ); err != nil {
392396 return err
393397 }
@@ -406,14 +410,11 @@ func (a *authService) updateLdapBindDn(c *cli.Context) error {
406410}
407411
408412// addLdapSimpleAuth adds a new LDAP (simple auth) authentication source.
409- func (a * authService ) addLdapSimpleAuth (c * cli.Context ) error {
413+ func (a * authService ) addLdapSimpleAuth (ctx context. Context , c * cli.Command ) error {
410414 if err := argsSet (c , "name" , "security-protocol" , "host" , "port" , "user-dn" , "user-filter" , "email-attribute" ); err != nil {
411415 return err
412416 }
413417
414- ctx , cancel := installSignals ()
415- defer cancel ()
416-
417418 if err := a .initDB (ctx ); err != nil {
418419 return err
419420 }
@@ -435,10 +436,7 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error {
435436}
436437
437438// updateLdapSimpleAuth updates a new LDAP (simple auth) authentication source.
438- func (a * authService ) updateLdapSimpleAuth (c * cli.Context ) error {
439- ctx , cancel := installSignals ()
440- defer cancel ()
441-
439+ func (a * authService ) updateLdapSimpleAuth (ctx context.Context , c * cli.Command ) error {
442440 if err := a .initDB (ctx ); err != nil {
443441 return err
444442 }
0 commit comments