Force user email to lower case. Include action on all forms.
This commit is contained in:
parent
1ccb0cad1a
commit
eafde27809
14 changed files with 100 additions and 50 deletions
|
|
@ -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,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue