2023-04-20 16:20:56 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-04-20 22:39:48 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-04-20 16:20:56 +02:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/go-ozzo/ozzo-validation/v4/is"
|
|
|
|
"github.com/pocketbase/pocketbase/core"
|
|
|
|
"github.com/pocketbase/pocketbase/models"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAdminCommand creates and returns new command for managing
|
|
|
|
// admin accounts (create, update, delete).
|
|
|
|
func NewAdminCommand(app core.App) *cobra.Command {
|
|
|
|
command := &cobra.Command{
|
|
|
|
Use: "admin",
|
|
|
|
Short: "Manages admin accounts",
|
|
|
|
}
|
|
|
|
|
|
|
|
command.AddCommand(adminCreateCommand(app))
|
|
|
|
command.AddCommand(adminUpdateCommand(app))
|
|
|
|
command.AddCommand(adminDeleteCommand(app))
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
|
|
|
func adminCreateCommand(app core.App) *cobra.Command {
|
|
|
|
command := &cobra.Command{
|
|
|
|
Use: "create",
|
|
|
|
Example: "admin create test@example.com 1234567890",
|
|
|
|
Short: "Creates a new admin account",
|
2023-04-20 22:39:48 +02:00
|
|
|
// prevents printing the error log twice
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2023-04-20 16:20:56 +02:00
|
|
|
if len(args) != 2 {
|
2023-04-20 22:39:48 +02:00
|
|
|
return errors.New("Missing email and password arguments.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-20 22:39:48 +02:00
|
|
|
if args[0] == "" || is.EmailFormat.Validate(args[0]) != nil {
|
|
|
|
return errors.New("Missing or invalid email address.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args[1]) < 8 {
|
2023-04-20 22:39:48 +02:00
|
|
|
return errors.New("The password must be at least 8 chars long.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
admin := &models.Admin{}
|
|
|
|
admin.Email = args[0]
|
|
|
|
admin.SetPassword(args[1])
|
|
|
|
|
|
|
|
if err := app.Dao().SaveAdmin(admin); err != nil {
|
2023-04-20 22:39:48 +02:00
|
|
|
return fmt.Errorf("Failed to create new admin account: %v", err)
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
color.Green("Successfully created new admin %s!", admin.Email)
|
2023-04-20 22:39:48 +02:00
|
|
|
return nil
|
2023-04-20 16:20:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
|
|
|
func adminUpdateCommand(app core.App) *cobra.Command {
|
|
|
|
command := &cobra.Command{
|
|
|
|
Use: "update",
|
|
|
|
Example: "admin update test@example.com 1234567890",
|
|
|
|
Short: "Changes the password of a single admin account",
|
2023-04-20 22:39:48 +02:00
|
|
|
// prevents printing the error log twice
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2023-04-20 16:20:56 +02:00
|
|
|
if len(args) != 2 {
|
2023-04-20 22:39:48 +02:00
|
|
|
return errors.New("Missing email and password arguments.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-20 22:39:48 +02:00
|
|
|
if args[0] == "" || is.EmailFormat.Validate(args[0]) != nil {
|
|
|
|
return errors.New("Missing or invalid email address.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args[1]) < 8 {
|
2023-04-20 22:39:48 +02:00
|
|
|
return errors.New("The new password must be at least 8 chars long.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
admin, err := app.Dao().FindAdminByEmail(args[0])
|
|
|
|
if err != nil {
|
2023-04-20 22:39:48 +02:00
|
|
|
return fmt.Errorf("Admin with email %s doesn't exist.", args[0])
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
admin.SetPassword(args[1])
|
|
|
|
|
|
|
|
if err := app.Dao().SaveAdmin(admin); err != nil {
|
2023-04-20 22:39:48 +02:00
|
|
|
return fmt.Errorf("Failed to change admin %s password: %v", admin.Email, err)
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
color.Green("Successfully changed admin %s password!", admin.Email)
|
2023-04-20 22:39:48 +02:00
|
|
|
return nil
|
2023-04-20 16:20:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
|
|
|
func adminDeleteCommand(app core.App) *cobra.Command {
|
|
|
|
command := &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Example: "admin delete test@example.com",
|
|
|
|
Short: "Deletes an existing admin account",
|
2023-04-20 22:39:48 +02:00
|
|
|
// prevents printing the error log twice
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 || args[0] == "" || is.EmailFormat.Validate(args[0]) != nil {
|
|
|
|
return errors.New("Invalid or missing email address.")
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
admin, err := app.Dao().FindAdminByEmail(args[0])
|
|
|
|
if err != nil {
|
|
|
|
color.Yellow("Admin %s is already deleted.", args[0])
|
2023-04-20 22:39:48 +02:00
|
|
|
return nil
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Dao().DeleteAdmin(admin); err != nil {
|
2023-04-20 22:39:48 +02:00
|
|
|
return fmt.Errorf("Failed to delete admin %s: %v", admin.Email, err)
|
2023-04-20 16:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
color.Green("Successfully deleted admin %s!", admin.Email)
|
2023-04-20 22:39:48 +02:00
|
|
|
return nil
|
2023-04-20 16:20:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|