1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-09 10:07:17 +02:00
pocketbase/forms/admin_password_reset_confirm.go

97 lines
2.9 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package forms
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/core"
2022-08-07 14:38:21 +02:00
"github.com/pocketbase/pocketbase/daos"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/pocketbase/forms/validators"
"github.com/pocketbase/pocketbase/models"
)
2022-10-30 10:28:14 +02:00
// AdminPasswordResetConfirm is an admin password reset confirmation form.
2022-07-06 23:19:05 +02:00
type AdminPasswordResetConfirm struct {
2022-10-30 10:28:14 +02:00
app core.App
dao *daos.Dao
2022-07-06 23:19:05 +02:00
Token string `form:"token" json:"token"`
Password string `form:"password" json:"password"`
PasswordConfirm string `form:"passwordConfirm" json:"passwordConfirm"`
}
2022-08-07 14:38:21 +02:00
// NewAdminPasswordResetConfirm creates a new [AdminPasswordResetConfirm]
2022-10-30 10:28:14 +02:00
// form initialized with from the provided [core.App] instance.
2022-08-07 14:38:21 +02:00
//
2022-10-30 10:28:14 +02:00
// If you want to submit the form as part of a transaction,
// you can change the default Dao via [SetDao()].
2022-07-06 23:19:05 +02:00
func NewAdminPasswordResetConfirm(app core.App) *AdminPasswordResetConfirm {
2022-10-30 10:28:14 +02:00
return &AdminPasswordResetConfirm{
app: app,
dao: app.Dao(),
2022-07-06 23:19:05 +02:00
}
2022-10-30 10:28:14 +02:00
}
2022-08-07 14:38:21 +02:00
2022-10-30 10:28:14 +02:00
// SetDao replaces the form Dao instance with the provided one.
//
// This is useful if you want to use a specific transaction Dao instance
// instead of the default app.Dao().
func (form *AdminPasswordResetConfirm) SetDao(dao *daos.Dao) {
form.dao = dao
2022-07-06 23:19:05 +02:00
}
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *AdminPasswordResetConfirm) Validate() error {
return validation.ValidateStruct(form,
validation.Field(&form.Token, validation.Required, validation.By(form.checkToken)),
2022-10-30 10:28:14 +02:00
validation.Field(&form.Password, validation.Required, validation.Length(10, 72)),
2022-07-06 23:19:05 +02:00
validation.Field(&form.PasswordConfirm, validation.Required, validation.By(validators.Compare(form.Password))),
)
}
func (form *AdminPasswordResetConfirm) checkToken(value any) error {
v, _ := value.(string)
if v == "" {
return nil // nothing to check
}
2022-10-30 10:28:14 +02:00
admin, err := form.dao.FindAdminByToken(v, form.app.Settings().AdminPasswordResetToken.Secret)
2022-07-06 23:19:05 +02:00
if err != nil || admin == nil {
return validation.NewError("validation_invalid_token", "Invalid or expired token.")
}
return nil
}
// Submit validates and submits the admin password reset confirmation form.
// On success returns the updated admin model associated to `form.Token`.
//
// You can optionally provide a list of InterceptorFunc to further
// modify the form behavior before persisting it.
func (form *AdminPasswordResetConfirm) Submit(interceptors ...InterceptorFunc[*models.Admin]) (*models.Admin, error) {
2022-07-06 23:19:05 +02:00
if err := form.Validate(); err != nil {
return nil, err
}
2022-10-30 10:28:14 +02:00
admin, err := form.dao.FindAdminByToken(
2022-07-06 23:19:05 +02:00
form.Token,
2022-10-30 10:28:14 +02:00
form.app.Settings().AdminPasswordResetToken.Secret,
2022-07-06 23:19:05 +02:00
)
if err != nil {
return nil, err
}
if err := admin.SetPassword(form.Password); err != nil {
return nil, err
}
interceptorsErr := runInterceptors(admin, func(m *models.Admin) error {
admin = m
return form.dao.SaveAdmin(m)
}, interceptors...)
if interceptorsErr != nil {
return nil, interceptorsErr
2022-07-06 23:19:05 +02:00
}
return admin, nil
}