This commit is contained in:
@@ -213,29 +213,42 @@ function Handle-FetchSites {
|
|||||||
$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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user