ulflow_phattt2901 f4ef71b63b
Some checks failed
CI Pipeline / Security Scan (push) Failing after 5m24s
CI Pipeline / Lint (push) Failing after 5m30s
CI Pipeline / Test (push) Has been skipped
CI Pipeline / Build (push) Has been skipped
CI Pipeline / Notification (push) Successful in 1s
feat: implement user authentication system with JWT and role-based access control
2025-05-24 11:24:19 +07:00

51 lines
1.5 KiB
Go

package user
import (
"time"
"starter-kit/internal/domain/role"
)
// User đại diện cho một người dùng trong hệ thống
type User struct {
ID string `json:"id" gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
Username string `json:"username" gorm:"size:50;uniqueIndex;not null"`
Email string `json:"email" gorm:"size:100;uniqueIndex;not null"`
PasswordHash string `json:"-" gorm:"not null"`
FullName string `json:"full_name" gorm:"size:100"`
AvatarURL string `json:"avatar_url,omitempty" gorm:"size:255"`
IsActive bool `json:"is_active" gorm:"default:true"`
LastLoginAt *time.Time `json:"last_login_at,omitempty"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
DeletedAt *time.Time `json:"-" gorm:"index"`
Roles []*role.Role `json:"roles,omitempty" gorm:"many2many:user_roles;"`
}
// TableName specifies the table name for the User model
func (User) TableName() string {
return "users"
}
// HasRole kiểm tra xem user có vai trò được chỉ định không
func (u *User) HasRole(roleName string) bool {
for _, r := range u.Roles {
if r.Name == roleName {
return true
}
}
return false
}
// HasAnyRole kiểm tra xem user có bất kỳ vai trò nào trong danh sách không
func (u *User) HasAnyRole(roles ...string) bool {
for _, r := range u.Roles {
for _, roleName := range roles {
if r.Name == roleName {
return true
}
}
}
return false
}