models

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmailVerification

type EmailVerification struct {
	ID        string    `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
	UserID    string    `gorm:"not null;index" json:"user_id"`
	Token     string    `gorm:"uniqueIndex;not null" json:"token"`
	Email     string    `gorm:"not null" json:"email"`
	IsUsed    bool      `gorm:"default:false" json:"is_used"`
	ExpiresAt time.Time `json:"expires_at"`
	CreatedAt time.Time `json:"created_at"`

	// Relationships
	User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

EmailVerification represents an email verification token

func (*EmailVerification) IsExpired

func (ev *EmailVerification) IsExpired() bool

IsExpired checks if the email verification token is expired

func (EmailVerification) TableName

func (EmailVerification) TableName() string

TableName returns the table name for EmailVerification

type OTPVerification

type OTPVerification struct {
	ID        string    `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
	UserID    string    `gorm:"not null;index" json:"user_id"`
	Phone     string    `gorm:"not null" json:"phone"`
	Code      string    `gorm:"not null" json:"code"`
	IsUsed    bool      `gorm:"default:false" json:"is_used"`
	ExpiresAt time.Time `json:"expires_at"`
	CreatedAt time.Time `json:"created_at"`

	// Relationships
	User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

OTPVerification represents an OTP verification

func (*OTPVerification) IsExpired

func (otp *OTPVerification) IsExpired() bool

IsExpired checks if the OTP is expired

func (OTPVerification) TableName

func (OTPVerification) TableName() string

TableName returns the table name for OTPVerification

type PasswordReset

type PasswordReset struct {
	ID        string    `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
	UserID    string    `gorm:"not null;index" json:"user_id"`
	Token     string    `gorm:"uniqueIndex;not null" json:"token"`
	Email     string    `gorm:"not null" json:"email"`
	IsUsed    bool      `gorm:"default:false" json:"is_used"`
	ExpiresAt time.Time `json:"expires_at"`
	CreatedAt time.Time `json:"created_at"`

	// Relationships
	User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

PasswordReset represents a password reset token

func (*PasswordReset) IsExpired

func (pr *PasswordReset) IsExpired() bool

IsExpired checks if the password reset token is expired

func (PasswordReset) TableName

func (PasswordReset) TableName() string

TableName returns the table name for PasswordReset

type Permission

type Permission struct {
	ID          uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id"`
	Name        string         `gorm:"size:100;not null" json:"name"`
	Slug        string         `gorm:"size:100;not null;unique;index" json:"slug"`
	Description string         `gorm:"type:text" json:"description"`
	Resource    string         `gorm:"size:100;index" json:"resource"` // e.g., "users", "posts"
	Action      string         `gorm:"size:50;index" json:"action"`    // e.g., "create", "read", "update", "delete"
	IsSystem    bool           `gorm:"default:false" json:"is_system"` // System permissions can't be deleted
	CreatedAt   time.Time      `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"autoUpdateTime" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"-"`

	// Relationships
	Roles []*Role `gorm:"many2many:role_permissions;" json:"roles,omitempty"`
}

Permission represents a system permission

func (*Permission) BeforeCreate

func (p *Permission) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate hook to set UUID if not already set

func (Permission) TableName

func (Permission) TableName() string

TableName returns the table name for Permission

type Role

type Role struct {
	ID          uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id"`
	Name        string         `gorm:"size:100;not null;unique;index" json:"name"`
	Slug        string         `gorm:"size:100;not null;unique;index" json:"slug"`
	Description string         `gorm:"type:text" json:"description"`
	IsSystem    bool           `gorm:"default:false" json:"is_system"` // System roles can't be deleted
	CreatedAt   time.Time      `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"autoUpdateTime" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"-"`

	// Relationships
	Permissions []*Permission `gorm:"many2many:role_permissions;" json:"permissions,omitempty"`
	Users       []*User       `gorm:"many2many:user_roles;" json:"users,omitempty"`
}

Role represents a user role

func (*Role) AddPermission

func (r *Role) AddPermission(db *gorm.DB, permission *Permission) error

AddPermission adds a permission to the role

func (*Role) BeforeCreate

func (r *Role) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate hook to set UUID if not already set

func (*Role) HasPermission

func (r *Role) HasPermission(permissionSlug string) bool

HasPermission checks if the role has a specific permission

func (*Role) RemovePermission

func (r *Role) RemovePermission(db *gorm.DB, permission *Permission) error

RemovePermission removes a permission from the role

func (*Role) SyncPermissions

func (r *Role) SyncPermissions(db *gorm.DB, permissions []*Permission) error

SyncPermissions replaces all permissions with the given ones

func (Role) TableName

func (Role) TableName() string

TableName returns the table name for Role

type Session

type Session struct {
	ID           string    `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
	UserID       string    `gorm:"not null;index" json:"user_id"`
	Token        string    `gorm:"uniqueIndex;not null" json:"token"`
	RefreshToken string    `gorm:"uniqueIndex" json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	IPAddress    string    `json:"ip_address"`
	IsActive     bool      `gorm:"default:true" json:"is_active"`
	ExpiresAt    time.Time `json:"expires_at"`
	LastUsedAt   time.Time `json:"last_used_at"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`

	// Relationships
	User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

Session represents a user session

func (*Session) IsExpired

func (s *Session) IsExpired() bool

IsExpired checks if the session is expired

func (Session) TableName

func (Session) TableName() string

TableName returns the table name for Session

type User

type User struct {
	ID              uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id"`
	Email           string         `gorm:"uniqueIndex;not null" json:"email" validate:"required,email"`
	Phone           string         `gorm:"uniqueIndex" json:"phone" validate:"omitempty,phone"`
	Password        string         `gorm:"not null" json:"-" validate:"required,password"`
	FirstName       string         `gorm:"not null" json:"first_name" validate:"required,min=2,max=50"`
	LastName        string         `gorm:"not null" json:"last_name" validate:"required,min=2,max=50"`
	IsActive        bool           `gorm:"default:true" json:"is_active"`
	IsEmailVerified bool           `gorm:"default:false" json:"is_email_verified"`
	IsPhoneVerified bool           `gorm:"default:false" json:"is_phone_verified"`
	Is2FAEnabled    bool           `gorm:"default:false" json:"is_2fa_enabled"`
	TwoFactorSecret string         `gorm:"-" json:"-"` // Not stored in DB for security
	LastLoginAt     *time.Time     `json:"last_login_at"`
	EmailVerifiedAt *time.Time     `json:"email_verified_at"`
	PhoneVerifiedAt *time.Time     `json:"phone_verified_at"`
	CreatedAt       time.Time      `json:"created_at"`
	UpdatedAt       time.Time      `json:"updated_at"`
	DeletedAt       gorm.DeletedAt `gorm:"index" json:"-"`

	// Relationships
	Roles       []*Role       `gorm:"many2many:user_roles;" json:"roles,omitempty"`
	Permissions []*Permission `gorm:"many2many:user_permissions;" json:"permissions,omitempty"`
	Sessions    []Session     `json:"sessions,omitempty"`
}

User represents a user in the system

func (*User) AssignRole

func (u *User) AssignRole(db *gorm.DB, role *Role) error

AssignRole assigns a role to the user

func (*User) BeforeCreate

func (u *User) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate hook to set UUID if not already set

func (*User) GetAllPermissions

func (u *User) GetAllPermissions() []string

GetAllPermissions returns all permissions (direct + from roles)

func (*User) GetFullName

func (u *User) GetFullName() string

GetFullName returns the user's full name

func (*User) GivePermission

func (u *User) GivePermission(db *gorm.DB, permission *Permission) error

GivePermission gives a direct permission to the user

func (*User) HasPermission

func (u *User) HasPermission(permissionSlug string) bool

HasPermission checks if user has a specific permission (by slug)

func (*User) HasRole

func (u *User) HasRole(roleSlug string) bool

HasRole checks if user has a specific role (by slug)

func (*User) IsAdmin

func (u *User) IsAdmin() bool

IsAdmin checks if user has admin role

func (*User) RemoveRole

func (u *User) RemoveRole(db *gorm.DB, role *Role) error

RemoveRole removes a role from the user

func (*User) RevokePermission

func (u *User) RevokePermission(db *gorm.DB, permission *Permission) error

RevokePermission revokes a direct permission from the user

func (User) TableName

func (User) TableName() string

TableName returns the table name for User

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL