Navigation menu with Roles

@Support

3.0.3 - I was using the Navigation with New-UDListItem. I would assume that I could create a menu based on roles by if statement. I attempted this with the code below and nothing executes inside the if statement.

I assumed the $Roles variable could be accessed from anywhere in the script, but I believe this is not the case. I tried to put the text of the menu option to $Roles and it came up blank. That makes sense why the if statement is not executing. I verified the $Roles variable is not null, by displaying it on the dashboard.

Is this method not supported? Would Navigation support not displaying if I attach -Role to the page?

$Navigation = @(
    New-UDListItem -Label "Home"
    New-UDListItem -Label 'Printer' -Icon (New-UDIcon -Icon 'Print') -OnClick { 
        Invoke-UDRedirect '/Printer'
    }
    if ($Roles -contains 'Administrator') {
        New-UDListItem -Label "Admin Area" -Children {
            New-UDListItem -Label ">> Settings" -OnClick { Invoke-UDRedirect '/settings' }
            New-UDListItem -Label ">> Usage" -OnClick { Invoke-UDRedirect '/usage' }
            New-UDListItem -Label ">> Logging" -OnClick { Invoke-UDRedirect '/logging' }
        }
    }
)


$Pages = @()
$Pages += New-UDPage -Name 'My Home Page' -Content {} -NavigationLayout permanent -Navigation $Navigation
$Pages += New-UDPage -Name 'Printer' -Content { 
    New-UDTypography -Text "Roles: $Roles"

} -NavigationLayout permanent -Navigation $Navigation
New-UDDashboard -Pages $Pages -Title 'Dashboard'
Product: PowerShell Universal
Version: 3.0.3

I build my menu navigation similarly but with slightly different syntax, I include a $Navigation scriptblock in my main/first pageā€¦

$Navigation = {
    New-UDStyle -Style "
		div.MuiListItemIcon-root {		
	   		min-width: 35px;
   		}
		.MuiSvgIcon-colorPrimary {
			color: $cache:logoGreen
		}
	" -Content {
		New-UDListItem -SubTitle $(Get-Date -Format "dd MMMM yyyy")
		
        New-UDListItem -Label 'Home' -Icon (New-UDIcon -Icon flask -Size lg -Color $cache:logoDarkBlue) -OnClick {
			Invoke-UDRedirect -Url '/Home'
		}

		New-UDListItem -Label 'Item1' -Icon (New-UDIcon -Icon FilePdf -Size lg -Color $cache:logoDarkBlue) -OnClick {
			Invoke-UDRedirect -Url '/Item1'
		}

		New-UDListItem -Label 'Help' -Icon (New-UDIcon -Icon hands_helping -Size lg -Color $cache:logoDarkBlue) -Children {
			New-UDStyle -Style '
					{		
						padding-left: 30px;
					}
            ' -Content {
                New-UDListItem -Label 'Help 1' -Icon (New-UDIcon -Icon caret_right -Size lg -Color $cache:logoDarkBlue) -OnClick {
                    Invoke-UDRedirect -Url 'https://h1.co.nz/h1' -OpenInNewWindow
                }
                New-UDListItem -Label 'Help 1' -Icon (New-UDIcon -Icon caret_right -Size lg -Color $cache:logoDarkBlue) -OnClick {
                    Invoke-UDRedirect -Url 'https://h2.co.nz/h2' -OpenInNewWindow
                }			
            }
		}
		

		# SUPEVISORS AND ADMINS
		If(($Roles -contains $cache:DashboardSupervisorPSURoleName) -Or ($Roles -contains $cache:DashboardAdministratorPSURoleName)) {
			New-UDListItem -Label 'System' -Icon (New-UDIcon -Icon windows -Size lg -Color $cache:logoDarkBlue) -Children {
				New-UDStyle -Style '
					{		
						padding-left: 30px;
					}
				' -Content {
					New-UDListItem -Label 'SysItem' -Icon (New-UDIcon -Icon caret_right -Size lg -Color $cache:logoDarkBlue) -OnClick {
						Invoke-UDRedirect -Url '/SysItem'
					}					
				}
			}
		} # end SUPEVISORS AND ADMINS


		# ADMINS ONLY SECTION
		If($Roles -contains $cache:DashboardAdministratorPSURoleName) {
			New-UDListItem -Label 'Administrators' -Icon (New-UDIcon -Icon UserShield -Size lg -Color $cache:logoDarkBlue)  -Children {
				
				New-UDStyle -Style '
					{		
						padding-left: 30px;
					}
				' -Content {
					New-UDListItem -Label 'Admin1' -Icon (New-UDIcon -Icon caret_right -Size lg -Color $cache:logoDarkBlue) -OnClick {
						Invoke-UDRedirect -Url '/Admin1'
					}
					New-UDListItem -Label 'Admin2' -Icon (New-UDIcon -Icon caret_right -Size lg -Color $cache:logoDarkBlue) -OnClick {
						Invoke-UDRedirect -Url '/Admin2'
					}
				}
			}
		} # end ADMINS ONLY SECTION

	} # end UDStyle

} # end Navigation scriptblock

And then when building my pages load that navigationā€¦

New-UDPage -Name "SysItem" -Content {
    <...>
} -NavigationLayout Permanent -LoadNavigation $Navigation -Role @("Administrator","Supervisor") # end this page

(adding extra role permission in case someone knew the URL to the page to circumvent the navigation menu it wonā€™t load unless the person is in that role)

Whether this is the best way or not - itā€™s working for me at the moment :slight_smile:

1 Like

-LoadNavigation is the correct way because it runs dynamically rather than statically so it can evaluate the roles.

1 Like

Thank you @adam & @pharnos

This fixed the issue, I must have read the documentation wrong, sorry about that! :smiley:

Looking to do something similar but include the navigation to other dashboards.

Havenā€™t been able to get the 3rd item in this code to show up without any roles

$Navigation = New-UDSideNav -Content{
New-UDSideNavItem -Text ā€˜Decomsā€™ -PageName ā€œDecomsā€
New-UDSideNavItem -Text ā€˜Staged Decomsā€™ -PageName ā€œStageDecomā€
New-UDSideNavItem -Divider
New-UDSideNavItem -Text ā€˜Approvalsā€™ -Url ā€˜https://dashboardtest/Approvals/ā€™
}

New-UDDashboard -Title ā€œDecomsā€ -Pages $Pages -Theme $Theme -Navigation $Navigation