2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package form // import "miniflux.app/v2/internal/ui/form"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-11-28 06:30:04 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/errors"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// UserForm represents the user form.
|
2017-11-20 06:10:04 +01:00
|
|
|
type UserForm struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Confirmation string
|
|
|
|
IsAdmin bool
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// ValidateCreation validates user creation.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u UserForm) ValidateCreation() error {
|
|
|
|
if u.Username == "" || u.Password == "" || u.Confirmation == "" {
|
2018-09-22 03:53:29 +02:00
|
|
|
return errors.NewLocalizedError("error.fields_mandatory")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password != u.Confirmation {
|
2018-09-22 03:53:29 +02:00
|
|
|
return errors.NewLocalizedError("error.different_passwords")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// ValidateModification validates user modification.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u UserForm) ValidateModification() error {
|
|
|
|
if u.Username == "" {
|
2018-09-22 03:53:29 +02:00
|
|
|
return errors.NewLocalizedError("error.user_mandatory_fields")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password != "" {
|
|
|
|
if u.Password != u.Confirmation {
|
2018-09-22 03:53:29 +02:00
|
|
|
return errors.NewLocalizedError("error.different_passwords")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.Password) < 6 {
|
2018-09-22 03:53:29 +02:00
|
|
|
return errors.NewLocalizedError("error.password_min_length")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// Merge updates the fields of the given user.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (u UserForm) Merge(user *model.User) *model.User {
|
|
|
|
user.Username = u.Username
|
|
|
|
user.IsAdmin = u.IsAdmin
|
|
|
|
|
|
|
|
if u.Password != "" {
|
|
|
|
user.Password = u.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// NewUserForm returns a new UserForm.
|
2017-11-20 06:10:04 +01:00
|
|
|
func NewUserForm(r *http.Request) *UserForm {
|
|
|
|
return &UserForm{
|
|
|
|
Username: r.FormValue("username"),
|
|
|
|
Password: r.FormValue("password"),
|
|
|
|
Confirmation: r.FormValue("confirmation"),
|
|
|
|
IsAdmin: r.FormValue("is_admin") == "1",
|
|
|
|
}
|
|
|
|
}
|