This commit is contained in:
2025-05-26 23:38:52 -04:00
parent dc8cbe1cae
commit 085078c808

View File

@@ -210,32 +210,45 @@ function Handle-FetchSites {
Write-Host "[Debug] Handle-FetchSites invoked" Write-Host "[Debug] Handle-FetchSites invoked"
try { try {
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd() $body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
$pw = (ConvertFrom-Json $body).password $pw = (ConvertFrom-Json $body).password
# Securely get Datto API credentials
$creds = Get-DattoApiCredentials -Password $pw $creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) { if (-not $creds) {
Write-LogHybrid "Credential fetch failed." "Error" Write-LogHybrid "Failed to fetch credentials." "Error" "Server"
$Context.Response.StatusCode = 403 $Context.Response.StatusCode = 500
Respond-JSON $Context @() $Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return return
} }
# Fetch the site list using the helper
$sites = Install-DattoRMM-Helper ` $sites = Install-DattoRMM-Helper `
-ApiUrl $creds.ApiUrl ` -ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey ` -ApiKey $creds.ApiKey `
-ApiSecretKey $creds.ApiSecretKey ` -ApiSecretKey $creds.ApiSecretKey `
-FetchSitesOnly -FetchSitesOnly
if (-not $sites) { if (-not $sites) {
$Context.Response.StatusCode = 500 $Context.Response.StatusCode = 500
$sites = @() $Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return
} }
Respond-JSON $Context $sites # Serialize and respond
$json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = "application/json"
$Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
} catch { } catch {
Write-LogHybrid "Handle-FetchSites failed: $($_.Exception.Message)" "Error" "Server"
$Context.Response.StatusCode = 500 $Context.Response.StatusCode = 500
Respond-JSON $Context @() $Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
} finally {
$Context.Response.OutputStream.Close()
} }
} }