From 5245c9484b448c004c8aec7ef50b94d84d16a73e Mon Sep 17 00:00:00 2001 From: mikestefanello <552328+mikestefanello@users.noreply.github.com> Date: Sun, 20 Apr 2025 10:44:14 -0400 Subject: [PATCH] Add make command to create an admin user. --- Makefile | 6 +++- cmd/admin/main.go | 62 ++++++++++++++++++++++++++++++++++++++++++ pkg/handlers/admin.go | 2 +- pkg/ui/pages/entity.go | 6 ++-- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 cmd/admin/main.go diff --git a/Makefile b/Makefile index a40014c..ffecf14 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,11 @@ ent-gen: ## Generate Ent code .PHONY: ent-new ent-new: ## Create a new Ent entity (ie, make ent-new NAME=MyEntity) - go run entgo.io/ent/cmd/ent new $(name) + go run entgo.io/ent/cmd/ent new $(NAME) + +.PHONY: admin +admin: ## Create a new admin (ie, make admin EMAIL=myemail@web.com) + go run cmd/admin/main.go --email=$(EMAIL) .PHONY: run run: ## Run the application diff --git a/cmd/admin/main.go b/cmd/admin/main.go new file mode 100644 index 0000000..b025d76 --- /dev/null +++ b/cmd/admin/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/mikestefanello/pagoda/pkg/log" + "github.com/mikestefanello/pagoda/pkg/services" +) + +// main creates a new admin user with the email passed in via the flag. +func main() { + // Start a new container. + c := services.NewContainer() + defer func() { + // Gracefully shutdown all services. + if err := c.Shutdown(); err != nil { + log.Default().Error("shutdown failed", "error", err) + } + }() + + var email string + flag.StringVar(&email, "email", "", "email address for the admin user") + flag.Parse() + + if len(email) == 0 { + invalid("email is required") + } + + // Generate a password. + pw, err := c.Auth.RandomToken(10) + if err != nil { + invalid("failed to generate a random password") + } + + err = c.ORM.User. + Create(). + SetEmail(email). + SetName("Admin"). + SetAdmin(true). + SetVerified(true). + SetPassword(pw). + Exec(context.Background()) + + if err != nil { + invalid(err.Error()) + } + + fmt.Println("") + fmt.Println("-- ADMIN USER CREATED --") + fmt.Printf("Email: %s\n", email) + fmt.Printf("Password: %s\n", pw) + fmt.Println("----") + fmt.Println("") +} + +func invalid(msg string) { + fmt.Printf("[ERROR] %s\n", msg) + os.Exit(1) +} diff --git a/pkg/handlers/admin.go b/pkg/handlers/admin.go index c01ab9b..a828f27 100644 --- a/pkg/handlers/admin.go +++ b/pkg/handlers/admin.go @@ -47,7 +47,7 @@ func (h *Admin) Init(c *services.Container) error { } func (h *Admin) Routes(g *echo.Group) { - entities := g.Group("/admin/content", middleware.RequireAdmin) + entities := g.Group("/admin/entity", middleware.RequireAdmin) for _, n := range h.graph.Nodes { ng := entities.Group(fmt.Sprintf("/%s", strings.ToLower(n.Name))) diff --git a/pkg/ui/pages/entity.go b/pkg/ui/pages/entity.go index bbe8867..021fce8 100644 --- a/pkg/ui/pages/entity.go +++ b/pkg/ui/pages/entity.go @@ -10,7 +10,7 @@ import ( "entgo.io/ent/schema/field" "github.com/labstack/echo/v4" "github.com/mikestefanello/pagoda/ent/admin" - "github.com/mikestefanello/pagoda/pkg/pager" // todo make this easier + "github.com/mikestefanello/pagoda/pkg/pager" "github.com/mikestefanello/pagoda/pkg/routenames" "github.com/mikestefanello/pagoda/pkg/ui" . "github.com/mikestefanello/pagoda/pkg/ui/components" @@ -26,7 +26,7 @@ func AdminEntityDelete(ctx echo.Context, entityTypeName string) error { form := Form( Method(http.MethodPost), - H2(Textf("Are you sure you want to delete this %s?", entityTypeName)), + P(Class("subtitle"), Textf("Are you sure you want to delete this %s?", entityTypeName)), ControlGroup( FormButton("is-link", "Delete"), ButtonLink( @@ -176,7 +176,7 @@ func AdminEntityList(ctx echo.Context, params AdminEntityListParams) error { r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID), "is-link", "Edit", - ), // todo make this easier + ), ), Td( ButtonLink(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID),