Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support cmd command to reset to custom password #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions cmd/resetpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,51 @@ var (
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var users []db.User
if err := db.DB(ctx).Find(&users).Error; err != nil {
logrus.Fatalln(err)
if userName != "" {
// search for user
if err := db.DB(ctx).Where("username = ?", userName).Find(&users).Error; err != nil {
logrus.Warnf("query username:[%v] with error: [%v] change to reset all users\n", userName, err)
}
}
if len(users) == 0 {
if err := db.DB(ctx).Find(&users).Error; err != nil {
logrus.Fatalln(err)
}
}
// double check if no user get
if len(users) == 0 {
logrus.Fatalln("unexpected error: No user found")
}
for _, u := range users {
password := gonanoid.Must(8)
if _, err := graphql.UpdatePassword(ctx, &struct {
CurrentPassword string
NewPassword string
}{
NewPassword: password,
}, &u, true); err != nil {
logrus.Fatalf("Username: %v: %v", u.Username, err)
if newUserPassword == "" {
password := gonanoid.Must(8)
if _, err := graphql.UpdatePassword(ctx, &struct {
CurrentPassword string
NewPassword string
}{
NewPassword: password,
}, &u, true); err != nil {
logrus.Fatalf("Username: %v: %v", u.Username, err)
}
fmt.Printf("Username: %v, Password: %v\n", u.Username, password)
} else {
if _, err := graphql.UpdatePassword(ctx, &struct {
CurrentPassword string
NewPassword string
}{
NewPassword: newUserPassword,
}, &u, true); err != nil {
logrus.Fatalf("Username: %v: %v", u.Username, err)
}
fmt.Printf("Username: %v, Password: %v\n", u.Username, newUserPassword)
}
fmt.Printf("Username: %v, Password: %v\n", u.Username, password)
}

},
}
)

func init() {
resetpassCmd.PersistentFlags().StringVarP(&cfgDir, "config", "c", filepath.Join("/etc", db.AppName), "config directory")
resetpassCmd.PersistentFlags().StringVarP(&userName, "username", "u", "", "user name")
resetpassCmd.PersistentFlags().StringVarP(&newUserPassword, "password", "p", "", "user new password")
}
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var (
disableTimestamp bool
listen string
apiOnly bool
newUserPassword string
userName string

runCmd = &cobra.Command{
Use: "run",
Expand Down