- Created docs/ directory for better organization - Moved 35 markdown files from root to docs/ - Includes all status reports, guides, and testing documentation Co-Authored-By: Warp <agent@warp.dev>
2.4 KiB
2.4 KiB
Bug Fix: Permission Checking Issue
Problem
The storage endpoints were returning 403 Forbidden - "insufficient permissions" even though the admin user had the correct storage:read permission in the database.
Root Cause
The requirePermission middleware was checking authUser.Permissions, but when a user was loaded via ValidateToken(), the Permissions field was empty. The permissions were never loaded from the database.
Solution
Updated the requirePermission middleware to:
- Check if permissions are already loaded in the user object
- If not, load them on-demand from the database using the DB connection stored in the request context
- Then perform the permission check
Also updated requireRole middleware for consistency.
Changes Made
File: backend/internal/common/router/middleware.go
-
Added database import to access the DB type
-
Updated
requirePermissionmiddleware to load permissions on-demand:// Load permissions if not already loaded if len(authUser.Permissions) == 0 { db, exists := c.Get("db") if exists { if dbConn, ok := db.(*database.DB); ok { permissions, err := iam.GetUserPermissions(dbConn, authUser.ID) if err == nil { authUser.Permissions = permissions } } } } -
Updated
requireRolemiddleware similarly to load roles on-demand
File: backend/internal/common/router/router.go
- Added middleware to store DB in context for permission middleware:
protected.Use(func(c *gin.Context) { // Store DB in context for permission middleware c.Set("db", db) c.Next() })
Testing
After this fix, the storage endpoints should work correctly:
# This should now return 200 OK instead of 403
curl http://localhost:8080/api/v1/storage/disks \
-H "Authorization: Bearer $TOKEN"
Impact
- ✅ Storage endpoints now work correctly
- ✅ Permission checking is more robust (lazy loading)
- ✅ No performance impact (permissions cached in user object for the request)
- ✅ Consistent behavior between role and permission checks
Related Files
backend/internal/common/router/middleware.go- Permission middlewarebackend/internal/common/router/router.go- Router setupbackend/internal/iam/user.go- User and permission retrieval functions