Add make command to create an admin user.
This commit is contained in:
parent
96fc0ea600
commit
5245c9484b
4 changed files with 71 additions and 5 deletions
6
Makefile
6
Makefile
|
|
@ -16,7 +16,11 @@ ent-gen: ## Generate Ent code
|
||||||
|
|
||||||
.PHONY: ent-new
|
.PHONY: ent-new
|
||||||
ent-new: ## Create a new Ent entity (ie, make ent-new NAME=MyEntity)
|
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
|
.PHONY: run
|
||||||
run: ## Run the application
|
run: ## Run the application
|
||||||
|
|
|
||||||
62
cmd/admin/main.go
Normal file
62
cmd/admin/main.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -47,7 +47,7 @@ func (h *Admin) Init(c *services.Container) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Admin) Routes(g *echo.Group) {
|
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 {
|
for _, n := range h.graph.Nodes {
|
||||||
ng := entities.Group(fmt.Sprintf("/%s", strings.ToLower(n.Name)))
|
ng := entities.Group(fmt.Sprintf("/%s", strings.ToLower(n.Name)))
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"entgo.io/ent/schema/field"
|
"entgo.io/ent/schema/field"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/mikestefanello/pagoda/ent/admin"
|
"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/routenames"
|
||||||
"github.com/mikestefanello/pagoda/pkg/ui"
|
"github.com/mikestefanello/pagoda/pkg/ui"
|
||||||
. "github.com/mikestefanello/pagoda/pkg/ui/components"
|
. "github.com/mikestefanello/pagoda/pkg/ui/components"
|
||||||
|
|
@ -26,7 +26,7 @@ func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
|
||||||
|
|
||||||
form := Form(
|
form := Form(
|
||||||
Method(http.MethodPost),
|
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(
|
ControlGroup(
|
||||||
FormButton("is-link", "Delete"),
|
FormButton("is-link", "Delete"),
|
||||||
ButtonLink(
|
ButtonLink(
|
||||||
|
|
@ -176,7 +176,7 @@ func AdminEntityList(ctx echo.Context, params AdminEntityListParams) error {
|
||||||
r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID),
|
r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID),
|
||||||
"is-link",
|
"is-link",
|
||||||
"Edit",
|
"Edit",
|
||||||
), // todo make this easier
|
),
|
||||||
),
|
),
|
||||||
Td(
|
Td(
|
||||||
ButtonLink(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID),
|
ButtonLink(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue