mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-06 11:39:42 -06:00
[client] Send invite email on user creation
This commit is contained in:
@@ -15,7 +15,7 @@ func SetupCommand() *cobra.Command {
|
||||
Short: "User Management",
|
||||
}
|
||||
cmd.AddCommand([]*cobra.Command{
|
||||
setupUserAddCommand(),
|
||||
setupInviteCommand(),
|
||||
setupUserModCommand(),
|
||||
setupUserListCommand(),
|
||||
setupUserPasswdCommand(),
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
@@ -13,53 +12,24 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core/db"
|
||||
"github.com/shroff/phylum/server/internal/core/fs"
|
||||
"github.com/shroff/phylum/server/internal/core/user"
|
||||
"github.com/shroff/phylum/server/internal/mail"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func setupUserAddCommand() *cobra.Command {
|
||||
func setupInviteCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "add email",
|
||||
Short: "Add User",
|
||||
Use: "invite email",
|
||||
Short: "Invite User",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
email := args[0]
|
||||
|
||||
displayName := email
|
||||
displayName := ""
|
||||
if n, err := cmd.Flags().GetString("name"); err != nil {
|
||||
fmt.Println("invalid value for flag 'name': " + err.Error())
|
||||
os.Exit(1)
|
||||
} else if n != "" {
|
||||
displayName = email
|
||||
}
|
||||
|
||||
password, err := cmd.Flags().GetString("password")
|
||||
if err != nil {
|
||||
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")
|
||||
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")
|
||||
os.Exit(1)
|
||||
}
|
||||
displayName = n
|
||||
}
|
||||
|
||||
f := common.RootFileSystem()
|
||||
@@ -70,9 +40,9 @@ func setupUserAddCommand() *cobra.Command {
|
||||
homePath = strings.TrimRight(basePath, "/") + "/" + email
|
||||
}
|
||||
|
||||
err = db.Get(context.Background()).RunInTx(func(db db.Handler) error {
|
||||
var u user.User
|
||||
err := db.Get(context.Background()).RunInTx(func(db db.Handler) error {
|
||||
userManager := user.ManagerFromDB(db)
|
||||
var userID int32
|
||||
var home fs.Resource
|
||||
if homePath != "" {
|
||||
f = f.WithDb(db)
|
||||
@@ -90,14 +60,14 @@ func setupUserAddCommand() *cobra.Command {
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
if user, err := userManager.CreateUser(email, displayName, password, homeID); err != nil {
|
||||
if user, err := userManager.CreateUser(email, displayName, homeID); err != nil {
|
||||
return err
|
||||
} else {
|
||||
userID = user.ID
|
||||
u = user
|
||||
}
|
||||
|
||||
if homeID.Valid {
|
||||
if _, err := home.UpdatePermissions(userID, fs.PermissionRead|fs.PermissionWrite|fs.PermissionShare); err != nil {
|
||||
if _, err := home.UpdatePermissions(u.ID, fs.PermissionRead|fs.PermissionWrite|fs.PermissionShare); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -105,14 +75,26 @@ func setupUserAddCommand() *cobra.Command {
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("could not add user: " + err.Error())
|
||||
os.Exit(3)
|
||||
fmt.Println("could not create user: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
if sendInvite(u) != nil {
|
||||
fmt.Println("could not send email: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringP("name", "n", "", "Full Name")
|
||||
cmd.Flags().StringP("password", "p", "", "Password")
|
||||
cmd.Flags().StringP("base-dir", "b", "/home", "Base directory for home")
|
||||
cmd.Flags().BoolP("no-create-home", "M", false, "Do not make home directory")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func sendInvite(user user.User) error {
|
||||
return mail.Send(mail.Message{
|
||||
ToName: user.DisplayName,
|
||||
ToEmail: user.Email,
|
||||
Subject: "Create your account",
|
||||
Body: "Create your account",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user