2022-05-26 19:04:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package forms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-27 18:45:04 +00:00
|
|
|
"strings"
|
2022-05-26 19:04:47 +00:00
|
|
|
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal/validator"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minimumTitleLength = 3
|
|
|
|
maximumTitleLength = 200
|
|
|
|
minimumContentLength = 3
|
|
|
|
maximumContentLength = 8000
|
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
minimumJustificationLen = 3
|
|
|
|
|
2022-05-26 19:04:47 +00:00
|
|
|
threeDays = 3
|
|
|
|
oneWeek = 7
|
|
|
|
twoWeeks = 14
|
|
|
|
threeWeeks = 28
|
|
|
|
)
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
type Form interface {
|
|
|
|
Validate() error
|
|
|
|
Normalize()
|
2022-05-26 19:04:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
func validateUserName(v *validator.Validator, name string, field string) {
|
|
|
|
v.CheckField(validator.NotBlank(name), field, "This field cannot be blank")
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateEmailAddress(v *validator.Validator, address string, field string) {
|
|
|
|
v.CheckField(validator.NotBlank(address), field, "This field cannot be blank")
|
|
|
|
v.CheckField(validator.IsEmail(address), field, "This field must be an email address")
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateReasoning(v *validator.Validator, reasoning string, field string) {
|
|
|
|
v.CheckField(validator.NotBlank(reasoning), field, "This field cannot be blank")
|
|
|
|
v.CheckField(
|
|
|
|
validator.MinChars(reasoning, minimumJustificationLen),
|
|
|
|
field,
|
|
|
|
fmt.Sprintf("This field must be at least %d characters long", minimumJustificationLen),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMotionTitle(v *validator.Validator, title string, field string) {
|
|
|
|
v.CheckField(
|
|
|
|
validator.NotBlank(title),
|
|
|
|
field,
|
2022-05-26 19:04:47 +00:00
|
|
|
"This field cannot be blank",
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
v.CheckField(
|
|
|
|
validator.MinChars(title, minimumTitleLength),
|
|
|
|
field,
|
2022-05-26 19:04:47 +00:00
|
|
|
fmt.Sprintf("This field must be at least %d characters long", minimumTitleLength),
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
v.CheckField(
|
|
|
|
validator.MaxChars(title, maximumTitleLength),
|
|
|
|
field,
|
2022-05-26 19:04:47 +00:00
|
|
|
fmt.Sprintf("This field must be at most %d characters long", maximumTitleLength),
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMotionContent(v *validator.Validator, content string, field string) {
|
|
|
|
v.CheckField(
|
|
|
|
validator.NotBlank(content),
|
|
|
|
field,
|
2022-05-26 19:04:47 +00:00
|
|
|
"This field cannot be blank",
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
v.CheckField(
|
|
|
|
validator.MinChars(content, minimumContentLength),
|
|
|
|
field,
|
2022-05-26 19:04:47 +00:00
|
|
|
fmt.Sprintf("This field must be at least %d characters long", minimumContentLength),
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
v.CheckField(
|
|
|
|
validator.MaxChars(content, maximumContentLength),
|
|
|
|
field,
|
2022-05-26 19:04:47 +00:00
|
|
|
fmt.Sprintf("This field must be at most %d characters long", maximumContentLength),
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type EditMotionForm struct {
|
|
|
|
Title string `form:"title"`
|
|
|
|
Content string `form:"content"`
|
|
|
|
Type *models.VoteType `form:"type"`
|
|
|
|
Due int `form:"due"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EditMotionForm) Validate() error {
|
|
|
|
validateMotionTitle(&f.Validator, f.Title, "title")
|
|
|
|
validateMotionContent(&f.Validator, f.Content, "content")
|
2022-05-29 13:36:27 +00:00
|
|
|
f.CheckField(validator.NotNil(f.Type), "type", "You must choose a valid vote type")
|
2022-05-26 19:04:47 +00:00
|
|
|
|
|
|
|
f.CheckField(validator.PermittedInt(
|
|
|
|
f.Due, threeDays, oneWeek, twoWeeks, threeWeeks), "due", "invalid duration choice",
|
|
|
|
)
|
2022-06-04 17:00:57 +00:00
|
|
|
|
|
|
|
return nil
|
2022-05-26 19:04:47 +00:00
|
|
|
}
|
2022-05-27 15:39:54 +00:00
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
func (f *EditMotionForm) Normalize() {
|
|
|
|
f.Title = strings.TrimSpace(f.Title)
|
|
|
|
f.Content = strings.TrimSpace(f.Content)
|
|
|
|
}
|
|
|
|
|
2022-05-27 15:39:54 +00:00
|
|
|
type DirectVoteForm struct {
|
|
|
|
Choice *models.VoteChoice
|
|
|
|
}
|
2022-05-27 18:45:04 +00:00
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
func (f *DirectVoteForm) Validate() error { return nil }
|
|
|
|
|
|
|
|
func (f *DirectVoteForm) Normalize() {}
|
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
type ProxyVoteForm struct {
|
2022-05-29 13:36:27 +00:00
|
|
|
Voter *models.User `form:"voter"`
|
2022-05-27 18:45:04 +00:00
|
|
|
Choice *models.VoteChoice `form:"choice"`
|
|
|
|
Justification string `form:"justification"`
|
|
|
|
Voters []*models.User `form:"-"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
func (f *ProxyVoteForm) Validate() error {
|
2022-05-29 13:36:27 +00:00
|
|
|
f.CheckField(validator.NotNil(f.Voter), "voter", "Please choose a valid voter")
|
2022-06-04 17:00:57 +00:00
|
|
|
validateReasoning(&f.Validator, f.Justification, "justification")
|
2022-05-27 18:45:04 +00:00
|
|
|
f.CheckField(validator.NotNil(f.Choice), "choice", "A choice has to be made")
|
2022-06-04 17:00:57 +00:00
|
|
|
|
|
|
|
return nil
|
2022-05-27 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ProxyVoteForm) Normalize() {
|
|
|
|
f.Justification = strings.TrimSpace(f.Justification)
|
|
|
|
}
|
2022-06-01 16:57:38 +00:00
|
|
|
|
2022-06-02 21:14:38 +00:00
|
|
|
type EditUserForm struct {
|
|
|
|
User *models.User `form:"-"`
|
|
|
|
MailAddresses []string `form:"-"`
|
|
|
|
AllRoles []*models.Role `form:"-"`
|
|
|
|
Name string `form:"name"`
|
|
|
|
Roles []string `form:"roles"`
|
|
|
|
ReminderMail string `form:"reminder_mail"`
|
|
|
|
Reasoning string `form:"reasoning"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EditUserForm) Validate() error {
|
|
|
|
addresses, err := f.User.EmailAddresses()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while validating form: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
validateUserName(&f.Validator, f.Name, "name")
|
2022-06-02 21:14:38 +00:00
|
|
|
|
|
|
|
f.CheckField(
|
|
|
|
validator.NotBlank(f.ReminderMail),
|
|
|
|
"reminder_mail",
|
|
|
|
"Must choose a reminder mail address",
|
|
|
|
)
|
|
|
|
f.CheckField(
|
|
|
|
validator.PermittedString(f.ReminderMail, addresses...),
|
|
|
|
"reminder_mail",
|
|
|
|
"Reminder mail must be one of the user's email addresses",
|
|
|
|
)
|
|
|
|
|
|
|
|
allRoleNames := make([]string, len(f.AllRoles))
|
|
|
|
for i := range f.AllRoles {
|
|
|
|
allRoleNames[i] = f.AllRoles[i].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
f.CheckField(
|
|
|
|
validator.PermittedStringSet(f.Roles, allRoleNames),
|
|
|
|
"roles",
|
|
|
|
fmt.Sprintf("Roles must only contain values from %s", strings.Join(allRoleNames, ", ")),
|
|
|
|
)
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
validateReasoning(&f.Validator, f.Reasoning, "reasoning")
|
2022-06-02 21:14:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EditUserForm) Normalize() {
|
|
|
|
f.Name = strings.TrimSpace(f.Name)
|
|
|
|
f.Reasoning = strings.TrimSpace(f.Reasoning)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EditUserForm) UpdateUser(edit *models.User) {
|
|
|
|
edit.Reminder.String = f.ReminderMail
|
|
|
|
edit.Name = f.Name
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
type NewUserForm struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
EmailAddress string `form:"email_address"`
|
|
|
|
Reasoning string `form:"reasoning"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NewUserForm) Validate() error {
|
|
|
|
validateUserName(&f.Validator, f.Name, "name")
|
|
|
|
validateEmailAddress(&f.Validator, f.EmailAddress, "email_address")
|
|
|
|
validateReasoning(&f.Validator, f.Reasoning, "reasoning")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NewUserForm) Normalize() {
|
|
|
|
f.Name = strings.TrimSpace(f.Name)
|
|
|
|
f.EmailAddress = strings.TrimSpace(f.EmailAddress)
|
|
|
|
f.Reasoning = strings.TrimSpace(f.Reasoning)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NewUserForm) FillUser() *models.User {
|
|
|
|
return &models.User{Name: f.Name}
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:57:38 +00:00
|
|
|
type DeleteUserForm struct {
|
|
|
|
User *models.User `form:"user"`
|
|
|
|
Reasoning string `form:"reasoning"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
func (f *DeleteUserForm) Validate() error {
|
|
|
|
validateReasoning(&f.Validator, f.Reasoning, "reasoning")
|
|
|
|
|
|
|
|
return nil
|
2022-06-01 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *DeleteUserForm) Normalize() {
|
|
|
|
f.Reasoning = strings.TrimSpace(f.Reasoning)
|
|
|
|
}
|
2022-06-03 18:57:20 +00:00
|
|
|
|
|
|
|
type AddEmailForm struct {
|
|
|
|
EmailAddress string `form:"email_address"`
|
|
|
|
Reasoning string `form:"reasoning"`
|
2022-06-04 17:00:57 +00:00
|
|
|
User *models.User `form:"-"`
|
2022-06-03 18:57:20 +00:00
|
|
|
EmailAddresses []string `form:"-"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
func (f *AddEmailForm) Validate() error {
|
|
|
|
validateEmailAddress(&f.Validator, f.EmailAddress, "email_address")
|
|
|
|
validateReasoning(&f.Validator, f.Reasoning, "reasoning")
|
|
|
|
|
|
|
|
return nil
|
2022-06-03 18:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *AddEmailForm) Normalize() {
|
|
|
|
f.EmailAddress = strings.TrimSpace(f.EmailAddress)
|
|
|
|
f.Reasoning = strings.TrimSpace(f.Reasoning)
|
|
|
|
}
|
2022-06-04 11:53:07 +00:00
|
|
|
|
|
|
|
type DeleteEmailForm struct {
|
|
|
|
EmailAddress string `form:"-"`
|
|
|
|
User *models.User `form:"-"`
|
|
|
|
Reasoning string `form:"reasoning"`
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
2022-06-04 17:00:57 +00:00
|
|
|
func (f *DeleteEmailForm) Validate() error {
|
|
|
|
validateReasoning(&f.Validator, f.Reasoning, "reasoning")
|
|
|
|
|
|
|
|
return nil
|
2022-06-04 11:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *DeleteEmailForm) Normalize() {
|
|
|
|
f.Reasoning = strings.TrimSpace(f.Reasoning)
|
|
|
|
}
|
2022-06-04 17:00:57 +00:00
|
|
|
|
|
|
|
type ChooseVoterForm struct {
|
|
|
|
Reasoning string `form:"reasoning"`
|
|
|
|
VoterIDs []int64 `form:"voters"`
|
|
|
|
Users []*models.User
|
|
|
|
validator.Validator `form:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ChooseVoterForm) Validate() error {
|
|
|
|
validateReasoning(&f.Validator, f.Reasoning, "reasoning")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ChooseVoterForm) Normalize() {
|
|
|
|
}
|