Force user email to lower case. Include action on all forms.

This commit is contained in:
mikestefanello 2021-12-25 21:30:22 -05:00
parent 1ccb0cad1a
commit eafde27809
14 changed files with 100 additions and 50 deletions

View file

@ -338,5 +338,6 @@ func (c *UserClient) QueryOwner(u *User) *PasswordTokenQuery {
// Hooks returns the client hooks.
func (c *UserClient) Hooks() []Hook {
return c.hooks.User
hooks := c.hooks.User
return append(hooks[:len(hooks):len(hooks)], user.Hooks[:]...)
}

View file

@ -2,43 +2,4 @@
package ent
import (
"goweb/ent/passwordtoken"
"goweb/ent/schema"
"goweb/ent/user"
"time"
)
// The init function reads all schema descriptors with runtime code
// (default values, validators, hooks and policies) and stitches it
// to their package variables.
func init() {
passwordtokenFields := schema.PasswordToken{}.Fields()
_ = passwordtokenFields
// passwordtokenDescHash is the schema descriptor for hash field.
passwordtokenDescHash := passwordtokenFields[0].Descriptor()
// passwordtoken.HashValidator is a validator for the "hash" field. It is called by the builders before save.
passwordtoken.HashValidator = passwordtokenDescHash.Validators[0].(func(string) error)
// passwordtokenDescCreatedAt is the schema descriptor for created_at field.
passwordtokenDescCreatedAt := passwordtokenFields[1].Descriptor()
// passwordtoken.DefaultCreatedAt holds the default value on creation for the created_at field.
passwordtoken.DefaultCreatedAt = passwordtokenDescCreatedAt.Default.(func() time.Time)
userFields := schema.User{}.Fields()
_ = userFields
// userDescName is the schema descriptor for name field.
userDescName := userFields[0].Descriptor()
// user.NameValidator is a validator for the "name" field. It is called by the builders before save.
user.NameValidator = userDescName.Validators[0].(func(string) error)
// userDescEmail is the schema descriptor for email field.
userDescEmail := userFields[1].Descriptor()
// user.EmailValidator is a validator for the "email" field. It is called by the builders before save.
user.EmailValidator = userDescEmail.Validators[0].(func(string) error)
// userDescPassword is the schema descriptor for password field.
userDescPassword := userFields[2].Descriptor()
// user.PasswordValidator is a validator for the "password" field. It is called by the builders before save.
user.PasswordValidator = userDescPassword.Validators[0].(func(string) error)
// userDescCreatedAt is the schema descriptor for created_at field.
userDescCreatedAt := userFields[3].Descriptor()
// user.DefaultCreatedAt holds the default value on creation for the created_at field.
user.DefaultCreatedAt = userDescCreatedAt.Default.(func() time.Time)
}
// The schema-stitching logic is generated in goweb/ent/runtime/runtime.go

View file

@ -2,7 +2,48 @@
package runtime
// The schema-stitching logic is generated in goweb/ent/runtime.go
import (
"goweb/ent/passwordtoken"
"goweb/ent/schema"
"goweb/ent/user"
"time"
)
// The init function reads all schema descriptors with runtime code
// (default values, validators, hooks and policies) and stitches it
// to their package variables.
func init() {
passwordtokenFields := schema.PasswordToken{}.Fields()
_ = passwordtokenFields
// passwordtokenDescHash is the schema descriptor for hash field.
passwordtokenDescHash := passwordtokenFields[0].Descriptor()
// passwordtoken.HashValidator is a validator for the "hash" field. It is called by the builders before save.
passwordtoken.HashValidator = passwordtokenDescHash.Validators[0].(func(string) error)
// passwordtokenDescCreatedAt is the schema descriptor for created_at field.
passwordtokenDescCreatedAt := passwordtokenFields[1].Descriptor()
// passwordtoken.DefaultCreatedAt holds the default value on creation for the created_at field.
passwordtoken.DefaultCreatedAt = passwordtokenDescCreatedAt.Default.(func() time.Time)
userHooks := schema.User{}.Hooks()
user.Hooks[0] = userHooks[0]
userFields := schema.User{}.Fields()
_ = userFields
// userDescName is the schema descriptor for name field.
userDescName := userFields[0].Descriptor()
// user.NameValidator is a validator for the "name" field. It is called by the builders before save.
user.NameValidator = userDescName.Validators[0].(func(string) error)
// userDescEmail is the schema descriptor for email field.
userDescEmail := userFields[1].Descriptor()
// user.EmailValidator is a validator for the "email" field. It is called by the builders before save.
user.EmailValidator = userDescEmail.Validators[0].(func(string) error)
// userDescPassword is the schema descriptor for password field.
userDescPassword := userFields[2].Descriptor()
// user.PasswordValidator is a validator for the "password" field. It is called by the builders before save.
user.PasswordValidator = userDescPassword.Validators[0].(func(string) error)
// userDescCreatedAt is the schema descriptor for created_at field.
userDescCreatedAt := userFields[3].Descriptor()
// user.DefaultCreatedAt holds the default value on creation for the created_at field.
user.DefaultCreatedAt = userDescCreatedAt.Default.(func() time.Time)
}
const (
Version = "v0.9.1" // Version of ent codegen.

View file

@ -1,8 +1,13 @@
package schema
import (
"context"
"strings"
"time"
ge "goweb/ent"
"goweb/ent/hook"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
@ -37,3 +42,20 @@ func (User) Edges() []ent.Edge {
Ref("user"),
}
}
func (User) Hooks() []ent.Hook {
return []ent.Hook{
hook.On(
func(next ent.Mutator) ent.Mutator {
return hook.UserFunc(func(ctx context.Context, m *ge.UserMutation) (ent.Value, error) {
if v, exists := m.Email(); exists {
m.SetEmail(strings.ToLower(v))
}
return next.Mutate(ctx, m)
})
},
// Limit the hook only for these operations.
ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne,
),
}
}

View file

@ -4,6 +4,8 @@ package user
import (
"time"
"entgo.io/ent"
)
const (
@ -51,7 +53,14 @@ func ValidColumn(column string) bool {
return false
}
// Note that the variables below are initialized by the runtime
// package on the initialization of the application. Therefore,
// it should be imported in the main as follows:
//
// import _ "goweb/ent/runtime"
//
var (
Hooks [1]ent.Hook
// NameValidator is a validator for the "name" field. It is called by the builders before save.
NameValidator func(string) error
// EmailValidator is a validator for the "email" field. It is called by the builders before save.

View file

@ -79,7 +79,9 @@ func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
err error
node *User
)
uc.defaults()
if err := uc.defaults(); err != nil {
return nil, err
}
if len(uc.hooks) == 0 {
if err = uc.check(); err != nil {
return nil, err
@ -138,11 +140,15 @@ func (uc *UserCreate) ExecX(ctx context.Context) {
}
// defaults sets the default values of the builder before save.
func (uc *UserCreate) defaults() {
func (uc *UserCreate) defaults() error {
if _, ok := uc.mutation.CreatedAt(); !ok {
if user.DefaultCreatedAt == nil {
return fmt.Errorf("ent: uninitialized user.DefaultCreatedAt (forgotten import ent/runtime?)")
}
v := user.DefaultCreatedAt()
uc.mutation.SetCreatedAt(v)
}
return nil
}
// check runs all checks and user-defined validators on the builder.