2023-05-20 20:15:57 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2023-08-19 04:52:15 +02:00
|
|
|
"net/mail"
|
|
|
|
"regexp"
|
2023-04-13 00:07:08 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"entgo.io/ent"
|
2023-08-19 04:52:15 +02:00
|
|
|
"entgo.io/ent/schema/edge"
|
2023-04-13 00:07:08 +02:00
|
|
|
"entgo.io/ent/schema/field"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// User holds the schema definition for the User entity.
|
|
|
|
type User struct {
|
|
|
|
ent.Schema
|
|
|
|
}
|
|
|
|
|
2023-08-19 04:52:15 +02:00
|
|
|
const usernameRegex = `(^[\S]+)([A-Za-z1-9<>~+_-])$`
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// Fields of the User.
|
|
|
|
func (User) Fields() []ent.Field {
|
|
|
|
return []ent.Field{
|
|
|
|
field.UUID("id", uuid.UUID{}).
|
|
|
|
Default(uuid.New).
|
|
|
|
Unique().
|
|
|
|
Immutable(),
|
|
|
|
field.String("username").
|
|
|
|
NotEmpty().
|
2023-08-19 04:52:15 +02:00
|
|
|
Unique().
|
|
|
|
MinLen(2).
|
|
|
|
Match(regexp.MustCompile(usernameRegex)).
|
|
|
|
Comment("user's handle, allowed `<>~+-_` symbols and alphanumeric ASCII characters, including capital letters"),
|
2023-05-01 22:48:11 +02:00
|
|
|
field.String("email").
|
|
|
|
NotEmpty().
|
2023-08-19 04:52:15 +02:00
|
|
|
Unique().
|
|
|
|
Validate(func(email string) error {
|
|
|
|
_, err := mail.ParseAddress(email)
|
|
|
|
return err
|
|
|
|
}),
|
2023-05-03 06:30:12 +02:00
|
|
|
field.Bytes("password").
|
2023-04-13 00:07:08 +02:00
|
|
|
Sensitive().
|
|
|
|
NotEmpty(),
|
|
|
|
field.Bool("is_admin").
|
|
|
|
Default(false),
|
|
|
|
field.Bool("is_active").
|
|
|
|
Default(true),
|
|
|
|
field.Time("created_at").
|
|
|
|
Default(time.Now).
|
|
|
|
Immutable(),
|
|
|
|
field.Time("updated_at").
|
|
|
|
Default(time.Now).
|
|
|
|
UpdateDefault(time.Now),
|
2023-08-10 19:27:14 +02:00
|
|
|
field.Time("last_login").
|
2023-08-24 18:43:24 +02:00
|
|
|
// UpdateDefault(time.Now).
|
|
|
|
Default(time.Unix(0, 0)),
|
2023-08-19 04:52:15 +02:00
|
|
|
// field.Bool("save_search_queries").
|
|
|
|
// Default(true),
|
2023-04-13 00:07:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edges of the User.
|
|
|
|
func (User) Edges() []ent.Edge {
|
2023-08-19 04:52:15 +02:00
|
|
|
return []ent.Edge{
|
|
|
|
edge.To("agekey", AgeKey.Type).
|
|
|
|
Unique().
|
|
|
|
Immutable(),
|
|
|
|
edge.To("tracked_breaches", TrackedBreaches.Type),
|
|
|
|
edge.To("search_queries", SearchQuery.Type),
|
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
}
|