/* 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" "strings" "git.cacert.org/cacert-boardvoting/internal/models" "git.cacert.org/cacert-boardvoting/internal/validator" ) const ( minimumTitleLength = 3 maximumTitleLength = 200 minimumContentLength = 3 maximumContentLength = 8000 minimumJustificationLen = 3 threeDays = 3 oneWeek = 7 twoWeeks = 14 threeWeeks = 28 ) 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() { f.CheckField( validator.NotBlank(f.Title), "title", "This field cannot be blank", ) f.CheckField( validator.MinChars(f.Title, minimumTitleLength), "title", fmt.Sprintf("This field must be at least %d characters long", minimumTitleLength), ) f.CheckField( validator.MaxChars(f.Title, maximumTitleLength), "title", fmt.Sprintf("This field must be at most %d characters long", maximumTitleLength), ) f.CheckField( validator.NotBlank(f.Content), "content", "This field cannot be blank", ) f.CheckField( validator.MinChars(f.Content, minimumContentLength), "content", fmt.Sprintf("This field must be at least %d characters long", minimumContentLength), ) f.CheckField( validator.MaxChars(f.Content, maximumContentLength), "content", fmt.Sprintf("This field must be at most %d characters long", maximumContentLength), ) f.CheckField(validator.NotNil(f.Type), "type", "You must choose a valid vote type") f.CheckField(validator.PermittedInt( f.Due, threeDays, oneWeek, twoWeeks, threeWeeks), "due", "invalid duration choice", ) } func (f *EditMotionForm) Normalize() { f.Title = strings.TrimSpace(f.Title) f.Content = strings.TrimSpace(f.Content) } type DirectVoteForm struct { Choice *models.VoteChoice } type ProxyVoteForm struct { Voter *models.User `form:"voter"` Choice *models.VoteChoice `form:"choice"` Justification string `form:"justification"` Voters []*models.User `form:"-"` validator.Validator `form:"-"` } func (f *ProxyVoteForm) Validate() { f.CheckField(validator.NotNil(f.Voter), "voter", "Please choose a valid voter") f.CheckField(validator.NotBlank(f.Justification), "justification", "This field cannot be blank") f.CheckField( validator.MinChars( f.Justification, minimumJustificationLen, ), "justification", fmt.Sprintf("This field must be at least %d characters long", minimumJustificationLen), ) f.CheckField(validator.NotNil(f.Choice), "choice", "A choice has to be made") } func (f *ProxyVoteForm) Normalize() { f.Justification = strings.TrimSpace(f.Justification) } 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) } f.CheckField(validator.NotBlank(f.Name), "name", "This field cannot be blank") 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, ", ")), ) f.CheckField(validator.NotBlank(f.Reasoning), "reasoning", "This field cannot be blank") f.CheckField( validator.MinChars(f.Reasoning, minimumJustificationLen), "reasoning", fmt.Sprintf("This field must be at least %d characters long", minimumJustificationLen), ) 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 } type DeleteUserForm struct { User *models.User `form:"user"` Reasoning string `form:"reasoning"` validator.Validator `form:"-"` } func (f *DeleteUserForm) Validate() { f.CheckField(validator.NotBlank(f.Reasoning), "reasoning", "This field cannot be blank") f.CheckField( validator.MinChars(f.Reasoning, minimumJustificationLen), "reasoning", fmt.Sprintf("This field must be at least %d characters long", minimumJustificationLen), ) } func (f *DeleteUserForm) Normalize() { f.Reasoning = strings.TrimSpace(f.Reasoning) } type AddEmailForm struct { EmailAddress string `form:"email_address"` Reasoning string `form:"reasoning"` User *models.User `form:"-""` EmailAddresses []string `form:"-"` validator.Validator `form:"-"` } func (f *AddEmailForm) Validate() { f.CheckField(validator.NotBlank(f.EmailAddress), "email_address", "This field cannot be blank") f.CheckField(validator.IsEmail(f.EmailAddress), "email_address", "This field must be an email address") f.CheckField(validator.NotBlank(f.Reasoning), "reasoning", "This field cannot be blank") f.CheckField( validator.MinChars(f.Reasoning, minimumJustificationLen), "reasoning", fmt.Sprintf("This field must be at least %d characters long", minimumJustificationLen), ) } func (f *AddEmailForm) Normalize() { f.EmailAddress = strings.TrimSpace(f.EmailAddress) f.Reasoning = strings.TrimSpace(f.Reasoning) }