backup: pre-hardening baseline
This commit is contained in:
commit
9e4c612dcb
57 changed files with 10393 additions and 0 deletions
56
backend/internal/database/repositories/user.go
Normal file
56
backend/internal/database/repositories/user.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/traefik/traefik-gui/backend/internal/models"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *sql.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetByID(id string) (*models.User, error) {
|
||||
user := &models.User{}
|
||||
err := r.db.QueryRow(
|
||||
`SELECT id, username, email, password_hash, role, created_at, updated_at, last_login
|
||||
FROM users WHERE id = ?`, id,
|
||||
).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt, &user.LastLogin)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetByUsername(username string) (*models.User, error) {
|
||||
user := &models.User{}
|
||||
err := r.db.QueryRow(
|
||||
`SELECT id, username, email, password_hash, role, created_at, updated_at, last_login
|
||||
FROM users WHERE username = ?`, username,
|
||||
).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt, &user.LastLogin)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdateLastLogin(id string) error {
|
||||
_, err := r.db.Exec(
|
||||
`UPDATE users SET last_login = ?, updated_at = ? WHERE id = ?`,
|
||||
time.Now(), time.Now(), id,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserRepository) Create(user *models.User) error {
|
||||
_, err := r.db.Exec(
|
||||
`INSERT INTO users (id, username, email, password_hash, role) VALUES (?, ?, ?, ?, ?)`,
|
||||
user.ID, user.Username, user.Email, user.PasswordHash, user.Role,
|
||||
)
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue