Skip to content

Commit 81b2ffc

Browse files
branding api
1 parent c57970a commit 81b2ffc

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 Settings
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+
break
55+
}
56+
}
57+
58+
if ($Request.Body.logo) {
59+
$Logo = $Request.Body.logo
60+
if ($Logo -match '^data:image\/') {
61+
$Base64Data = $Logo -replace '^data:image\/[^;]+;base64,', ''
62+
try {
63+
$ImageBytes = [Convert]::FromBase64String($Base64Data)
64+
if ($ImageBytes.Length -le 2097152) {
65+
$BrandingConfig.logo = $Logo
66+
$Updated = $true
67+
} else {
68+
$StatusCode = [HttpStatusCode]::BadRequest
69+
$Results = 'Error: Image size must be less than 2MB'
70+
break
71+
}
72+
} catch {
73+
$StatusCode = [HttpStatusCode]::BadRequest
74+
$Results = 'Error: Invalid base64 image data'
75+
break
76+
}
77+
} elseif ($Logo -eq $null -or $Logo -eq '') {
78+
$BrandingConfig.logo = $null
79+
$Updated = $true
80+
}
81+
}
82+
83+
if ($Updated) {
84+
$BrandingConfig.PartitionKey = 'BrandingSettings'
85+
$BrandingConfig.RowKey = 'BrandingSettings'
86+
87+
Add-CIPPAzDataTableEntity @Table -Entity $BrandingConfig -Force | Out-Null
88+
Write-LogMessage -API $APIName -tenant 'Global' -headers $Request.Headers -message 'Updated branding settings' -Sev 'Info'
89+
$Results = 'Successfully updated branding settings'
90+
} else {
91+
$StatusCode = [HttpStatusCode]::BadRequest
92+
$Results = 'Error: No valid branding data provided'
93+
}
94+
}
95+
'Reset' {
96+
$DefaultConfig = @{
97+
PartitionKey = 'BrandingSettings'
98+
RowKey = 'BrandingSettings'
99+
colour = '#F77F00'
100+
logo = $null
101+
}
102+
103+
Add-CIPPAzDataTableEntity @Table -Entity $DefaultConfig -Force | Out-Null
104+
Write-LogMessage -API $APIName -tenant 'Global' -headers $Request.Headers -message 'Reset branding settings to defaults' -Sev 'Info'
105+
$Results = 'Successfully reset branding settings to defaults'
106+
}
107+
default {
108+
$StatusCode = [HttpStatusCode]::BadRequest
109+
$Results = 'Error: Invalid action specified'
110+
}
111+
}
112+
} catch {
113+
Write-LogMessage -API $APIName -tenant 'Global' -headers $Request.Headers -message "Branding Settings API failed: $($_.Exception.Message)" -Sev 'Error'
114+
$StatusCode = [HttpStatusCode]::InternalServerError
115+
$Results = "Failed to process branding settings: $($_.Exception.Message)"
116+
}
117+
118+
$body = [pscustomobject]@{'Results' = $Results }
119+
120+
# Associate values to output bindings by calling 'Push-OutputBinding'.
121+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
122+
StatusCode = $StatusCode
123+
Body = $body
124+
})
125+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Users/Invoke-ListUserSettings.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ function Invoke-ListUserSettings {
2020
$UserSettings = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq 'allUsers'"
2121
if (!$UserSettings) { $UserSettings = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$Username'" }
2222
$UserSettings = $UserSettings.JSON | ConvertFrom-Json -Depth 10 -ErrorAction SilentlyContinue
23+
#Get branding settings
24+
if ($UserSettings) {
25+
$brandingTable = Get-CippTable -tablename 'Config'
26+
$BrandingSettings = Get-CIPPAzDataTableEntity @brandingTable -Filter "RowKey eq 'BrandingSettings'"
27+
if ($BrandingSettings) {
28+
$UserSettings | Add-Member -MemberType NoteProperty -Name 'BrandingSettings' -Value $BrandingSettings -Force | Out-Null
29+
}
30+
}
2331
$StatusCode = [HttpStatusCode]::OK
2432
$Results = $UserSettings
2533
} catch {

0 commit comments

Comments
 (0)