2017-05-07 00:10:40 +02:00
package form
2017-05-06 21:21:39 +02:00
import (
"regexp"
2017-05-07 10:25:09 +02:00
2017-05-06 21:21:39 +02:00
"github.com/ewhal/nyaa/util/log"
)
const EMAIL_REGEX = ` (\w[-._\w]*\w@\w[-._\w]*\w\.\w { 2,3}) `
2017-05-07 05:04:55 +02:00
const USERNAME_REGEX = ` (\W) `
2017-05-06 21:21:39 +02:00
func EmailValidation ( email string ) bool {
exp , err := regexp . Compile ( EMAIL_REGEX )
if regexpCompiled := log . CheckError ( err ) ; regexpCompiled {
if exp . MatchString ( email ) {
return true
}
}
return false
}
2017-05-07 05:04:55 +02:00
func ValidateUsername ( username string ) bool {
2017-05-07 11:08:44 +02:00
exp , err := regexp . Compile ( USERNAME_REGEX )
if username == "" {
return false
}
if ( len ( username ) < 3 ) || ( len ( username ) > 15 ) {
return false
}
if regexpCompiled := log . CheckError ( err ) ; regexpCompiled {
2017-05-07 05:04:55 +02:00
if exp . MatchString ( username ) {
return false
}
} else {
return false
}
2017-05-07 11:08:44 +02:00
return true
2017-05-07 05:04:55 +02:00
}
2017-05-06 21:21:39 +02:00
// RegistrationForm is used when creating a user.
type RegistrationForm struct {
2017-05-07 14:53:01 +02:00
Username string ` form:"registrationUsername" `
Email string ` form:"registrationEmail" `
Password string ` form:"registrationPassword" `
CaptchaID string ` form:"captchaID" inmodel:"false" `
2017-05-06 21:21:39 +02:00
}
// RegistrationForm is used when creating a user authentication.
type LoginForm struct {
2017-05-07 14:53:01 +02:00
Email string ` form:"email" `
Password string ` form:"password" `
2017-05-06 21:21:39 +02:00
}
// UserForm is used when updating a user.
type UserForm struct {
2017-05-07 14:53:01 +02:00
Email string ` form:"email" `
2017-05-06 21:21:39 +02:00
}
// PasswordForm is used when updating a user password.
type PasswordForm struct {
2017-05-07 14:53:01 +02:00
CurrentPassword string ` form:"currentPassword" `
Password string ` form:"newPassword" `
2017-05-06 21:21:39 +02:00
}
// SendPasswordResetForm is used when sending a password reset token.
type SendPasswordResetForm struct {
2017-05-07 14:53:01 +02:00
Email string ` form:"email" `
2017-05-06 21:21:39 +02:00
}
// PasswordResetForm is used when reseting a password.
type PasswordResetForm struct {
2017-05-07 14:53:01 +02:00
PasswordResetToken string ` form:"token" `
Password string ` form:"newPassword" `
2017-05-06 21:21:39 +02:00
}
// VerifyEmailForm is used when verifying an email.
type VerifyEmailForm struct {
2017-05-07 14:53:01 +02:00
ActivationToken string ` form:"token" `
2017-05-06 21:21:39 +02:00
}
// ActivateForm is used when activating user.
type ActivateForm struct {
2017-05-07 14:53:01 +02:00
Activation bool ` form:"activation" `
2017-05-06 21:21:39 +02:00
}
// UserRoleForm is used when adding or removing a role from a user.
type UserRoleForm struct {
2017-05-07 14:53:01 +02:00
UserId int ` form:"userId" `
RoleId int ` form:"roleId" `
2017-05-06 21:21:39 +02:00
}