@@ -12,7 +12,7 @@ import (
12
12
"code.gitea.io/gitea/modules/util"
13
13
"code.gitea.io/gitea/services/auth/source/ldap"
14
14
15
- "github.com/urfave/cli/v2 "
15
+ "github.com/urfave/cli/v3 "
16
16
)
17
17
18
18
type (
24
24
}
25
25
)
26
26
27
- var (
28
- commonLdapCLIFlags = []cli.Flag {
27
+ func commonLdapCLIFlags () []cli. Flag {
28
+ return []cli.Flag {
29
29
& cli.StringFlag {
30
30
Name : "name" ,
31
31
Usage : "Authentication name." ,
@@ -103,8 +103,10 @@ var (
103
103
Usage : "The attribute of the user’s LDAP record containing the user’s avatar." ,
104
104
},
105
105
}
106
+ }
106
107
107
- ldapBindDnCLIFlags = append (commonLdapCLIFlags ,
108
+ func ldapBindDnCLIFlags () []cli.Flag {
109
+ return append (commonLdapCLIFlags (),
108
110
& cli.StringFlag {
109
111
Name : "bind-dn" ,
110
112
Usage : "The DN to bind to the LDAP server with when searching for the user." ,
@@ -157,49 +159,59 @@ var (
157
159
Name : "group-team-map-removal" ,
158
160
Usage : "Remove users from synchronized teams if user does not belong to corresponding LDAP group" ,
159
161
})
162
+ }
160
163
161
- ldapSimpleAuthCLIFlags = append (commonLdapCLIFlags ,
164
+ func ldapSimpleAuthCLIFlags () []cli.Flag {
165
+ return append (commonLdapCLIFlags (),
162
166
& cli.StringFlag {
163
167
Name : "user-dn" ,
164
168
Usage : "The user's DN." ,
165
169
})
170
+ }
166
171
167
- microcmdAuthAddLdapBindDn = & cli.Command {
172
+ func microcmdAuthAddLdapBindDn () * cli.Command {
173
+ return & cli.Command {
168
174
Name : "add-ldap" ,
169
175
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 )
172
178
},
173
- Flags : ldapBindDnCLIFlags ,
179
+ Flags : ldapBindDnCLIFlags () ,
174
180
}
181
+ }
175
182
176
- microcmdAuthUpdateLdapBindDn = & cli.Command {
183
+ func microcmdAuthUpdateLdapBindDn () * cli.Command {
184
+ return & cli.Command {
177
185
Name : "update-ldap" ,
178
186
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 )
181
189
},
182
- Flags : append ([]cli.Flag {idFlag }, ldapBindDnCLIFlags ... ),
190
+ Flags : append ([]cli.Flag {idFlag () }, ldapBindDnCLIFlags () ... ),
183
191
}
192
+ }
184
193
185
- microcmdAuthAddLdapSimpleAuth = & cli.Command {
194
+ func microcmdAuthAddLdapSimpleAuth () * cli.Command {
195
+ return & cli.Command {
186
196
Name : "add-ldap-simple" ,
187
197
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 )
190
200
},
191
- Flags : ldapSimpleAuthCLIFlags ,
201
+ Flags : ldapSimpleAuthCLIFlags () ,
192
202
}
203
+ }
193
204
194
- microcmdAuthUpdateLdapSimpleAuth = & cli.Command {
205
+ func microcmdAuthUpdateLdapSimpleAuth () * cli.Command {
206
+ return & cli.Command {
195
207
Name : "update-ldap-simple" ,
196
208
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 )
199
211
},
200
- Flags : append ([]cli.Flag {idFlag }, ldapSimpleAuthCLIFlags ... ),
212
+ Flags : append ([]cli.Flag {idFlag () }, ldapSimpleAuthCLIFlags () ... ),
201
213
}
202
- )
214
+ }
203
215
204
216
// newAuthService creates a service with default functions.
205
217
func newAuthService () * authService {
@@ -212,7 +224,7 @@ func newAuthService() *authService {
212
224
}
213
225
214
226
// 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 ) {
216
228
if c .IsSet ("name" ) {
217
229
authSource .Name = c .String ("name" )
218
230
}
@@ -232,7 +244,7 @@ func parseAuthSourceLdap(c *cli.Context, authSource *auth.Source) {
232
244
}
233
245
234
246
// 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 {
236
248
if c .IsSet ("name" ) {
237
249
config .Name = c .String ("name" )
238
250
}
@@ -245,7 +257,7 @@ func parseLdapConfig(c *cli.Context, config *ldap.Source) error {
245
257
if c .IsSet ("security-protocol" ) {
246
258
p , ok := findLdapSecurityProtocolByName (c .String ("security-protocol" ))
247
259
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" ))
249
261
}
250
262
config .SecurityProtocol = p
251
263
}
@@ -337,32 +349,27 @@ func findLdapSecurityProtocolByName(name string) (ldap.SecurityProtocol, bool) {
337
349
338
350
// getAuthSource gets the login source by its id defined in the command line flags.
339
351
// 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 ) {
341
353
if err := argsSet (c , "id" ); err != nil {
342
354
return nil , err
343
355
}
344
-
345
356
authSource , err := a .getAuthSourceByID (ctx , c .Int64 ("id" ))
346
357
if err != nil {
347
358
return nil , err
348
359
}
349
360
350
361
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 ())
352
363
}
353
364
354
365
return authSource , nil
355
366
}
356
367
357
368
// 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 {
359
370
if err := argsSet (c , "name" , "security-protocol" , "host" , "port" , "user-search-base" , "user-filter" , "email-attribute" ); err != nil {
360
371
return err
361
372
}
362
-
363
- ctx , cancel := installSignals ()
364
- defer cancel ()
365
-
366
373
if err := a .initDB (ctx ); err != nil {
367
374
return err
368
375
}
@@ -384,10 +391,7 @@ func (a *authService) addLdapBindDn(c *cli.Context) error {
384
391
}
385
392
386
393
// 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 {
391
395
if err := a .initDB (ctx ); err != nil {
392
396
return err
393
397
}
@@ -406,14 +410,11 @@ func (a *authService) updateLdapBindDn(c *cli.Context) error {
406
410
}
407
411
408
412
// 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 {
410
414
if err := argsSet (c , "name" , "security-protocol" , "host" , "port" , "user-dn" , "user-filter" , "email-attribute" ); err != nil {
411
415
return err
412
416
}
413
417
414
- ctx , cancel := installSignals ()
415
- defer cancel ()
416
-
417
418
if err := a .initDB (ctx ); err != nil {
418
419
return err
419
420
}
@@ -435,10 +436,7 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error {
435
436
}
436
437
437
438
// 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 {
442
440
if err := a .initDB (ctx ); err != nil {
443
441
return err
444
442
}
0 commit comments