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
This commit is contained in:
parent
68285d861a
commit
2834657125
13 changed files with 474 additions and 812 deletions
|
|
@ -1,12 +1,10 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/imc-vibe/backend/internal/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/imc-vibe/backend/internal/db"
|
||||
)
|
||||
|
||||
|
|
@ -19,8 +17,8 @@ func NewUserHandler(database *db.DB) *UserHandler {
|
|||
}
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
Quota int64 `json:"quota"`
|
||||
}
|
||||
|
||||
|
|
@ -29,130 +27,110 @@ type UpdateUserRequest struct {
|
|||
Quota int64 `json:"quota,omitempty"`
|
||||
}
|
||||
|
||||
func (h *UserHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
domainName := extractDomainNameFromPath(r.URL.Path)
|
||||
func (h *UserHandler) List(c *gin.Context) {
|
||||
domainName := c.Param("name")
|
||||
if domainName == "" {
|
||||
Error(w, http.StatusBadRequest, "domain name required")
|
||||
Error(c, http.StatusBadRequest, "domain name required")
|
||||
return
|
||||
}
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "domain not found")
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
users, err := h.db.GetUsersByDomain(domain.ID)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "database error")
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
|
||||
Success(w, users)
|
||||
Success(c, users)
|
||||
}
|
||||
|
||||
func (h *UserHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
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
|
||||
}
|
||||
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/domains/"), "/")
|
||||
if len(parts) < 4 {
|
||||
Error(w, http.StatusBadRequest, "invalid path")
|
||||
return
|
||||
}
|
||||
domainName := parts[0]
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
idStr := parts[3]
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid user id")
|
||||
Error(c, http.StatusBadRequest, "invalid user id")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.db.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "user not found")
|
||||
Error(c, http.StatusNotFound, "user not found")
|
||||
return
|
||||
}
|
||||
|
||||
Success(w, user)
|
||||
Success(c, user)
|
||||
}
|
||||
|
||||
func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
domainName := extractDomainNameFromPath(r.URL.Path)
|
||||
func (h *UserHandler) Create(c *gin.Context) {
|
||||
domainName := c.Param("name")
|
||||
if domainName == "" {
|
||||
Error(w, http.StatusBadRequest, "domain name required")
|
||||
Error(c, http.StatusBadRequest, "domain name required")
|
||||
return
|
||||
}
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "domain not found")
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
var req CreateUserRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
if req.Email == "" || req.Password == "" {
|
||||
Error(w, http.StatusBadRequest, "email and password required")
|
||||
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(w, http.StatusConflict, "user already exists")
|
||||
Error(c, http.StatusConflict, "user already exists")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -160,145 +138,117 @@ func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
user, err := h.db.CreateUserInDomain(req.Email, passwordHash, req.Quota, domain.ID)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "failed to create user")
|
||||
Error(c, http.StatusInternalServerError, "failed to create user")
|
||||
return
|
||||
}
|
||||
|
||||
Created(w, user)
|
||||
Created(c, user)
|
||||
}
|
||||
|
||||
func (h *UserHandler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
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
|
||||
}
|
||||
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/domains/"), "/")
|
||||
if len(parts) < 4 {
|
||||
Error(w, http.StatusBadRequest, "invalid path")
|
||||
return
|
||||
}
|
||||
domainName := parts[0]
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
idStr := parts[3]
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid user id")
|
||||
Error(c, http.StatusBadRequest, "invalid user id")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.db.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "user not found")
|
||||
Error(c, http.StatusNotFound, "user not found")
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateUserRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid request body")
|
||||
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(w, http.StatusInternalServerError, "failed to update password")
|
||||
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(w, http.StatusInternalServerError, "failed to update quota")
|
||||
Error(c, http.StatusInternalServerError, "failed to update quota")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Success(w, map[string]string{"message": "user updated"})
|
||||
Success(c, map[string]string{"message": "user updated"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
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
|
||||
}
|
||||
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/domains/"), "/")
|
||||
if len(parts) < 4 {
|
||||
Error(w, http.StatusBadRequest, "invalid path")
|
||||
return
|
||||
}
|
||||
domainName := parts[0]
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
idStr := parts[3]
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid user id")
|
||||
Error(c, http.StatusBadRequest, "invalid user id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.DeleteUser(uint(id)); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "failed to delete user")
|
||||
Error(c, http.StatusInternalServerError, "failed to delete user")
|
||||
return
|
||||
}
|
||||
|
||||
NoContent(w)
|
||||
NoContent(c)
|
||||
}
|
||||
|
||||
func (h *UserHandler) ListAll(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
func (h *UserHandler) ListAll(c *gin.Context) {
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil || !authCtx.IsAdmin() {
|
||||
Error(w, http.StatusForbidden, "admin access required")
|
||||
Error(c, http.StatusForbidden, "admin access required")
|
||||
return
|
||||
}
|
||||
|
||||
users, err := h.db.GetAllMailUsers()
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "database error")
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
|
||||
Success(w, users)
|
||||
Success(c, users)
|
||||
}
|
||||
|
||||
func extractDomainNameFromPath(path string) string {
|
||||
parts := strings.Split(strings.TrimPrefix(path, "/api/"), "/")
|
||||
if len(parts) >= 2 && parts[0] == "domains" && parts[1] != "" {
|
||||
if idx := strings.Index(parts[1], "/"); idx > 0 {
|
||||
return parts[1][:idx]
|
||||
}
|
||||
return parts[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var _ = auth.RoleAdmin
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue