diff options
author | 2017-11-19 21:10:04 -0800 | |
---|---|---|
committer | 2017-11-19 22:01:46 -0800 | |
commit | 8ffb773f43c8dc54801ca1d111854e7e881c93c9 (patch) | |
tree | 38133a2fc612597a75fed1d13e5b4042f58a2b7e /server/ui/form/user.go | |
download | v2-8ffb773f43c8dc54801ca1d111854e7e881c93c9.tar.gz v2-8ffb773f43c8dc54801ca1d111854e7e881c93c9.tar.zst v2-8ffb773f43c8dc54801ca1d111854e7e881c93c9.zip |
First commit
Diffstat (limited to 'server/ui/form/user.go')
-rw-r--r-- | server/ui/form/user.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/server/ui/form/user.go b/server/ui/form/user.go new file mode 100644 index 00000000..1197b484 --- /dev/null +++ b/server/ui/form/user.go @@ -0,0 +1,80 @@ +// 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. + +package form + +import ( + "errors" + "github.com/miniflux/miniflux2/model" + "net/http" +) + +type UserForm struct { + Username string + Password string + Confirmation string + IsAdmin bool +} + +func (u UserForm) ValidateCreation() error { + if u.Username == "" || u.Password == "" || u.Confirmation == "" { + return errors.New("All fields are mandatory.") + } + + if u.Password != u.Confirmation { + return errors.New("Passwords are not the same.") + } + + if len(u.Password) < 6 { + return errors.New("You must use at least 6 characters.") + } + + return nil +} + +func (u UserForm) ValidateModification() error { + if u.Username == "" { + return errors.New("The username is mandatory.") + } + + if u.Password != "" { + if u.Password != u.Confirmation { + return errors.New("Passwords are not the same.") + } + + if len(u.Password) < 6 { + return errors.New("You must use at least 6 characters.") + } + } + + return nil +} + +func (u UserForm) ToUser() *model.User { + return &model.User{ + Username: u.Username, + Password: u.Password, + IsAdmin: u.IsAdmin, + } +} + +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 +} + +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", + } +} |