first commit
This commit is contained in:
60
backend/models/user.go
Normal file
60
backend/models/user.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
|
||||
Email string `json:"email" gorm:"uniqueIndex;not null"`
|
||||
Password string `json:"-" gorm:"not null"` // Hidden from JSON
|
||||
FirstName string `json:"first_name" gorm:"not null"`
|
||||
LastName string `json:"last_name" gorm:"not null"`
|
||||
Avatar string `json:"avatar"`
|
||||
Role string `json:"role" gorm:"default:'member'"` // admin, manager, member
|
||||
|
||||
// Relationships
|
||||
Projects []Project `json:"projects" gorm:"many2many:project_members"`
|
||||
OwnedProjects []Project `json:"owned_projects" gorm:"foreignKey:OwnerID"`
|
||||
Tasks []Task `json:"tasks" gorm:"foreignKey:AssignedToID"`
|
||||
Comments []Comment `json:"comments" gorm:"foreignKey:UserID"`
|
||||
FileUploads []FileUpload `json:"file_uploads" gorm:"foreignKey:UploadedByID"`
|
||||
}
|
||||
|
||||
// HashPassword hashes the user's password
|
||||
func (u *User) HashPassword(password string) error {
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.Password = string(hashedPassword)
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckPassword checks if the provided password matches the user's password
|
||||
func (u *User) CheckPassword(password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// GetFullName returns the user's full name
|
||||
func (u *User) GetFullName() string {
|
||||
return u.FirstName + " " + u.LastName
|
||||
}
|
||||
|
||||
// IsAdmin checks if the user has admin role
|
||||
func (u *User) IsAdmin() bool {
|
||||
return u.Role == "admin"
|
||||
}
|
||||
|
||||
// IsManager checks if the user has manager role or higher
|
||||
func (u *User) IsManager() bool {
|
||||
return u.Role == "manager" || u.Role == "admin"
|
||||
}
|
Reference in New Issue
Block a user