Skip to content

Commit f484ba0

Browse files
authored
Merge pull request #464 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents a4b81a2 + a43d01a commit f484ba0

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecCustomRole.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ function Invoke-ExecCustomRole {
6161
$Body = @{Results = "Failed to save custom role $($Request.Body.RoleName)" }
6262
}
6363
}
64+
'Clone' {
65+
try {
66+
if ($Request.Body.NewRoleName -in $DefaultRoles) {
67+
throw "Role name $($Request.Body.NewRoleName) cannot be used"
68+
}
69+
$ExistingRole = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$($Request.Body.RoleName.ToLower())'"
70+
if (!$ExistingRole) {
71+
throw "Role $($Request.Body.RoleName) not found"
72+
}
73+
74+
if ($ExistingRole.RowKey -eq $Request.Body.NewRoleName.ToLower()) {
75+
throw "New role name cannot be the same as the existing role name"
76+
}
77+
78+
$NewRoleTest = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$($Request.Body.NewRoleName.ToLower())'"
79+
if ($NewRoleTest) {
80+
throw "Role name $($Request.Body.NewRoleName) already exists"
81+
}
82+
83+
$NewRole = @{
84+
'PartitionKey' = 'CustomRoles'
85+
'RowKey' = "$($Request.Body.NewRoleName.ToLower())"
86+
'Permissions' = $ExistingRole.Permissions
87+
'AllowedTenants' = $ExistingRole.AllowedTenants
88+
'BlockedTenants' = $ExistingRole.BlockedTenants
89+
'BlockedEndpoints' = $ExistingRole.BlockedEndpoints
90+
}
91+
Add-CIPPAzDataTableEntity @Table -Entity $NewRole -Force | Out-Null
92+
$Body = @{Results = "Custom role '$($Request.Body.NewRoleName)' cloned from '$($Request.Body.RoleName)'" }
93+
Write-LogMessage -headers $Request.Headers -API 'ExecCustomRole' -message "Cloned custom role $($Request.Body.RoleName) to $($Request.Body.NewRoleName)" -Sev 'Info'
94+
} catch {
95+
Write-Warning "Failed to clone custom role $($Request.Body.RoleName): $($_.Exception.Message)"
96+
Write-Warning $_.InvocationInfo.PositionMessage
97+
$Body = @{Results = "Failed to clone custom role $($Request.Body.RoleName)" }
98+
}
99+
}
64100
'Delete' {
65101
Write-Information "Deleting custom role $($Request.Body.RoleName)"
66102
$Role = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$($Request.Body.RoleName)'" -Property RowKey, PartitionKey

Modules/CIPPCore/Public/Get-CIPPTextReplacement.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function Get-CIPPTextReplacement {
1313
#>
1414
param (
1515
[string]$TenantFilter,
16-
$Text
16+
$Text,
17+
[switch]$EscapeForJson
1718
)
1819
if ($Text -isnot [string]) {
1920
return $Text
@@ -54,13 +55,21 @@ function Get-CIPPTextReplacement {
5455
$Vars = @{}
5556
if ($GlobalMap) {
5657
foreach ($Var in $GlobalMap) {
58+
if ($EscapeForJson.IsPresent) {
59+
# Escape quotes for JSON if not already escaped
60+
$Var.Value = $Var.Value -replace '(?<!\\)"', '\"'
61+
}
5762
$Vars[$Var.RowKey] = $Var.Value
5863
}
5964
}
6065
# Tenant Specific Variables
6166
$ReplaceMap = Get-CIPPAzDataTableEntity @ReplaceTable -Filter "PartitionKey eq '$CustomerId'"
6267
if ($ReplaceMap) {
6368
foreach ($Var in $ReplaceMap) {
69+
if ($EscapeForJson.IsPresent) {
70+
# Escape quotes for JSON if not already escaped
71+
$Var.Value = $Var.Value -replace '(?<!\\)"', '\"'
72+
}
6473
$Vars[$Var.RowKey] = $Var.Value
6574
}
6675
}

Modules/CIPPCore/Public/GraphHelper/New-GraphPOSTRequest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function New-GraphPOSTRequest ($uri, $tenantid, $body, $type, $scope, $AsApp, $N
2222
$contentType = 'application/json; charset=utf-8'
2323
}
2424
try {
25-
$body = Get-CIPPTextReplacement -TenantFilter $tenantid -Text $body
25+
$body = Get-CIPPTextReplacement -TenantFilter $tenantid -Text $body -EscapeForJson
2626
$ReturnedData = (Invoke-RestMethod -Uri $($uri) -Method $TYPE -Body $body -Headers $headers -ContentType $contentType -SkipHttpErrorCheck:$IgnoreErrors -ResponseHeadersVariable responseHeaders)
2727
} catch {
2828
$Message = if ($_.ErrorDetails.Message) {

0 commit comments

Comments
 (0)