Browse Source

Generate random password (#5023)

* add random-password flag

* run make fmt

* add length cli flag rather than use a default value
for-closed-social
Lanre Adelowo 6 years ago
committed by techknowlogick
parent
commit
e5daa2698f
1 changed files with 32 additions and 2 deletions
  1. +32
    -2
      cmd/admin.go

+ 32
- 2
cmd/admin.go View File

@ -6,6 +6,7 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"text/tabwriter" "text/tabwriter"
@ -13,6 +14,7 @@ import (
"code.gitea.io/git" "code.gitea.io/git"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth/oauth2" "code.gitea.io/gitea/modules/auth/oauth2"
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -59,10 +61,19 @@ var (
Value: "custom/conf/app.ini", Value: "custom/conf/app.ini",
Usage: "Custom configuration file path", Usage: "Custom configuration file path",
}, },
cli.BoolFlag{
Name: "random-password",
Usage: "Generate a random password for the user",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "must-change-password", Name: "must-change-password",
Usage: "Force the user to change his/her password after initial login", Usage: "Force the user to change his/her password after initial login",
}, },
cli.IntFlag{
Name: "random-password-length",
Usage: "Length of the random password to be generated",
Value: 12,
},
}, },
} }
@ -277,10 +288,29 @@ func runChangePassword(c *cli.Context) error {
} }
func runCreateUser(c *cli.Context) error { func runCreateUser(c *cli.Context) error {
if err := argsSet(c, "name", "password", "email"); err != nil {
if err := argsSet(c, "name", "email"); err != nil {
return err return err
} }
if c.IsSet("password") && c.IsSet("random-password") {
return errors.New("cannot set both -random-password and -password flags")
}
var password string
if c.IsSet("password") {
password = c.String("password")
} else if c.IsSet("random-password") {
password, err := generate.GetRandomString(c.Int("random-password-length"))
if err != nil {
return err
}
fmt.Printf("generated random password is '%s'\n", password)
} else {
return errors.New("must set either password or random-password flag")
}
if c.IsSet("config") { if c.IsSet("config") {
setting.CustomConf = c.String("config") setting.CustomConf = c.String("config")
} }
@ -299,7 +329,7 @@ func runCreateUser(c *cli.Context) error {
if err := models.CreateUser(&models.User{ if err := models.CreateUser(&models.User{
Name: c.String("name"), Name: c.String("name"),
Email: c.String("email"), Email: c.String("email"),
Passwd: c.String("password"),
Passwd: password,
IsActive: true, IsActive: true,
IsAdmin: c.Bool("admin"), IsAdmin: c.Bool("admin"),
MustChangePassword: changePassword, MustChangePassword: changePassword,

Loading…
Cancel
Save