Commit eef4b6e1 authored by ale's avatar ale

Split availability check from user validation too

And fix an issue where validHostedEmail() was no longer checking for
availability in email alias validation.
parent 0bb4fb1e
Pipeline #5065 passed with stages
in 4 minutes and 9 seconds
......@@ -181,7 +181,7 @@ func (r *CreateUserRequest) Validate(rctx *RequestContext) error {
return err
}
}
if err := rctx.userValidator(rctx.Context, r.User); err != nil {
if err := rctx.userValidator(rctx.Context, r.User, true); err != nil {
log.Printf("validation error while creating user %+v: %v", r.User, err)
return err
}
......
......@@ -324,7 +324,7 @@ func (r *AddEmailAliasRequest) Validate(rctx *RequestContext) error {
return errors.New("too many aliases")
}
if err := rctx.fieldValidators.email(rctx.Context, r.Addr); err != nil {
if err := rctx.fieldValidators.newEmail(rctx.Context, r.Addr); err != nil {
return newValidationError(nil, "addr", err.Error())
}
return nil
......
......@@ -452,6 +452,13 @@ func (v *validationContext) validHostedEmail() ValidatorFunc {
)
}
func (v *validationContext) validHostedNewEmail() ValidatorFunc {
return allOf(
v.validHostedEmail(),
v.isAvailableEmailAddr(),
)
}
func (v *validationContext) validHostedMailingList() ValidatorFunc {
return validateUsernameAndDomain(
allOf(
......@@ -532,7 +539,7 @@ func (v *validationContext) validateShardedResource(ctx context.Context, r *Reso
func (v *validationContext) validEmailResource() ResourceValidatorFunc {
emailValidator := v.validHostedEmail()
newEmailValidator := allOf(emailValidator, v.isAvailableEmailAddr())
newEmailValidator := v.validHostedNewEmail()
return func(ctx context.Context, r *Resource, user *User, isNew bool) error {
if err := v.validateShardedResource(ctx, r, user); err != nil {
......@@ -873,18 +880,18 @@ func (v *resourceValidator) validateResource(ctx context.Context, r *Resource, u
// Common validators for specific field types.
type fieldValidators struct {
password ValidatorFunc
email ValidatorFunc
newEmail ValidatorFunc
}
func newFieldValidators(v *validationContext) *fieldValidators {
return &fieldValidators{
password: v.validPassword(),
email: v.validHostedEmail(),
newEmail: v.validHostedNewEmail(),
}
}
// UserValidatorFunc is a compound validator for User objects.
type UserValidatorFunc func(context.Context, *User) error
type UserValidatorFunc func(context.Context, *User, bool) error
// Verify that user-level invariants are respected. This check can be applied
// to new or existing objects.
......@@ -935,8 +942,15 @@ func checkUserInvariants(user *User) error {
// A custom validator for new User objects.
func (v *validationContext) validUser() UserValidatorFunc {
nameValidator := v.validHostedEmail()
return func(ctx context.Context, user *User) error {
if err := nameValidator(ctx, user.Name); err != nil {
newNameValidator := v.validHostedNewEmail()
return func(ctx context.Context, user *User, isNew bool) error {
var err error
if isNew {
err = newNameValidator(ctx, user.Name)
} else {
err = nameValidator(ctx, user.Name)
}
if err != nil {
return err
}
return checkUserInvariants(user)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment