Skip to content
This repository was archived by the owner on Jul 23, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion Modules/CIPPCore/Public/Authentication/Test-CIPPAccess.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ function Test-CIPPAccess {
$Tenants = Get-Tenants -IncludeErrors
$swTenantsLoad.Stop()
$AccessTimings['LoadTenants'] = $swTenantsLoad.Elapsed.TotalMilliseconds

$OwnTenantIds = @()
if ($Type -eq 'User' -and $User.userDetails -match '^[^@]+@(?<domain>[^@]+)$') {
$UserDomain = $Matches.domain
$MatchingOwnTenants = @(
$Tenants | Where-Object {
$_.defaultDomainName -eq $UserDomain -or $_.initialDomainName -eq $UserDomain
}
)

if ($MatchingOwnTenants.Count -eq 1) {
$OwnTenantIds = @($MatchingOwnTenants.customerId)
} elseif ($MatchingOwnTenants.Count -gt 1) {
Write-Warning "Could not resolve own tenant for '$($User.userDetails)' because '$UserDomain' matches multiple tenants."
}
}

$PermissionsFound = $false
$swRolePerms = [System.Diagnostics.Stopwatch]::StartNew()
$PermissionSet = foreach ($CustomRole in $CustomRoles) {
Expand Down Expand Up @@ -358,6 +375,8 @@ function Test-CIPPAccess {
Write-Warning "Failed to expand tenant group '$($AllowedItem.label)': $($_.Exception.Message)"
@()
}
} elseif ($AllowedItem -eq 'OwnTenant') {
$OwnTenantIds
} else {
$AllowedItem
}
Comment on lines +378 to 382
Expand Down Expand Up @@ -424,12 +443,20 @@ function Test-CIPPAccess {
# Check tenant level access
if (($Role.BlockedTenants | Measure-Object).Count -eq 0 -and $Role.AllowedTenants -contains 'AllTenants') {
$TenantAllowed = $true
} elseif ($TenantFilter -eq 'AllTenants' -and $Role.AllowedTenants -contains 'OwnTenant' -and $Role.AllowedTenants -notcontains 'AllTenants') {
$TenantAllowed = $false
Comment on lines +446 to +447
} elseif ($TenantFilter -eq 'AllTenants' -and $ApiRole -match 'Write$') {
$TenantAllowed = $false
} elseif ($TenantFilter -eq 'AllTenants' -and $ApiRole -match 'Read$') {
$TenantAllowed = $true
} else {
$Tenant = ($Tenants | Where-Object { $TenantFilter -eq $_.customerId -or $TenantFilter -eq $_.defaultDomainName }).customerId
$Tenant = (
$Tenants | Where-Object {
$TenantFilter -eq $_.customerId -or
$TenantFilter -eq $_.defaultDomainName -or
$TenantFilter -eq $_.initialDomainName
}
).customerId

# Expand allowed tenant groups to individual tenant IDs
$ExpandedAllowedTenants = foreach ($AllowedItem in $Role.AllowedTenants) {
Expand All @@ -441,6 +468,8 @@ function Test-CIPPAccess {
Write-Warning "Failed to expand allowed tenant group '$($AllowedItem.label)': $($_.Exception.Message)"
@()
}
} elseif ($AllowedItem -eq 'OwnTenant') {
$OwnTenantIds
} else {
$AllowedItem
}
Comment on lines +471 to 475
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Invoke-ListCustomRole {
$IPRanges = @()
}


$RoleList.Add([pscustomobject]@{
RoleName = $Role
Type = 'Built-In'
Expand All @@ -61,7 +62,13 @@ function Invoke-ListCustomRole {
if ($Role.AllowedTenants) {
try {
$AllowedTenants = $Role.AllowedTenants | ConvertFrom-Json -ErrorAction Stop | ForEach-Object {
if ($_ -is [PSCustomObject] -and $_.type -eq 'Group') {
if ($_ -eq 'OwnTenant') {
[PSCustomObject]@{
type = 'OwnTenant'
value = 'OwnTenant'
label = 'Own Tenant'
}
} elseif ($_ -is [PSCustomObject] -and $_.type -eq 'Group') {
Comment on lines 64 to +71
# Return group objects as-is for frontend display
[PSCustomObject]@{
type = 'Group'
Expand Down Expand Up @@ -146,4 +153,3 @@ function Invoke-ListCustomRole {
Body = ConvertTo-Json -InputObject $Body -Depth 5
})
}

Loading