Add PDF reports, file uploads, admin auth, and comprehensive tests
Features: - PDF service for generating task reports with ReportLab-style formatting - Storage service for file uploads (local and S3-compatible) - Admin authentication middleware with JWT support - Admin user model and repository Infrastructure: - Updated Docker configuration for admin panel builds - Email service enhancements for task notifications - Updated router with admin and file upload routes - Environment configuration updates Tests: - Unit tests for handlers (auth, residence, task) - Unit tests for models (user, residence, task) - Unit tests for repositories (user, residence, task) - Unit tests for services (residence, task) - Integration test setup - Test utilities for mocking database and services Database: - Admin user seed data - Updated test data seeds 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
63
internal/models/admin.go
Normal file
63
internal/models/admin.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// AdminRole represents the role of an admin user
|
||||
type AdminRole string
|
||||
|
||||
const (
|
||||
AdminRoleAdmin AdminRole = "admin"
|
||||
AdminRoleSuperAdmin AdminRole = "super_admin"
|
||||
)
|
||||
|
||||
// AdminUser represents an administrator for the admin panel
|
||||
type AdminUser struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Email string `gorm:"uniqueIndex;size:254;not null" json:"email"`
|
||||
Password string `gorm:"size:128;not null" json:"-"`
|
||||
FirstName string `gorm:"size:100" json:"first_name"`
|
||||
LastName string `gorm:"size:100" json:"last_name"`
|
||||
Role AdminRole `gorm:"size:20;default:'admin'" json:"role"`
|
||||
IsActive bool `gorm:"default:true" json:"is_active"`
|
||||
LastLogin *time.Time `json:"last_login,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName specifies the table name for AdminUser
|
||||
func (AdminUser) TableName() string {
|
||||
return "admin_users"
|
||||
}
|
||||
|
||||
// SetPassword hashes and sets the password
|
||||
func (a *AdminUser) SetPassword(password string) error {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Password = string(hash)
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckPassword verifies the password against the stored hash
|
||||
func (a *AdminUser) CheckPassword(password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// FullName returns the admin's full name
|
||||
func (a *AdminUser) FullName() string {
|
||||
if a.FirstName == "" && a.LastName == "" {
|
||||
return a.Email
|
||||
}
|
||||
return a.FirstName + " " + a.LastName
|
||||
}
|
||||
|
||||
// IsSuperAdmin checks if the admin has super admin privileges
|
||||
func (a *AdminUser) IsSuperAdmin() bool {
|
||||
return a.Role == AdminRoleSuperAdmin
|
||||
}
|
||||
Reference in New Issue
Block a user