imc-vibe/backend/internal/api/handlers/users.go
Christoph Haas 2834657125 Switch from net/http to gin-gonic web framework
- Added gin-gonic v1.10.0 dependency
- Refactored router.go: clean route groups with middleware chains
- Refactored all handlers to use gin.Context instead of http.ResponseWriter/*http.Request
- Simplified response helpers (JSON, Error, Success, Created, NoContent)
- Clean auth middleware using Gin's c.Set() for context
- Cleaner route definitions with path parameters (e.g., /domains/:name/users/:id)
- Admin routes moved to /api/admin group with RequireAdmin middleware
2026-03-22 23:28:28 +01:00

254 lines
5.6 KiB
Go

package handlers
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/imc-vibe/backend/internal/db"
)
type UserHandler struct {
db *db.DB
}
func NewUserHandler(database *db.DB) *UserHandler {
return &UserHandler{db: database}
}
type CreateUserRequest struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
Quota int64 `json:"quota"`
}
type UpdateUserRequest struct {
Password string `json:"password,omitempty"`
Quota int64 `json:"quota,omitempty"`
}
func (h *UserHandler) List(c *gin.Context) {
domainName := c.Param("name")
if domainName == "" {
Error(c, http.StatusBadRequest, "domain name required")
return
}
authCtx := GetAuthContext(c)
if authCtx == nil {
Error(c, http.StatusUnauthorized, "authentication required")
return
}
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
if !canAccess {
Error(c, http.StatusForbidden, "access denied")
return
}
domain, err := h.db.GetDomainByName(domainName)
if err != nil {
Error(c, http.StatusNotFound, "domain not found")
return
}
users, err := h.db.GetUsersByDomain(domain.ID)
if err != nil {
Error(c, http.StatusInternalServerError, "database error")
return
}
Success(c, users)
}
func (h *UserHandler) Get(c *gin.Context) {
domainName := c.Param("name")
idStr := c.Param("id")
if domainName == "" || idStr == "" {
Error(c, http.StatusBadRequest, "domain name and user id required")
return
}
authCtx := GetAuthContext(c)
if authCtx == nil {
Error(c, http.StatusUnauthorized, "authentication required")
return
}
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
if !canAccess {
Error(c, http.StatusForbidden, "access denied")
return
}
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
Error(c, http.StatusBadRequest, "invalid user id")
return
}
user, err := h.db.GetUserByID(uint(id))
if err != nil {
Error(c, http.StatusNotFound, "user not found")
return
}
Success(c, user)
}
func (h *UserHandler) Create(c *gin.Context) {
domainName := c.Param("name")
if domainName == "" {
Error(c, http.StatusBadRequest, "domain name required")
return
}
authCtx := GetAuthContext(c)
if authCtx == nil {
Error(c, http.StatusUnauthorized, "authentication required")
return
}
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
if !canAccess {
Error(c, http.StatusForbidden, "access denied")
return
}
domain, err := h.db.GetDomainByName(domainName)
if err != nil {
Error(c, http.StatusNotFound, "domain not found")
return
}
var req CreateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
Error(c, http.StatusBadRequest, "invalid request body")
return
}
existing, _ := h.db.GetUserByEmail(req.Email)
if existing != nil {
Error(c, http.StatusConflict, "user already exists")
return
}
passwordHash := "{BLF-CRYPT}" + req.Password
user, err := h.db.CreateUserInDomain(req.Email, passwordHash, req.Quota, domain.ID)
if err != nil {
Error(c, http.StatusInternalServerError, "failed to create user")
return
}
Created(c, user)
}
func (h *UserHandler) Update(c *gin.Context) {
domainName := c.Param("name")
idStr := c.Param("id")
if domainName == "" || idStr == "" {
Error(c, http.StatusBadRequest, "domain name and user id required")
return
}
authCtx := GetAuthContext(c)
if authCtx == nil {
Error(c, http.StatusUnauthorized, "authentication required")
return
}
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
if !canAccess {
Error(c, http.StatusForbidden, "access denied")
return
}
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
Error(c, http.StatusBadRequest, "invalid user id")
return
}
user, err := h.db.GetUserByID(uint(id))
if err != nil {
Error(c, http.StatusNotFound, "user not found")
return
}
var req UpdateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
Error(c, http.StatusBadRequest, "invalid request")
return
}
if req.Password != "" {
passwordHash := "{BLF-CRYPT}" + req.Password
if err := h.db.UpdateUserPassword(user.ID, passwordHash); err != nil {
Error(c, http.StatusInternalServerError, "failed to update password")
return
}
}
if req.Quota >= 0 {
if err := h.db.UpdateUserQuota(user.ID, req.Quota); err != nil {
Error(c, http.StatusInternalServerError, "failed to update quota")
return
}
}
Success(c, map[string]string{"message": "user updated"})
}
func (h *UserHandler) Delete(c *gin.Context) {
domainName := c.Param("name")
idStr := c.Param("id")
if domainName == "" || idStr == "" {
Error(c, http.StatusBadRequest, "domain name and user id required")
return
}
authCtx := GetAuthContext(c)
if authCtx == nil {
Error(c, http.StatusUnauthorized, "authentication required")
return
}
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
if !canAccess {
Error(c, http.StatusForbidden, "access denied")
return
}
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
Error(c, http.StatusBadRequest, "invalid user id")
return
}
if err := h.db.DeleteUser(uint(id)); err != nil {
Error(c, http.StatusInternalServerError, "failed to delete user")
return
}
NoContent(c)
}
func (h *UserHandler) ListAll(c *gin.Context) {
authCtx := GetAuthContext(c)
if authCtx == nil || !authCtx.IsAdmin() {
Error(c, http.StatusForbidden, "admin access required")
return
}
users, err := h.db.GetAllMailUsers()
if err != nil {
Error(c, http.StatusInternalServerError, "database error")
return
}
Success(c, users)
}