- Introduced helpers.LoadTemplateFiles() for consistent layout + topbar rendering - Replaced repeated template.ParseFiles() calls across handlers - Created generic RenderError(w, r, statusCode) helper - Replaced old Render403 with flexible RenderError - Updated AdminOnly middleware to render 403 errors with context - Added 500.html template for graceful panic fallback - Prepared structure for future error codes (404, 429, etc.)
39 lines
952 B
Go
39 lines
952 B
Go
package middleware
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"net/http"
|
|
"synlotto-website/helpers"
|
|
)
|
|
|
|
func AdminOnly(db *sql.DB, next http.HandlerFunc) http.HandlerFunc {
|
|
return Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := helpers.GetCurrentUserID(r)
|
|
if !ok || !helpers.IsAdmin(db, userID) {
|
|
log.Printf("⛔️ Unauthorized admin attempt: user_id=%v, IP=%s, Path=%s", userID, r.RemoteAddr, r.URL.Path)
|
|
helpers.RenderError(w, r, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
ip := r.RemoteAddr
|
|
ua := r.UserAgent()
|
|
path := r.URL.Path
|
|
|
|
_, err := db.Exec(`
|
|
INSERT INTO admin_access_log (user_id, path, ip, user_agent)
|
|
VALUES (?, ?, ?, ?)`,
|
|
userID, path, ip, ua,
|
|
)
|
|
if err != nil {
|
|
log.Printf("⚠️ Failed to log admin access: %v", err)
|
|
}
|
|
|
|
log.Printf("🛡️ Admin access: user_id=%d IP=%s Path=%s", userID, ip, path)
|
|
|
|
next(w, r)
|
|
})
|
|
}
|
|
|
|
// ToDo need to look into audit/access log tables and consolidate
|