Files
sm-user-plugin/console/require_password_change.go
Jakub Zych 9986342c23 feat: console help, examples and named loggers (SummerCMS v0.4.0)
Every command gets long help and examples, descriptions in imperative
sentence case, and a warn-only TestCommandsDocumented over bonfire.CheckDocs.
The plugin logs through a diary component logger instead of the process
default logger. Requires SummerCMS v0.4.0; command names and flags unchanged.

user:token:create and user:token:list gain --json, user:backfill-handles a
progress bar; planning IDs leave the descriptions.
2026-10-21 18:03:50 +02:00

61 lines
2.0 KiB
Go

package console
import (
"context"
"fmt"
"strings"
"git.golem15.com/golem15/sm-user-plugin/classes"
"git.golem15.com/golem15/sm-user-plugin/models"
"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(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",
Long: `Marks the account with this e-mail so that it must set a new password at its
next sign-in; setting the password clears the mark. Use it after a suspected
leak or when handing over an account created with a temporary password. It
ends with "Password change required for <email>".`,
Examples: []bonfire.Example{
{Command: "user:require-password-change jane@example.com", Description: "Make Jane set a new password at her next sign-in"},
},
Args: []bonfire.Arg{{
Name: "email",
Description: "Account email",
Required: true,
}},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
email, ok := in.Argument("email")
email = strings.TrimSpace(email)
if !ok || email == "" {
return fmt.Errorf("email is required")
}
if withDB == nil {
return fmt.Errorf("user:require-password-change: database is not configured")
}
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
})
},
}
}