ironmansoftware / powershell-universal

Issue tracker for PowerShell Universal
https://powershelluniversal.com
34 stars 2 forks source link

Pages: $Roles not working at the "Main App" Level #3451

Closed donlent closed 1 month ago

donlent commented 1 month ago

Version

4.3.2

Severity

High

Environment

msi

Steps to Reproduce

if ($Roles -contains "User") {
    $Navigation = @(
        New-UDListItem -Label "Home" -Href '/Home' -Icon (New-UDIcon -Icon "home" -Size lg)
        New-UDListItem -Label "My Connections" -Href '/My-Connections' -Icon (New-UDIcon -Icon "network-wired" -Size lg)
    )
}
if ($Roles -contains "Admin") {
    $Navigation += @(
        New-UDListItem -Label "Administration" -Icon (New-UDIcon -Icon "gears" -Size lg) -Children {
            New-UDListItem -Label "Customers" -Href '/Customers' -Icon (New-UDIcon -Icon "id-card-clip" -Size lg -Style @{"padding-left" = "15px" } )
            New-UDListItem -Label "Workloads" -Href '/Workloads' -Icon (New-UDIcon -Icon "desktop" -Size lg -Style @{"padding-left" = "15px" } )
        }
        New-UDListItem -Label "Logs" -Icon (New-UDIcon -Icon "file-lines" -Size lg) -Children {
            New-UDListItem -Label "Management Logs" -Href '/ManagementLogs' -Icon (New-UDIcon -Icon "file-shield" -Size lg -Style @{"padding-left" = "15px" } )
            New-UDListItem -Label "Customer Activity Logs" -Href '/CustomerActivityLogs' -Icon (New-UDIcon -Icon "clock-rotate-left" -Size lg -Style @{"padding-left" = "15px" } )
        }
    )
}

$Pages = @(
    Get-UDPage -Name "Home"
    Get-UDPage -Name "My Connections"
    Get-UDPage -Name 'Administration'
    Get-UDPage -Name 'Customers'
    Get-UDPage -Name 'Workloads'
    Get-UDPage -Name 'ManagementLogs'
    Get-UDPage -Name 'CustomerActivityLogs'
)

New-UDApp -Title 'Test' -Pages $Pages -Theme $Theme -Navigation $Navigation

Expected behavior

The $Roles Variables should be always available, after a user authenticates.

Actual behavior

The $Roles Variable is not available at the Main "Apps" Stage. Only inside other "Sub Pages" you can enumerate the Roles.

Additional Environment data

No response

Screenshots/Animations

No response

adamdriscoll commented 1 month ago

This is by design. The main script only runs on app startup so there are no roles defined.

Use -LoadNavigation to work around this.

https://docs.powershelluniversal.com/userinterfaces/components/pages#dynamic-navigation

$LoadNavigation = {
if ($Roles -contains "User") {
    $Navigation = @(
        New-UDListItem -Label "Home" -Href '/Home' -Icon (New-UDIcon -Icon "home" -Size lg)
        New-UDListItem -Label "My Connections" -Href '/My-Connections' -Icon (New-UDIcon -Icon "network-wired" -Size lg)
    )
}
if ($Roles -contains "Admin") {
    $Navigation += @(
        New-UDListItem -Label "Administration" -Icon (New-UDIcon -Icon "gears" -Size lg) -Children {
            New-UDListItem -Label "Customers" -Href '/Customers' -Icon (New-UDIcon -Icon "id-card-clip" -Size lg -Style @{"padding-left" = "15px" } )
            New-UDListItem -Label "Workloads" -Href '/Workloads' -Icon (New-UDIcon -Icon "desktop" -Size lg -Style @{"padding-left" = "15px" } )
        }
        New-UDListItem -Label "Logs" -Icon (New-UDIcon -Icon "file-lines" -Size lg) -Children {
            New-UDListItem -Label "Management Logs" -Href '/ManagementLogs' -Icon (New-UDIcon -Icon "file-shield" -Size lg -Style @{"padding-left" = "15px" } )
            New-UDListItem -Label "Customer Activity Logs" -Href '/CustomerActivityLogs' -Icon (New-UDIcon -Icon "clock-rotate-left" -Size lg -Style @{"padding-left" = "15px" } )
        }
    )
}
$Navigation
}

$Pages = @(
    Get-UDPage -Name "Home"
    Get-UDPage -Name "My Connections"
    Get-UDPage -Name 'Administration'
    Get-UDPage -Name 'Customers'
    Get-UDPage -Name 'Workloads'
    Get-UDPage -Name 'ManagementLogs'
    Get-UDPage -Name 'CustomerActivityLogs'
)

New-UDApp -Title 'Test' -Pages $Pages -Theme $Theme -LoadNavigation $LoadNavigation
donlent commented 1 month ago

Thank you so much @adamdriscoll !! :-D