Skip to content

Commit 4429b09

Browse files
refactor(sharepoint): enhance template handling in API and frontend
- Updated backend logic to handle TemplateId more robustly, ensuring proper GUID generation and existing template retrieval. - Improved frontend logic to differentiate between edit and copy modes, ensuring accurate template data fetching and form hydration. - Adjusted API calls to prevent stale data from being displayed during template editing and copying. Synced from CyberDrain/CIPP@793915b
1 parent 9990315 commit 4429b09

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Teams-Sharepoint/Invoke-ExecSharePointTemplate.ps1

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,23 @@ function Invoke-ExecSharePointTemplate {
3232
break
3333
}
3434

35-
$GUID = $Request.Body.TemplateId ?? (New-Guid).GUID
35+
# Frontend sends TemplateId only when editing (add.js). Create/copy omit it.
36+
$GUID = $Request.Body.TemplateId
37+
if ([string]::IsNullOrWhiteSpace([string]$GUID)) {
38+
$GUID = (New-Guid).GUID
39+
$Existing = $null
40+
} else {
41+
$GUID = [string]$GUID
42+
$Existing = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'SharePointTemplate' and RowKey eq '$GUID'"
43+
}
3644

37-
# Keep only the template payload; strip transport/metadata fields.
38-
$TemplateObject = $Request.Body | Select-Object -Property * -ExcludeProperty Action, TemplateId
45+
# Never trust client-supplied audit fields.
46+
$TemplateObject = $Request.Body | Select-Object -Property * -ExcludeProperty Action, TemplateId, GUID, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn
3947

40-
# Stamp audit metadata. CreatedBy/On only on first save (or preserved from existing
41-
# JSON on edit — the form does not round-trip those fields). UpdatedBy/On every save.
42-
if ($Request.Body.TemplateId) {
43-
$Existing = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'SharePointTemplate' and RowKey eq '$GUID'"
44-
if ($Existing?.JSON) {
45-
$ExistingData = $Existing.JSON | ConvertFrom-Json
46-
if ($ExistingData.CreatedBy) {
47-
$TemplateObject | Add-Member -NotePropertyName 'CreatedBy' -NotePropertyValue $ExistingData.CreatedBy -Force
48-
$TemplateObject | Add-Member -NotePropertyName 'CreatedOn' -NotePropertyValue $ExistingData.CreatedOn -Force
49-
} else {
50-
$TemplateObject | Add-Member -NotePropertyName 'CreatedBy' -NotePropertyValue ($User.userDetails ?? 'CIPP-API') -Force
51-
$TemplateObject | Add-Member -NotePropertyName 'CreatedOn' -NotePropertyValue (Get-Date).ToString('o') -Force
52-
}
53-
} else {
54-
$TemplateObject | Add-Member -NotePropertyName 'CreatedBy' -NotePropertyValue ($User.userDetails ?? 'CIPP-API') -Force
55-
$TemplateObject | Add-Member -NotePropertyName 'CreatedOn' -NotePropertyValue (Get-Date).ToString('o') -Force
56-
}
48+
if ($Existing) {
49+
$ExistingData = $Existing.JSON | ConvertFrom-Json
50+
$TemplateObject | Add-Member -NotePropertyName 'CreatedBy' -NotePropertyValue ($ExistingData.CreatedBy ?? $User.userDetails ?? 'CIPP-API') -Force
51+
$TemplateObject | Add-Member -NotePropertyName 'CreatedOn' -NotePropertyValue ($ExistingData.CreatedOn ?? (Get-Date).ToString('o')) -Force
5752
} else {
5853
$TemplateObject | Add-Member -NotePropertyName 'CreatedBy' -NotePropertyValue ($User.userDetails ?? 'CIPP-API') -Force
5954
$TemplateObject | Add-Member -NotePropertyName 'CreatedOn' -NotePropertyValue (Get-Date).ToString('o') -Force

0 commit comments

Comments
 (0)