diff --git a/ent/schema/audit_log.go b/ent/schema/audit_log.go new file mode 100644 index 0000000..a58ecb8 --- /dev/null +++ b/ent/schema/audit_log.go @@ -0,0 +1,59 @@ +// Package schema defines the Ent schema for domain entities. +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" +) + +// AuditLog holds the schema definition for the AuditLog entity. +type AuditLog struct { + ent.Schema +} + +// Fields of the AuditLog. +func (AuditLog) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("user_id"). + NotEmpty(). + Comment("ID of the user/actor performing the action"), + field.String("action"). + NotEmpty(). + Comment("Action performed (e.g., user.create, user.update)"), + field.String("resource"). + Optional(). + Comment("Resource type (e.g., user, role)"), + field.String("resource_id"). + Optional(). + Comment("ID of the target resource"), + field.String("ip_address"). + Optional(). + Comment("IP address of the client"), + field.String("user_agent"). + Optional(). + Comment("User agent of the client"), + field.JSON("metadata", map[string]interface{}{}). + Optional(). + Comment("Additional metadata as JSON"), + field.Time("timestamp"). + Default(time.Now). + Immutable(), + } +} + +// Indexes of the AuditLog. +func (AuditLog) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("user_id"), + index.Fields("resource_id"), + index.Fields("timestamp"), + index.Fields("action"), + index.Fields("resource"), + } +} diff --git a/ent/schema/permission.go b/ent/schema/permission.go index cd589ec..8bfd7a3 100644 --- a/ent/schema/permission.go +++ b/ent/schema/permission.go @@ -1,6 +1,10 @@ package schema -import "entgo.io/ent" +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) // Permission holds the schema definition for the Permission entity. type Permission struct { @@ -9,10 +13,20 @@ type Permission struct { // Fields of the Permission. func (Permission) Fields() []ent.Field { - return nil + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("name"). + Unique(). + NotEmpty(). + Comment("Format: module.resource.action"), + } } // Edges of the Permission. func (Permission) Edges() []ent.Edge { - return nil + return []ent.Edge{ + edge.To("role_permissions", RolePermission.Type), + } } diff --git a/ent/schema/refresh_token.go b/ent/schema/refresh_token.go new file mode 100644 index 0000000..b49ed75 --- /dev/null +++ b/ent/schema/refresh_token.go @@ -0,0 +1,44 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" +) + +// RefreshToken holds the schema definition for the RefreshToken entity. +type RefreshToken struct { + ent.Schema +} + +// Fields of the RefreshToken. +func (RefreshToken) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("user_id"). + NotEmpty(). + Comment("ID of the user who owns this refresh token"), + field.String("token_hash"). + NotEmpty(). + Sensitive(). + Comment("SHA256 hash of the refresh token"), + field.Time("expires_at"). + Comment("When the refresh token expires"), + field.Time("created_at"). + Default(time.Now). + Immutable(), + } +} + +// Indexes of the RefreshToken. +func (RefreshToken) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("user_id"), + index.Fields("token_hash"), + index.Fields("expires_at"), + } +} diff --git a/ent/schema/role.go b/ent/schema/role.go index b80c4da..689fa37 100644 --- a/ent/schema/role.go +++ b/ent/schema/role.go @@ -1,6 +1,12 @@ package schema -import "entgo.io/ent" +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) // Role holds the schema definition for the Role entity. type Role struct { @@ -9,10 +15,25 @@ type Role struct { // Fields of the Role. func (Role) Fields() []ent.Field { - return nil + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("name"). + Unique(). + NotEmpty(), + field.String("description"). + Optional(), + field.Time("created_at"). + Default(time.Now). + Immutable(), + } } // Edges of the Role. func (Role) Edges() []ent.Edge { - return nil + return []ent.Edge{ + edge.To("role_permissions", RolePermission.Type), + edge.To("user_roles", UserRole.Type), + } } diff --git a/ent/schema/role_permission.go b/ent/schema/role_permission.go new file mode 100644 index 0000000..255c9b2 --- /dev/null +++ b/ent/schema/role_permission.go @@ -0,0 +1,34 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// RolePermission holds the schema definition for the RolePermission entity (many-to-many relationship). +type RolePermission struct { + ent.Schema +} + +// Fields of the RolePermission. +func (RolePermission) Fields() []ent.Field { + return []ent.Field{ + field.String("role_id"), + field.String("permission_id"), + } +} + +// Edges of the RolePermission. +func (RolePermission) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role", Role.Type). + Unique(). + Required(). + Field("role_id"), + edge.To("permission", Permission.Type). + Unique(). + Required(). + Field("permission_id"), + } +} diff --git a/ent/schema/user.go b/ent/schema/user.go index 7c14fb8..086a074 100644 --- a/ent/schema/user.go +++ b/ent/schema/user.go @@ -1,6 +1,12 @@ package schema -import "entgo.io/ent" +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) // User holds the schema definition for the User entity. type User struct { @@ -9,10 +15,43 @@ type User struct { // Fields of the User. func (User) Fields() []ent.Field { - return nil + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("email"). + Unique(). + NotEmpty(), + field.String("username"). + Optional(), + field.String("first_name"). + Optional(), + field.String("last_name"). + Optional(), + field.String("password_hash"). + NotEmpty(), + field.Bool("verified"). + Default(false), + field.String("email_verification_token"). + Optional(). + Sensitive(), + field.String("password_reset_token"). + Optional(). + Sensitive(), + field.Time("password_reset_expires_at"). + Optional(), + field.Time("created_at"). + Default(time.Now). + Immutable(), + field.Time("updated_at"). + Default(time.Now). + UpdateDefault(time.Now), + } } // Edges of the User. func (User) Edges() []ent.Edge { - return nil + return []ent.Edge{ + edge.To("user_roles", UserRole.Type), + } } diff --git a/ent/schema/user_role.go b/ent/schema/user_role.go new file mode 100644 index 0000000..bcfc366 --- /dev/null +++ b/ent/schema/user_role.go @@ -0,0 +1,34 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// UserRole holds the schema definition for the UserRole entity (many-to-many relationship). +type UserRole struct { + ent.Schema +} + +// Fields of the UserRole. +func (UserRole) Fields() []ent.Field { + return []ent.Field{ + field.String("user_id"), + field.String("role_id"), + } +} + +// Edges of the UserRole. +func (UserRole) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("user", User.Type). + Unique(). + Required(). + Field("user_id"), + edge.To("role", Role.Type). + Unique(). + Required(). + Field("role_id"), + } +} diff --git a/internal/ent/schema/audit_log.go b/internal/ent/schema/audit_log.go new file mode 100644 index 0000000..a58ecb8 --- /dev/null +++ b/internal/ent/schema/audit_log.go @@ -0,0 +1,59 @@ +// Package schema defines the Ent schema for domain entities. +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" +) + +// AuditLog holds the schema definition for the AuditLog entity. +type AuditLog struct { + ent.Schema +} + +// Fields of the AuditLog. +func (AuditLog) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("user_id"). + NotEmpty(). + Comment("ID of the user/actor performing the action"), + field.String("action"). + NotEmpty(). + Comment("Action performed (e.g., user.create, user.update)"), + field.String("resource"). + Optional(). + Comment("Resource type (e.g., user, role)"), + field.String("resource_id"). + Optional(). + Comment("ID of the target resource"), + field.String("ip_address"). + Optional(). + Comment("IP address of the client"), + field.String("user_agent"). + Optional(). + Comment("User agent of the client"), + field.JSON("metadata", map[string]interface{}{}). + Optional(). + Comment("Additional metadata as JSON"), + field.Time("timestamp"). + Default(time.Now). + Immutable(), + } +} + +// Indexes of the AuditLog. +func (AuditLog) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("user_id"), + index.Fields("resource_id"), + index.Fields("timestamp"), + index.Fields("action"), + index.Fields("resource"), + } +} diff --git a/internal/ent/schema/permission.go b/internal/ent/schema/permission.go new file mode 100644 index 0000000..8bfd7a3 --- /dev/null +++ b/internal/ent/schema/permission.go @@ -0,0 +1,32 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// Permission holds the schema definition for the Permission entity. +type Permission struct { + ent.Schema +} + +// Fields of the Permission. +func (Permission) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("name"). + Unique(). + NotEmpty(). + Comment("Format: module.resource.action"), + } +} + +// Edges of the Permission. +func (Permission) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role_permissions", RolePermission.Type), + } +} diff --git a/internal/ent/schema/refresh_token.go b/internal/ent/schema/refresh_token.go new file mode 100644 index 0000000..b49ed75 --- /dev/null +++ b/internal/ent/schema/refresh_token.go @@ -0,0 +1,44 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/index" +) + +// RefreshToken holds the schema definition for the RefreshToken entity. +type RefreshToken struct { + ent.Schema +} + +// Fields of the RefreshToken. +func (RefreshToken) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("user_id"). + NotEmpty(). + Comment("ID of the user who owns this refresh token"), + field.String("token_hash"). + NotEmpty(). + Sensitive(). + Comment("SHA256 hash of the refresh token"), + field.Time("expires_at"). + Comment("When the refresh token expires"), + field.Time("created_at"). + Default(time.Now). + Immutable(), + } +} + +// Indexes of the RefreshToken. +func (RefreshToken) Indexes() []ent.Index { + return []ent.Index{ + index.Fields("user_id"), + index.Fields("token_hash"), + index.Fields("expires_at"), + } +} diff --git a/internal/ent/schema/role.go b/internal/ent/schema/role.go new file mode 100644 index 0000000..689fa37 --- /dev/null +++ b/internal/ent/schema/role.go @@ -0,0 +1,39 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// Role holds the schema definition for the Role entity. +type Role struct { + ent.Schema +} + +// Fields of the Role. +func (Role) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("name"). + Unique(). + NotEmpty(), + field.String("description"). + Optional(), + field.Time("created_at"). + Default(time.Now). + Immutable(), + } +} + +// Edges of the Role. +func (Role) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role_permissions", RolePermission.Type), + edge.To("user_roles", UserRole.Type), + } +} diff --git a/internal/ent/schema/role_permission.go b/internal/ent/schema/role_permission.go new file mode 100644 index 0000000..255c9b2 --- /dev/null +++ b/internal/ent/schema/role_permission.go @@ -0,0 +1,34 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// RolePermission holds the schema definition for the RolePermission entity (many-to-many relationship). +type RolePermission struct { + ent.Schema +} + +// Fields of the RolePermission. +func (RolePermission) Fields() []ent.Field { + return []ent.Field{ + field.String("role_id"), + field.String("permission_id"), + } +} + +// Edges of the RolePermission. +func (RolePermission) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("role", Role.Type). + Unique(). + Required(). + Field("role_id"), + edge.To("permission", Permission.Type). + Unique(). + Required(). + Field("permission_id"), + } +} diff --git a/internal/ent/schema/user.go b/internal/ent/schema/user.go new file mode 100644 index 0000000..086a074 --- /dev/null +++ b/internal/ent/schema/user.go @@ -0,0 +1,57 @@ +package schema + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// User holds the schema definition for the User entity. +type User struct { + ent.Schema +} + +// Fields of the User. +func (User) Fields() []ent.Field { + return []ent.Field{ + field.String("id"). + Unique(). + Immutable(), + field.String("email"). + Unique(). + NotEmpty(), + field.String("username"). + Optional(), + field.String("first_name"). + Optional(), + field.String("last_name"). + Optional(), + field.String("password_hash"). + NotEmpty(), + field.Bool("verified"). + Default(false), + field.String("email_verification_token"). + Optional(). + Sensitive(), + field.String("password_reset_token"). + Optional(). + Sensitive(), + field.Time("password_reset_expires_at"). + Optional(), + field.Time("created_at"). + Default(time.Now). + Immutable(), + field.Time("updated_at"). + Default(time.Now). + UpdateDefault(time.Now), + } +} + +// Edges of the User. +func (User) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("user_roles", UserRole.Type), + } +} diff --git a/internal/ent/schema/user_role.go b/internal/ent/schema/user_role.go new file mode 100644 index 0000000..bcfc366 --- /dev/null +++ b/internal/ent/schema/user_role.go @@ -0,0 +1,34 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/edge" + "entgo.io/ent/schema/field" +) + +// UserRole holds the schema definition for the UserRole entity (many-to-many relationship). +type UserRole struct { + ent.Schema +} + +// Fields of the UserRole. +func (UserRole) Fields() []ent.Field { + return []ent.Field{ + field.String("user_id"), + field.String("role_id"), + } +} + +// Edges of the UserRole. +func (UserRole) Edges() []ent.Edge { + return []ent.Edge{ + edge.To("user", User.Type). + Unique(). + Required(). + Field("user_id"), + edge.To("role", Role.Type). + Unique(). + Required(). + Field("role_id"), + } +}