Private/Get-RBACKnowledgeBase.ps1
|
function Get-RBACKnowledgeBase { <# .SYNOPSIS Loads a PSAutoRBAC data file (knowledge base) from disk, cached per path. .DESCRIPTION Internal. Imports a .psd1 from the module's Data directory and caches the parsed hashtable for the lifetime of the module session. Used for both the command -> requirement map (offline fallback) and the role -> action map (offline action-to-role reverse mapping). .PARAMETER Name The data file base name, e.g. 'CommandRoleMap' or 'RoleActionMap'. .PARAMETER Path Optional explicit path, primarily for testing. Overrides -Name. .OUTPUTS System.Collections.Hashtable #> [CmdletBinding()] [OutputType([hashtable])] param( [Parameter()] [string]$Name = 'CommandRoleMap', [Parameter()] [string]$Path ) if (-not $Path) { $Path = Join-Path -Path $script:ModuleRoot -ChildPath "Data/$Name.psd1" } Write-PSFMessage -Level Verbose -Message "Loading knowledge base '$Name' from '$Path'." -Tag 'PSAutoRBAC', 'KnowledgeBase' if (-not (Test-Path -LiteralPath $Path)) { Write-PSFMessage -Level Error -Message "Data file not found at '$Path'." -Tag 'PSAutoRBAC', 'KnowledgeBase' throw "PSAutoRBAC data file not found at '$Path'." } if (-not (Get-Variable -Name 'RBACDataCache' -Scope Script -ErrorAction SilentlyContinue)) { $script:RBACDataCache = @{} } $resolved = (Resolve-Path -LiteralPath $Path).ProviderPath if ($script:RBACDataCache.ContainsKey($resolved)) { Write-PSFMessage -Level Debug -Message "Cache hit for '$resolved'." -Tag 'PSAutoRBAC', 'KnowledgeBase' } else { Write-PSFMessage -Level Debug -Message "Cache miss; importing '$resolved'." -Tag 'PSAutoRBAC', 'KnowledgeBase' $script:RBACDataCache[$resolved] = Import-PowerShellDataFile -LiteralPath $resolved Write-PSFMessage -Level Debug -Message "Imported $($script:RBACDataCache[$resolved].Keys.Count) top-level key(s) from '$Name'." -Tag 'PSAutoRBAC', 'KnowledgeBase' } return $script:RBACDataCache[$resolved] } |