@@ -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 {
3030Name :"name" ,
3131Usage :"Authentication name." ,
@@ -103,8 +103,10 @@ var (
103103Usage :"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 {
109111Name :"bind-dn" ,
110112Usage :"The DN to bind to the LDAP server with when searching for the user." ,
@@ -157,49 +159,59 @@ var (
157159Name :"group-team-map-removal" ,
158160Usage :"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 {
163167Name :"user-dn" ,
164168Usage :"The user's DN." ,
165169})
170+ }
166171
167- microcmdAuthAddLdapBindDn = & cli.Command {
172+ func microcmdAuthAddLdapBindDn ()* cli.Command {
173+ return & cli.Command {
168174Name :"add-ldap" ,
169175Usage :"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 {
177185Name :"update-ldap" ,
178186Usage :"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 {
186196Name :"add-ldap-simple" ,
187197Usage :"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 {
195207Name :"update-ldap-simple" ,
196208Usage :"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 ) {
216228if c .IsSet ("name" ) {
217229authSource .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 {
236248if c .IsSet ("name" ) {
237249config .Name = c .String ("name" )
238250}
@@ -245,7 +257,7 @@ func parseLdapConfig(c *cli.Context, config *ldap.Source) error {
245257if c .IsSet ("security-protocol" ) {
246258p ,ok := findLdapSecurityProtocolByName (c .String ("security-protocol" ))
247259if ! 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}
250262config .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 ) {
341353if err := argsSet (c ,"id" );err != nil {
342354return nil ,err
343355}
344-
345356authSource ,err := a .getAuthSourceByID (ctx ,c .Int64 ("id" ))
346357if err != nil {
347358return nil ,err
348359}
349360
350361if 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
354365return 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 {
359370if err := argsSet (c ,"name" ,"security-protocol" ,"host" ,"port" ,"user-search-base" ,"user-filter" ,"email-attribute" );err != nil {
360371return err
361372}
362-
363- ctx ,cancel := installSignals ()
364- defer cancel ()
365-
366373if err := a .initDB (ctx );err != nil {
367374return 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 {
391395if err := a .initDB (ctx );err != nil {
392396return 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 {
410414if err := argsSet (c ,"name" ,"security-protocol" ,"host" ,"port" ,"user-dn" ,"user-filter" ,"email-attribute" );err != nil {
411415return err
412416}
413417
414- ctx ,cancel := installSignals ()
415- defer cancel ()
416-
417418if err := a .initDB (ctx );err != nil {
418419return 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 {
442440if err := a .initDB (ctx );err != nil {
443441return err
444442}