forked from baron/baron-sso
feat: enforce tenant isolation for audit logs and enhance user list filtering for multi-tenant admins
This commit is contained in:
@@ -58,6 +58,8 @@ func (h *AuditHandler) CreateLog(c *fiber.Ctx) error {
|
||||
func (h *AuditHandler) ListLogs(c *fiber.Ctx) error {
|
||||
limit := c.QueryInt("limit", 50)
|
||||
cursorRaw := c.Query("cursor")
|
||||
requestedTenantID := c.Query("tenantId")
|
||||
|
||||
cursor, err := parseAuditCursor(cursorRaw)
|
||||
if err != nil {
|
||||
return errorJSON(c, fiber.StatusBadRequest, "Invalid cursor")
|
||||
@@ -67,7 +69,41 @@ func (h *AuditHandler) ListLogs(c *fiber.Ctx) error {
|
||||
return errorJSON(c, fiber.StatusServiceUnavailable, "Audit service unavailable")
|
||||
}
|
||||
|
||||
logs, err := h.repo.FindPage(c.Context(), limit+1, cursor)
|
||||
// [New] Role-based Filtering
|
||||
profile, _ := c.Locals("user_profile").(*domain.UserProfileResponse)
|
||||
var filterTenantID string
|
||||
|
||||
if profile != nil {
|
||||
if profile.Role == domain.RoleSuperAdmin {
|
||||
// Super Admin can see everything or filter by a specific tenant if requested
|
||||
filterTenantID = requestedTenantID
|
||||
} else if profile.Role == domain.RoleTenantAdmin {
|
||||
// Tenant Admin can only see their own tenant logs (or manageable ones)
|
||||
// For now, lock to their primary tenant or requested one IF it's in their manageable list
|
||||
if profile.TenantID != nil {
|
||||
filterTenantID = *profile.TenantID
|
||||
}
|
||||
|
||||
// If they requested a specific tenant, verify they can manage it
|
||||
if requestedTenantID != "" && requestedTenantID != filterTenantID {
|
||||
canManage := false
|
||||
for _, t := range profile.ManageableTenants {
|
||||
if t.ID == requestedTenantID {
|
||||
canManage = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !canManage {
|
||||
return errorJSON(c, fiber.StatusForbidden, "forbidden: cannot view logs for this tenant")
|
||||
}
|
||||
filterTenantID = requestedTenantID
|
||||
}
|
||||
} else {
|
||||
return errorJSON(c, fiber.StatusForbidden, "forbidden")
|
||||
}
|
||||
}
|
||||
|
||||
logs, err := h.repo.FindPage(c.Context(), limit+1, cursor, filterTenantID)
|
||||
if err != nil {
|
||||
return errorJSON(c, fiber.StatusInternalServerError, "Failed to retrieve audit logs")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user