fix: open Postgres for one-shot console commands
This commit is contained in:
@@ -16,16 +16,14 @@ import (
|
||||
|
||||
// APITokenCommands are the application-wide token create/list/revoke
|
||||
// commands. Domain plugins deliberately do not mint their own credentials.
|
||||
func APITokenCommands(app *backpack.App) []bonfire.Command {
|
||||
withDB := func(ctx context.Context, fn func(*gorm.DB) error) error {
|
||||
if app == nil {
|
||||
return fmt.Errorf("golem15.user: app is nil")
|
||||
}
|
||||
db, ok := app.Lookup[*gorm.DB]()
|
||||
if !ok || db == nil {
|
||||
// withDB is the plugin's database accessor so the commands work both during
|
||||
// serve and as one-shot CLI runs that open Postgres from config.
|
||||
func APITokenCommands(app *backpack.App, withDB func(context.Context, func(*gorm.DB) error) error) []bonfire.Command {
|
||||
open := func(ctx context.Context, fn func(*gorm.DB) error) error {
|
||||
if withDB == nil {
|
||||
return fmt.Errorf("golem15.user: database is not configured")
|
||||
}
|
||||
return fn(db.WithContext(ctx))
|
||||
return withDB(ctx, fn)
|
||||
}
|
||||
return []bonfire.Command{
|
||||
{
|
||||
@@ -54,7 +52,7 @@ func APITokenCommands(app *backpack.App) []bonfire.Command {
|
||||
}
|
||||
expiresAt = &parsed
|
||||
}
|
||||
return withDB(ctx, func(db *gorm.DB) error {
|
||||
return open(ctx, func(db *gorm.DB) error {
|
||||
user, err := classes.LookupByEmail(ctx, db, strings.TrimSpace(email))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -83,7 +81,7 @@ func APITokenCommands(app *backpack.App) []bonfire.Command {
|
||||
{
|
||||
Name: "user:token:list", Description: "List API tokens",
|
||||
Run: func(ctx context.Context, _ bonfire.Input, out bonfire.Output) error {
|
||||
return withDB(ctx, func(db *gorm.DB) error {
|
||||
return open(ctx, func(db *gorm.DB) error {
|
||||
var tokens []models.APIToken
|
||||
if err := db.Order("id").Find(&tokens).Error; err != nil {
|
||||
return fmt.Errorf("user:token:list: %w", err)
|
||||
@@ -116,7 +114,7 @@ func APITokenCommands(app *backpack.App) []bonfire.Command {
|
||||
if err != nil || id == 0 {
|
||||
return fmt.Errorf("user:token:revoke: %q is not a token id", raw)
|
||||
}
|
||||
return withDB(ctx, func(db *gorm.DB) error {
|
||||
return open(ctx, func(db *gorm.DB) error {
|
||||
res := db.Model(&models.APIToken{}).Where("id = ? AND revoked_at IS NULL", id).UpdateColumn("revoked_at", time.Now())
|
||||
if res.Error != nil {
|
||||
return fmt.Errorf("user:token:revoke: %w", res.Error)
|
||||
|
||||
@@ -7,13 +7,12 @@ import (
|
||||
|
||||
"git.golem15.com/golem15/sm-user-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-user-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RequirePasswordChange forces the named account to set a new password on next login.
|
||||
func RequirePasswordChange(app *backpack.App) bonfire.Command {
|
||||
func RequirePasswordChange(withDB func(context.Context, func(*gorm.DB) error) error) bonfire.Command {
|
||||
return bonfire.Command{
|
||||
Name: "user:require-password-change",
|
||||
Description: "Force a user to change their password on next login (D-21)",
|
||||
@@ -28,29 +27,27 @@ func RequirePasswordChange(app *backpack.App) bonfire.Command {
|
||||
if !ok || email == "" {
|
||||
return fmt.Errorf("email is required")
|
||||
}
|
||||
if app == nil {
|
||||
return fmt.Errorf("user:require-password-change: app is nil")
|
||||
}
|
||||
gdb, ok := app.Lookup[*gorm.DB]()
|
||||
if !ok || gdb == nil {
|
||||
if withDB == nil {
|
||||
return fmt.Errorf("user:require-password-change: database is not configured")
|
||||
}
|
||||
user, err := classes.LookupByEmail(ctx, gdb, email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user == nil {
|
||||
return fmt.Errorf("user %s not found", email)
|
||||
}
|
||||
res := gdb.WithContext(ctx).Model(&models.User{}).Where("id = ?", user.ID).Update("must_change_password", true)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected != 1 {
|
||||
return fmt.Errorf("user %s was not updated", email)
|
||||
}
|
||||
out.Success("Password change required for " + user.Email)
|
||||
return nil
|
||||
return withDB(ctx, func(gdb *gorm.DB) error {
|
||||
user, err := classes.LookupByEmail(ctx, gdb, email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user == nil {
|
||||
return fmt.Errorf("user %s not found", email)
|
||||
}
|
||||
res := gdb.WithContext(ctx).Model(&models.User{}).Where("id = ?", user.ID).Update("must_change_password", true)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected != 1 {
|
||||
return fmt.Errorf("user %s was not updated", email)
|
||||
}
|
||||
out.Success("Password change required for " + user.Email)
|
||||
return nil
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user