[server][cli] Allow setting password during admin user create

This commit is contained in:
Abhishek Shroff
2025-07-08 19:49:26 +05:30
parent 510ab5c013
commit dca4b9ee42
2 changed files with 47 additions and 21 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"codeberg.org/shroff/phylum/server/internal/auth"
"codeberg.org/shroff/phylum/server/internal/core"
"codeberg.org/shroff/phylum/server/internal/db"
"codeberg.org/shroff/phylum/server/internal/mail"
@@ -14,14 +15,24 @@ import (
func setupInviteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "invite email",
Short: "Invite User",
Use: "create <email>",
Short: "Create User",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
email := args[0]
name, _ := cmd.Flags().GetString("name")
noCreateHome, _ := cmd.Flags().GetBool("no-create-home")
noPassword, _ := cmd.Flags().GetBool("no-passwd")
password := ""
if !noPassword {
if p, err := readPassword(); err != nil {
fmt.Println("failed to read password: " + err.Error())
os.Exit(1)
} else {
password = p
}
}
err := db.Get(context.Background()).RunInTx(func(db db.TxHandler) error {
var user core.User
@@ -31,6 +42,12 @@ func setupInviteCommand() *cobra.Command {
user = u
}
if !noPassword {
if err := auth.UpdateUserPassword(db, email, password); err != nil {
return err
}
}
if b, _ := cmd.Flags().GetBool("no-email"); !b {
if err := mail.SendWelcomeEmail(user); err != nil {
fmt.Println("Use --no-email if you want don't want to try sending the welcome email")
@@ -49,7 +66,9 @@ func setupInviteCommand() *cobra.Command {
// TODO: #flags/#config
cmd.Flags().StringP("user_basedir", "b", "", "Base directory for home")
// TODO: #flags/#config
cmd.Flags().BoolP("no-passwd", "P", false, "Do not create a password")
cmd.Flags().BoolP("no-create-home", "M", false, "Do not make home directory")
cmd.Flags().Bool("no-email", false, "Do not send email")
return cmd
}

View File

@@ -2,6 +2,7 @@ package user
import (
"context"
"errors"
"fmt"
"os"
"syscall"
@@ -24,26 +25,9 @@ func setupPasswdCommand() *cobra.Command {
fmt.Println("invalid password: " + err.Error())
os.Exit(1)
} else if password == "" {
os.Stdout.WriteString("Password: ")
bytes, err := term.ReadPassword(syscall.Stdin)
os.Stdout.WriteString("\n")
password, err = readPassword()
if err != nil {
fmt.Println("unable to read password: " + err.Error())
os.Exit(1)
}
password = string(bytes)
os.Stdout.WriteString("Confirm Password: ")
bytes, err = term.ReadPassword(syscall.Stdin)
os.Stdout.WriteString("\n")
if err != nil {
fmt.Println("unable to read password: " + err.Error())
os.Exit(1)
}
passwordConf := string(bytes)
if password != passwordConf {
fmt.Println("password does not match confirmation")
fmt.Println("unable to read password from stdin: " + err.Error())
os.Exit(1)
}
}
@@ -59,3 +43,26 @@ func setupPasswdCommand() *cobra.Command {
cmd.Flags().StringP("password", "p", "", "Password")
return cmd
}
func readPassword() (string, error) {
os.Stdout.WriteString("Password: ")
bytes, err := term.ReadPassword(syscall.Stdin)
os.Stdout.WriteString("\n")
if err != nil {
return "", err
}
password := string(bytes)
os.Stdout.WriteString("Confirm Password: ")
bytes, err = term.ReadPassword(syscall.Stdin)
os.Stdout.WriteString("\n")
if err != nil {
return "", err
}
passwordConf := string(bytes)
if password != passwordConf {
return "", errors.New("password does not match confirmation")
}
return password, nil
}