What we want
For anon, we want the top menu to be:
Admins have user list links, a logout link, and no login link:
Managers have sales list links, a logout link, and no login link:
The top menu
Here's code for that. It's Boostrap navbar code.
- <ul class="navbar-nav mr-auto">
- <li class="nav-item">
- <a class="nav-link" href="products.php" title="Our products">Products</a>
- </li>
- <?php
- if (hasRole(ROLE_NAME_MANAGER)) {
- ?>
- <li class="nav-item">
- <a class="nav-link" href="sales.php" title="Sales">Sales</a>
- </li>
- <?php
- }
- if (hasRole(ROLE_NAME_ADMIN)) {
- ?>
- <li class="nav-item">
- <a class="nav-link" href="users.php" title="User accounts">Users</a>
- </li>
- <?php
- }
- ?>
- <li class="nav-item">
- <?php
- if (isLoggedIn()) {
- print "<a class='nav-link' href='logout.php' title='Logout'>Logout</a>";
- }
- else {
- print "<a class='nav-link' href='login.php' title='Login'>Login</a>";
- }
- ?>
- </li>
- </ul>
The products link is always there, no matter who the user (lines 2 to 4).
The sales link is output for people with the manager role:
- if (hasRole(ROLE_NAME_MANAGER)) {
- ?>
- <li class="nav-item">
- <a class="nav-link" href="sales.php" title="Sales">Sales</a>
- </li>
- <?php
- }
Don't have the manager role? The output is skipped. We'll need to make the program sales.php
to show the sales.
The users link shows for people with the admin role (lines 13 to 19).
The login/logout links are done in a similar way.
- If the user is logged in, you want to show the logout link, but not the login link (since they're already logged in).
- If the user is not logged in, you want to show the login link, but not the logout link (since they're not logged in, they can't logout).
- if (isLoggedIn()) {
- print "<a class='nav-link' href='logout.php' title='Logout'>Logout</a>";
- }
- else {
- print "<a class='nav-link' href='login.php' title='Login'>Login</a>";
- }
Instead of testing the role, the code tests whether the user is logged in, or not.
We'll make login.php
and logout.php
soon.
Home page link list
The home page has links, too. For example, for admins, there are links to products and users:
The code for that is similar to that for the top menu, just without the navbar stuff:
- <li>
- <a href="products.php">Products</a>
- </li>
- <?php
- if (hasRole(ROLE_NAME_MANAGER)) {
- ?>
- <li>
- <a href="sales.php" title="Sales">Sales</a>
- </li>
- <?php
- }
- if (hasRole(ROLE_NAME_ADMIN)) {
- ?>
- <li>
- <a href="users.php" title="User accounts">Users</a>
- </li>
- <?php
- }
The link to the products report is always there. The other two links depend on the user's role.
Up next
Let's make a DB table to store user data in.