@@ -12,20 +12,50 @@ function Test-CIPPAccess {
1212 # Get function help
1313 $FunctionName = ' Invoke-{0}' -f $Request.Params.CIPPEndpoint
1414
15+ $SwPermissions = [System.Diagnostics.Stopwatch ]::StartNew()
16+ if (-not $global :CIPPFunctionPermissions ) {
17+ $CIPPCoreModule = Get-Module - Name CIPPCore
18+ if ($CIPPCoreModule ) {
19+ $PermissionsFileJson = Join-Path $CIPPCoreModule.ModuleBase ' lib' ' data' ' function-permissions.json'
20+
21+ if (Test-Path $PermissionsFileJson ) {
22+ try {
23+ $jsonData = Get-Content - Path $PermissionsFileJson - Raw | ConvertFrom-Json - AsHashtable
24+ $global :CIPPFunctionPermissions = [System.Collections.Hashtable ]::new([StringComparer ]::OrdinalIgnoreCase)
25+ foreach ($key in $jsonData.Keys ) {
26+ $global :CIPPFunctionPermissions [$key ] = $jsonData [$key ]
27+ }
28+ Write-Information " Loaded $ ( $global :CIPPFunctionPermissions.Count ) function permissions from JSON cache"
29+ } catch {
30+ Write-Warning " Failed to load function permissions from JSON: $ ( $_.Exception.Message ) "
31+ }
32+ }
33+ }
34+ }
35+ $SwPermissions.Stop ()
36+ $AccessTimings [' FunctionPermissions' ] = $SwPermissions.Elapsed.TotalMilliseconds
37+
1538 if ($FunctionName -ne ' Invoke-me' ) {
1639 $swHelp = [System.Diagnostics.Stopwatch ]::StartNew()
17- try {
18- $Help = Get-Help $FunctionName - ErrorAction Stop
19- } catch {
20- Write-Warning " Function '$FunctionName ' not found"
40+ if ($global :CIPPFunctionPermissions -and $global :CIPPFunctionPermissions.ContainsKey ($FunctionName )) {
41+ $PermissionData = $global :CIPPFunctionPermissions [$FunctionName ]
42+ $APIRole = $PermissionData [' Role' ]
43+ $Functionality = $PermissionData [' Functionality' ]
44+ Write-Information " Loaded function permission data from cache for '$FunctionName ': Role='$APIRole ', Functionality='$Functionality '"
45+ } else {
46+ try {
47+ $Help = Get-Help $FunctionName - ErrorAction Stop
48+ $APIRole = $Help.Role
49+ $Functionality = $Help.Functionality
50+ Write-Information " Loaded function permission data via Get-Help for '$FunctionName ': Role='$APIRole ', Functionality='$Functionality '"
51+ } catch {
52+ Write-Warning " Function '$FunctionName ' not found"
53+ }
2154 }
2255 $swHelp.Stop ()
2356 $AccessTimings [' GetHelp' ] = $swHelp.Elapsed.TotalMilliseconds
2457 }
2558
26- # Check help for role
27- $APIRole = $Help.Role
28-
2959 # Get default roles from config
3060 $swRolesLoad = [System.Diagnostics.Stopwatch ]::StartNew()
3161 $CIPPCoreModuleRoot = Get-Module - Name CIPPCore | Select-Object - ExpandProperty ModuleBase
@@ -367,7 +397,7 @@ function Test-CIPPAccess {
367397 if (! $APIAllowed ) {
368398 throw " Access to this CIPP API endpoint is not allowed, you do not have the required permission: $APIRole "
369399 }
370- if (! $TenantAllowed -and $Help . Functionality -notmatch ' AnyTenant' ) {
400+ if (! $TenantAllowed -and $Functionality -notmatch ' AnyTenant' ) {
371401 throw ' Access to this tenant is not allowed'
372402 } else {
373403 return $true
@@ -405,12 +435,12 @@ function Test-CIPPAccess {
405435 }
406436 }
407437
408- if (! $TenantAllowed -and $Help . Functionality -notmatch ' AnyTenant' ) {
438+ if (! $TenantAllowed -and $Functionality -notmatch ' AnyTenant' ) {
409439
410440 if (! $APIAllowed ) {
411441 throw " Access to this CIPP API endpoint is not allowed, you do not have the required permission: $APIRole "
412442 }
413- if (! $TenantAllowed -and $Help . Functionality -notmatch ' AnyTenant' ) {
443+ if (! $TenantAllowed -and $Functionality -notmatch ' AnyTenant' ) {
414444 Write-Information " Tenant not allowed: $TenantFilter "
415445
416446 throw ' Access to this tenant is not allowed'
0 commit comments