2017-11-20 00:10:04 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package form // import "miniflux.app/ui/form"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-11-28 00:30:04 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/errors"
|
|
|
|
"miniflux.app/model"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UserForm represents the user form.
|
2017-11-20 00:10:04 -05:00
|
|
|
type UserForm struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Confirmation string
|
|
|
|
IsAdmin bool
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// ValidateCreation validates user creation.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (u UserForm) ValidateCreation() error {
|
|
|
|
if u.Username == "" || u.Password == "" || u.Confirmation == "" {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("All fields are mandatory.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password != u.Confirmation {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("Passwords are not the same.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.Password) < 6 {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("You must use at least 6 characters.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// ValidateModification validates user modification.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (u UserForm) ValidateModification() error {
|
|
|
|
if u.Username == "" {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("The username is mandatory.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password != "" {
|
|
|
|
if u.Password != u.Confirmation {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("Passwords are not the same.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.Password) < 6 {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("You must use at least 6 characters.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// ToUser returns a User from the form values.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (u UserForm) ToUser() *model.User {
|
|
|
|
return &model.User{
|
|
|
|
Username: u.Username,
|
|
|
|
Password: u.Password,
|
|
|
|
IsAdmin: u.IsAdmin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Merge updates the fields of the given user.
|
2017-11-20 00:10:04 -05: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 00:30:04 -05:00
|
|
|
// NewUserForm returns a new UserForm.
|
2017-11-20 00:10:04 -05: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",
|
|
|
|
}
|
|
|
|
}
|