Skip to content

Commit e94aa1a

Browse files
authored
Merge pull request #465 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents f484ba0 + 21ce75c commit e94aa1a

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ function Invoke-ExecCustomRole {
1212
$AccessRoleGroupTable = Get-CippTable -tablename 'AccessRoleGroups'
1313
$Action = $Request.Query.Action ?? $Request.Body.Action
1414

15-
$DefaultRoles = @('readonly', 'editor', 'admin', 'superadmin')
15+
$CIPPCore = (Get-Module -Name CIPPCore).ModuleBase
16+
$CIPPRoot = (Get-Item -Path $CIPPCore).Parent.Parent.FullName
17+
18+
$CippRolesJson = Join-Path -Path $CIPPRoot -ChildPath 'Config\cipp-roles.json'
19+
if (Test-Path $CippRolesJson) {
20+
$DefaultRoles = Get-Content -Path $CippRolesJson | ConvertFrom-Json
21+
} else {
22+
throw "Could not find $CippRolesJson"
23+
}
24+
1625
$BlockedRoles = @('anonymous', 'authenticated')
1726

1827
if ($Request.Body.RoleName -in $BlockedRoles) {
@@ -24,7 +33,7 @@ function Invoke-ExecCustomRole {
2433
try {
2534
$Results = [System.Collections.Generic.List[string]]::new()
2635
Write-LogMessage -headers $Request.Headers -API 'ExecCustomRole' -message "Saved custom role $($Request.Body.RoleName)" -Sev 'Info'
27-
if ($Request.Body.RoleName -notin $DefaultRoles) {
36+
if ($Request.Body.RoleName -notin $DefaultRoles.PSObject.Properties.Name) {
2837
$Role = @{
2938
'PartitionKey' = 'CustomRoles'
3039
'RowKey' = "$($Request.Body.RoleName.ToLower())"
@@ -63,7 +72,7 @@ function Invoke-ExecCustomRole {
6372
}
6473
'Clone' {
6574
try {
66-
if ($Request.Body.NewRoleName -in $DefaultRoles) {
75+
if ($Request.Body.NewRoleName -in $DefaultRoles.PSObject.Properties.Name) {
6776
throw "Role name $($Request.Body.NewRoleName) cannot be used"
6877
}
6978
$ExistingRole = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$($Request.Body.RoleName.ToLower())'"
@@ -72,7 +81,7 @@ function Invoke-ExecCustomRole {
7281
}
7382

7483
if ($ExistingRole.RowKey -eq $Request.Body.NewRoleName.ToLower()) {
75-
throw "New role name cannot be the same as the existing role name"
84+
throw 'New role name cannot be the same as the existing role name'
7685
}
7786

7887
$NewRoleTest = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$($Request.Body.NewRoleName.ToLower())'"
@@ -127,13 +136,13 @@ function Invoke-ExecCustomRole {
127136
try {
128137
$Role.Permissions = $Role.Permissions | ConvertFrom-Json
129138
} catch {
130-
$Role.Permissions = ''
139+
$Role.Permissions = @()
131140
}
132141
if ($Role.AllowedTenants) {
133142
try {
134143
$Role.AllowedTenants = @($Role.AllowedTenants | ConvertFrom-Json)
135144
} catch {
136-
$Role.AllowedTenants = ''
145+
$Role.AllowedTenants = @()
137146
}
138147
} else {
139148
$Role | Add-Member -NotePropertyName AllowedTenants -NotePropertyValue @() -Force
@@ -142,7 +151,7 @@ function Invoke-ExecCustomRole {
142151
try {
143152
$Role.BlockedTenants = @($Role.BlockedTenants | ConvertFrom-Json)
144153
} catch {
145-
$Role.BlockedTenants = ''
154+
$Role.BlockedTenants = @()
146155
}
147156
} else {
148157
$Role | Add-Member -NotePropertyName BlockedTenants -NotePropertyValue @() -Force
@@ -151,7 +160,7 @@ function Invoke-ExecCustomRole {
151160
try {
152161
$Role.BlockedEndpoints = @($Role.BlockedEndpoints | ConvertFrom-Json)
153162
} catch {
154-
$Role.BlockedEndpoints = ''
163+
$Role.BlockedEndpoints = @()
155164
}
156165
} else {
157166
$Role | Add-Member -NotePropertyName BlockedEndpoints -NotePropertyValue @() -Force
@@ -164,13 +173,13 @@ function Invoke-ExecCustomRole {
164173
}
165174
$Role
166175
}
167-
$DefaultRoles = foreach ($DefaultRole in $DefaultRoles) {
176+
$DefaultRoles = foreach ($DefaultRole in $DefaultRoles.PSObject.Properties.Name) {
168177
$Role = @{
169178
RowKey = $DefaultRole
170-
Permissions = ''
179+
Permissions = $DefaultRoles.$DefaultRole
171180
AllowedTenants = @('AllTenants')
172-
BlockedTenants = @('')
173-
BlockedEndpoints = @('')
181+
BlockedTenants = @()
182+
BlockedEndpoints = @()
174183
}
175184
$EntraRoleGroup = $EntraRoleGroups | Where-Object -Property RowKey -EQ $Role.RowKey
176185
if ($EntraRoleGroup) {

0 commit comments

Comments
 (0)