Documentation
¶
Index ¶
- type EmailVerification
- type OTPVerification
- type PasswordReset
- type Permission
- type Role
- func (r *Role) AddPermission(db *gorm.DB, permission *Permission) error
- func (r *Role) BeforeCreate(tx *gorm.DB) (err error)
- func (r *Role) HasPermission(permissionSlug string) bool
- func (r *Role) RemovePermission(db *gorm.DB, permission *Permission) error
- func (r *Role) SyncPermissions(db *gorm.DB, permissions []*Permission) error
- func (Role) TableName() string
- type Session
- type User
- func (u *User) AssignRole(db *gorm.DB, role *Role) error
- func (u *User) BeforeCreate(tx *gorm.DB) (err error)
- func (u *User) GetAllPermissions() []string
- func (u *User) GetFullName() string
- func (u *User) GivePermission(db *gorm.DB, permission *Permission) error
- func (u *User) HasPermission(permissionSlug string) bool
- func (u *User) HasRole(roleSlug string) bool
- func (u *User) IsAdmin() bool
- func (u *User) RemoveRole(db *gorm.DB, role *Role) error
- func (u *User) RevokePermission(db *gorm.DB, permission *Permission) error
- func (User) TableName() string
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 ¶
BeforeCreate hook to set UUID if not already set
func (*Role) HasPermission ¶
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
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
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 ¶
AssignRole assigns a role to the user
func (*User) BeforeCreate ¶
BeforeCreate hook to set UUID if not already set
func (*User) GetAllPermissions ¶
GetAllPermissions returns all permissions (direct + from roles)
func (*User) GetFullName ¶
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 ¶
HasPermission checks if user has a specific permission (by slug)
func (*User) RemoveRole ¶
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