|
| 1 | +using namespace System.Net |
| 2 | + |
| 3 | +Function Invoke-ExecBrandingSettings { |
| 4 | + <# |
| 5 | + .FUNCTIONALITY |
| 6 | + Entrypoint |
| 7 | + .ROLE |
| 8 | + CIPP.AppSettings.ReadWrite |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param($Request, $TriggerMetadata) |
| 12 | + |
| 13 | + $APIName = $Request.Params.CIPPEndpoint |
| 14 | + $Headers = $Request.Headers |
| 15 | + Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug' |
| 16 | + |
| 17 | + $StatusCode = [HttpStatusCode]::OK |
| 18 | + $Results = @{} |
| 19 | + |
| 20 | + try { |
| 21 | + $Table = Get-CIPPTable -TableName Config |
| 22 | + $Filter = "PartitionKey eq 'BrandingSettings' and RowKey eq 'BrandingSettings'" |
| 23 | + $BrandingConfig = Get-CIPPAzDataTableEntity @Table -Filter $Filter |
| 24 | + |
| 25 | + if (-not $BrandingConfig) { |
| 26 | + $BrandingConfig = @{ |
| 27 | + PartitionKey = 'BrandingSettings' |
| 28 | + RowKey = 'BrandingSettings' |
| 29 | + colour = '#F77F00' |
| 30 | + logo = $null |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + $Action = if ($Request.Body.Action) { $Request.Body.Action } else { $Request.Query.Action } |
| 35 | + |
| 36 | + switch ($Action) { |
| 37 | + 'Get' { |
| 38 | + $Results = @{ |
| 39 | + colour = $BrandingConfig.colour |
| 40 | + logo = $BrandingConfig.logo |
| 41 | + } |
| 42 | + } |
| 43 | + 'Set' { |
| 44 | + $Updated = $false |
| 45 | + |
| 46 | + if ($Request.Body.colour) { |
| 47 | + $Colour = $Request.Body.colour |
| 48 | + if ($Colour -match '^#[0-9A-Fa-f]{6}$') { |
| 49 | + $BrandingConfig.colour = $Colour |
| 50 | + $Updated = $true |
| 51 | + } else { |
| 52 | + $StatusCode = [HttpStatusCode]::BadRequest |
| 53 | + $Results = 'Error: Invalid color format. Please use hex format (e.g., #F77F00)' |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if ($Request.Body.logo) { |
| 58 | + $Logo = $Request.Body.logo |
| 59 | + if ($Logo -match '^data:image\/') { |
| 60 | + $Base64Data = $Logo -replace '^data:image\/[^;]+;base64,', '' |
| 61 | + try { |
| 62 | + $ImageBytes = [Convert]::FromBase64String($Base64Data) |
| 63 | + if ($ImageBytes.Length -le 2097152) { |
| 64 | + Write-Host 'updating logo' |
| 65 | + $BrandingConfig.logo = $Logo |
| 66 | + $Updated = $true |
| 67 | + } else { |
| 68 | + $StatusCode = [HttpStatusCode]::BadRequest |
| 69 | + $Results = 'Error: Image size must be less than 2MB' |
| 70 | + } |
| 71 | + } catch { |
| 72 | + $StatusCode = [HttpStatusCode]::BadRequest |
| 73 | + $Results = 'Error: Invalid base64 image data' |
| 74 | + } |
| 75 | + } elseif ($Logo -eq $null -or $Logo -eq '') { |
| 76 | + $BrandingConfig.logo = $null |
| 77 | + $Updated = $true |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if ($Updated) { |
| 82 | + $BrandingConfig.PartitionKey = 'BrandingSettings' |
| 83 | + $BrandingConfig.RowKey = 'BrandingSettings' |
| 84 | + |
| 85 | + Add-CIPPAzDataTableEntity @Table -Entity $BrandingConfig -Force | Out-Null |
| 86 | + Write-LogMessage -API $APIName -tenant 'Global' -headers $Request.Headers -message 'Updated branding settings' -Sev 'Info' |
| 87 | + $Results = 'Successfully updated branding settings' |
| 88 | + } else { |
| 89 | + $StatusCode = [HttpStatusCode]::BadRequest |
| 90 | + $Results = 'Error: No valid branding data provided' |
| 91 | + } |
| 92 | + } |
| 93 | + 'Reset' { |
| 94 | + $DefaultConfig = @{ |
| 95 | + PartitionKey = 'BrandingSettings' |
| 96 | + RowKey = 'BrandingSettings' |
| 97 | + colour = '#F77F00' |
| 98 | + logo = $null |
| 99 | + } |
| 100 | + |
| 101 | + Add-CIPPAzDataTableEntity @Table -Entity $DefaultConfig -Force | Out-Null |
| 102 | + Write-LogMessage -API $APIName -tenant 'Global' -headers $Request.Headers -message 'Reset branding settings to defaults' -Sev 'Info' |
| 103 | + $Results = 'Successfully reset branding settings to defaults' |
| 104 | + } |
| 105 | + default { |
| 106 | + $StatusCode = [HttpStatusCode]::BadRequest |
| 107 | + $Results = 'Error: Invalid action specified' |
| 108 | + } |
| 109 | + } |
| 110 | + } catch { |
| 111 | + Write-LogMessage -API $APIName -tenant 'Global' -headers $Request.Headers -message "Branding Settings API failed: $($_.Exception.Message)" -Sev 'Error' |
| 112 | + $StatusCode = [HttpStatusCode]::InternalServerError |
| 113 | + $Results = "Failed to process branding settings: $($_.Exception.Message)" |
| 114 | + } |
| 115 | + |
| 116 | + $body = [pscustomobject]@{'Results' = $Results } |
| 117 | + |
| 118 | + # Associate values to output bindings by calling 'Push-OutputBinding'. |
| 119 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 120 | + StatusCode = $StatusCode |
| 121 | + Body = $body |
| 122 | + }) |
| 123 | +} |
0 commit comments