2022-08-30 02:30:36 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"entgo.io/ent"
|
2022-09-04 02:42:03 +00:00
|
|
|
"entgo.io/ent/dialect/entsql"
|
2022-08-30 02:30:36 +00:00
|
|
|
"entgo.io/ent/schema/edge"
|
|
|
|
"entgo.io/ent/schema/field"
|
2023-03-07 06:18:58 +00:00
|
|
|
"entgo.io/ent/schema/mixin"
|
|
|
|
"github.com/google/uuid"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// User holds the schema definition for the User entity.
|
|
|
|
type User struct {
|
|
|
|
ent.Schema
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:04:50 +00:00
|
|
|
func (User) Mixin() []ent.Mixin {
|
|
|
|
return []ent.Mixin{
|
|
|
|
mixins.BaseMixin{},
|
2023-03-07 06:18:58 +00:00
|
|
|
GroupMixin{ref: "users"},
|
2022-08-30 18:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// Fields of the User.
|
|
|
|
func (User) Fields() []ent.Field {
|
|
|
|
return []ent.Field{
|
|
|
|
field.String("name").
|
2022-08-30 18:04:50 +00:00
|
|
|
MaxLen(255).
|
2022-08-30 02:30:36 +00:00
|
|
|
NotEmpty(),
|
|
|
|
field.String("email").
|
2022-08-30 18:04:50 +00:00
|
|
|
MaxLen(255).
|
2022-08-30 02:30:36 +00:00
|
|
|
NotEmpty().
|
|
|
|
Unique(),
|
|
|
|
field.String("password").
|
2022-08-30 18:04:50 +00:00
|
|
|
MaxLen(255).
|
2022-08-30 02:30:36 +00:00
|
|
|
NotEmpty().
|
|
|
|
Sensitive(),
|
|
|
|
field.Bool("is_superuser").
|
|
|
|
Default(false),
|
2023-03-07 06:18:58 +00:00
|
|
|
field.Bool("superuser").
|
|
|
|
Default(false),
|
2022-10-09 17:44:13 +00:00
|
|
|
field.Enum("role").
|
|
|
|
Default("user").
|
|
|
|
Values("user", "owner"),
|
|
|
|
field.Time("activated_on").
|
|
|
|
Optional(),
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edges of the User.
|
|
|
|
func (User) Edges() []ent.Edge {
|
|
|
|
return []ent.Edge{
|
2022-09-29 05:42:33 +00:00
|
|
|
edge.To("auth_tokens", AuthTokens.Type).
|
2024-03-02 18:02:41 +00:00
|
|
|
Annotations(entsql.Annotation{OnDelete: entsql.Cascade}),
|
2023-03-07 06:18:58 +00:00
|
|
|
edge.To("notifiers", Notifier.Type).
|
2024-03-02 18:02:41 +00:00
|
|
|
Annotations(entsql.Annotation{OnDelete: entsql.Cascade}),
|
|
|
|
edge.To("action_tokens", ActionToken.Type).
|
|
|
|
Annotations(entsql.Annotation{OnDelete: entsql.Cascade}),
|
2023-03-07 06:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserMixin when embedded in an ent.Schema, adds a reference to
|
|
|
|
// the Group entity.
|
|
|
|
type UserMixin struct {
|
|
|
|
ref string
|
|
|
|
field string
|
|
|
|
mixin.Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g UserMixin) Fields() []ent.Field {
|
|
|
|
if g.field != "" {
|
|
|
|
return []ent.Field{
|
|
|
|
field.UUID(g.field, uuid.UUID{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g UserMixin) Edges() []ent.Edge {
|
|
|
|
edge := edge.From("user", User.Type).
|
|
|
|
Ref(g.ref).
|
|
|
|
Unique().
|
|
|
|
Required()
|
|
|
|
|
|
|
|
if g.field != "" {
|
|
|
|
edge = edge.Field(g.field)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
2023-03-07 06:18:58 +00:00
|
|
|
|
|
|
|
return []ent.Edge{edge}
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|