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

106 lines
3.2 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-08-07 14:38:21 +02:00
// AdminPasswordResetConfirm specifies an admin password reset confirmation form.
2022-07-06 23:19:05 +02:00
type AdminPasswordResetConfirm struct {
2022-08-07 14:38:21 +02:00
config AdminPasswordResetConfirmConfig
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
// AdminPasswordResetConfirmConfig is the [AdminPasswordResetConfirm] factory initializer config.
//
// NB! App is required struct member.
type AdminPasswordResetConfirmConfig struct {
App core.App
Dao *daos.Dao
2022-08-07 14:38:21 +02:00
}
// NewAdminPasswordResetConfirm creates a new [AdminPasswordResetConfirm]
// form with initializer config created from the provided [core.App] instance.
//
// If you want to submit the form as part of another transaction, use
// [NewAdminPasswordResetConfirmWithConfig] with explicitly set Dao.
2022-07-06 23:19:05 +02:00
func NewAdminPasswordResetConfirm(app core.App) *AdminPasswordResetConfirm {
2022-08-07 14:38:21 +02:00
return NewAdminPasswordResetConfirmWithConfig(AdminPasswordResetConfirmConfig{
App: app,
})
}
// NewAdminPasswordResetConfirmWithConfig creates a new [AdminPasswordResetConfirm]
// form with the provided config or panics on invalid configuration.
func NewAdminPasswordResetConfirmWithConfig(config AdminPasswordResetConfirmConfig) *AdminPasswordResetConfirm {
form := &AdminPasswordResetConfirm{config: config}
if form.config.App == nil {
panic("Missing required config.App instance.")
}
if form.config.Dao == nil {
form.config.Dao = form.config.App.Dao()
2022-07-06 23:19:05 +02:00
}
2022-08-07 14:38:21 +02:00
return form
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)),
validation.Field(&form.Password, validation.Required, validation.Length(10, 100)),
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
}
admin, err := form.config.Dao.FindAdminByToken(
2022-07-06 23:19:05 +02:00
v,
2022-08-07 14:38:21 +02:00
form.config.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`.
func (form *AdminPasswordResetConfirm) Submit() (*models.Admin, error) {
if err := form.Validate(); err != nil {
return nil, err
}
admin, err := form.config.Dao.FindAdminByToken(
2022-07-06 23:19:05 +02:00
form.Token,
2022-08-07 14:38:21 +02:00
form.config.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
}
if err := form.config.Dao.SaveAdmin(admin); err != nil {
2022-07-06 23:19:05 +02:00
return nil, err
}
return admin, nil
}