From e0dde8173f0da6cb3628cadca564a219df082d76 Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Sat, 4 May 2019 23:03:10 +0100 Subject: Generate access token in admin cli (#6847) * add cli flag for access token while creating a user --- cmd/admin.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'cmd') diff --git a/cmd/admin.go b/cmd/admin.go index be21ec0f36..ecb4eb48a6 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -73,6 +73,10 @@ var ( Usage: "Length of the random password to be generated", Value: 12, }, + cli.BoolFlag{ + Name: "access-token", + Usage: "Generate access token for the user", + }, }, } @@ -300,7 +304,7 @@ func runCreateUser(c *cli.Context) error { changePassword = c.Bool("must-change-password") } - if err := models.CreateUser(&models.User{ + u := &models.User{ Name: username, Email: c.String("email"), Passwd: password, @@ -308,10 +312,25 @@ func runCreateUser(c *cli.Context) error { IsAdmin: c.Bool("admin"), MustChangePassword: changePassword, Theme: setting.UI.DefaultTheme, - }); err != nil { + } + + if err := models.CreateUser(u); err != nil { return fmt.Errorf("CreateUser: %v", err) } + if c.Bool("access-token") { + t := &models.AccessToken{ + Name: "gitea-admin", + UID: u.ID, + } + + if err := models.NewAccessToken(t); err != nil { + return err + } + + fmt.Printf("Access token was successfully created... %s\n", t.Token) + } + fmt.Printf("New user '%s' has been successfully created!\n", username) return nil } -- cgit v1.2.3