Show/Hide options in dropdown menu according to user privilege

Josue Marin

I have two type of users in my site. Admins and Users. Those are stored in the table user under the name of kind.

With this I can validate the login session:

<body>
      <?php if(isset($_SESSION["user_id"])):?>
      <div class="wrapper">
         <div class="sidebar" data-color="blue">
            <div class="logo">
               <a href="./" class="simple-text">
               INDEX PAGE FOR BOTH ADMINS AND USERS
               </a>
            </div>
            <div class="sidebar-wrapper">
               <ul class="nav">
                  <li class="">
                     <a href="./">
                        <i class="fa fa-home"></i>
                        <p>Home</p>
                     </a>
                  </li>
                  <li>
                     <a href="./?view=tickets">
                        <i class="fa fa-ticket"></i>
                        <p>Tickets</p>
                     </a>
                  </li>
                  <li>
                     <a href="./?view=calendario">
                        <i class="fa fa-ticket"></i>
                        <p>Calendar</p>
                     </a>
                  </li>
                  <li>
                     <a href="./?view=projects">
                        <i class="fa fa-building"></i>
                        <p>Projects</p>
                     </a>
                  </li>
                  <li>
                     <a href="./?view=categories">
                        <i class="fa fa-th-list"></i>
                        <p>Categories</p>
                     </a>
                  </li>
                  <li>
                     <a href="./?view=reports">
                        <i class="fa fa-area-chart"></i>
                        <p>Reports</p>
                     </a>
                  </li>
                  <li>
                     <a href="./?view=users">
                        <i class="fa fa-users"></i>
                        <p>Users</p>
                     </a>
                  </li>
               </ul>
            </div>
         </div>
         <div class="main-panel">
            <nav class="navbar navbar-transparent navbar-absolute">
               <div class="container-fluid">
                  <div class="navbar-header">
                     <button type="button" class="navbar-toggle" data-toggle="collapse">
                     <span class="sr-only">Toggle navigation</span>
                     <span class="icon-bar"></span>
                     <span class="icon-bar"></span>
                     <span class="icon-bar"></span>
                     </button>
                     <a class="navbar-brand" href="./"><b>Desktop IT Manager</b></a>
                  </div>
                  <div class="collapse navbar-collapse">
                     <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                           <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                           <i class="fa fa-user"></i>
                           &nbsp;<?php echo Core::$user->name." ".Core::$user->lastname." ".Core::$user->kind; ?>
                           </a>
                           <ul class="dropdown-menu">
                              <li class="divider"></li>
                              <li><a href="./?view=configuration">Account Settings</a></li>
                              <li class="divider"></li>
                              <li><a href="logout.php">Logout</a></li>
                           </ul>
                        </li>
                     </ul>
                     <!--
                        <form class="navbar-form navbar-right" role="search">
                          <div class="form-group  is-empty">
                            <input type="text" class="form-control" placeholder="Search">
                            <span class="material-input"></span>
                          </div>
                          <button type="submit" class="btn btn-white btn-round btn-just-icon">
                            <i class="fa fa-search"></i><div class="ripple-container"></div>
                          </button>
                        </form>
                        -->
                  </div>
               </div>
            </nav>
            <div class="content">
               <div class="container-fluid">
                  <?php 
                     View::load("login");
                     ?>
               </div>
            </div>
            <footer class="footer">
               <div class="container-fluid">
                  <nav class="pull-left">
                     <ul>
                        <li>
                        </li>
                        <!--
                           <li>
                             <a href="#">
                               Company
                             </a>
                           </li>
                           <li>
                             <a href="#">
                               Portfolio
                             </a>
                           </li>
                           <li>
                             <a href="#">
                                Blog
                             </a>
                           </li>
                           -->
                     </ul>
                  </nav>
                  <p class="copyright pull-right">
                     Developed by <a href="http://wwww.iesahn.com" target="_blank">Imagen Electrónica S.A de C.V</a> &copy; <script type="text/javascript">document.write(new Date().getFullYear());</script> 
                  </p>
               </div>
            </footer>
         </div>
      </div>
      <?php else:?>
      <?php 
         View::load("login"); 
         ?>
      <?php endif;?>
   </body>

Using <?php echo Core::$user->name." ".Core::$user->lastname." ".Core::$user->kind; ?> I'm pulling from the database the user's First and Last Name, and with Core::$user->kind I'm pulling the user type. 1 is for Admins and 2 is for Users.

Now, I would like to restrict access to certain sections of the ticket system by user type.

I had this idea, at least for the menu:

  <?php if(isset($_SESSION["user_id"])):?>
     <?php if((<?php echo Core::$user->kind; ?>) == 1):?>
        // insert full <div class="wrapper"> here 
     <?php else:?>
        // insert modified <div class="wrapper"> here (limited functions for 
        // normal users
     <?php endif;?>
  <?php else:?>
  <?php 
     View::load("login");
     ?>
  <?php endif;?>

But... It won't work. How can I make it work?

Josue Marin

It was easier than I thought, actually.

  <?php if(isset($_SESSION["user_id"])):?>
     <?php if(Core::$user->kind==1):?>
       //Admin Content
     <?php elseif(Core::$user->kind==2):?>
       //User Content
     <?php endif; ?>    
  <?php else:?>
  <?php 
     View::load("login");         
     ?>
  <?php endif;?>

This worked perfectly. I will validate all other sections of the ticket system using this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Show / hide menu according to user privilege

From Dev

dropdown menu according to selected value in database

From Dev

Limit the number of options a user can select in D3 dropdown menu

From Dev

User menu dropdown inside a navbar

From Dev

Tkinter Dropdown options menu using Enum

From Dev

options under dropdown menu is not getting selected

From Dev

creating dynamic menu in JSF according to user credentials

From Dev

Display different contents in the menu according to the role of the user

From Dev

Dropdown Menu working error when used with multiple dropdown options

From Dev

Dropdown Menu working error when used with multiple dropdown options

From Dev

GNOME 3 Different menu options for root and user

From Dev

NiFi: Add dropdown menu for user input

From Dev

Show options in second menu based on user selection in first menu

From Dev

How to combine two fields to be displayed as options in a dropdown menu using jquery?

From Dev

How to expand options in a select in the same option/dropdown menu

From Dev

Dropdown menu closes when I hover the options in Firefox

From Dev

How to expand options in a select in the same option/dropdown menu

From Dev

Dropdown menu closes when I hover the options in Firefox

From Dev

Dropdown menu close when user click anywhere on body or outside dropdown menu

From Dev

Dropdown menu close when user click anywhere on body or outside dropdown menu

From Dev

What mean "user lacks privilege"?

From Dev

user lacks privilege or object not found

From Dev

What mean "user lacks privilege"?

From Dev

Access table user in sysdba privilege

From Dev

Dropdown menu links from logged in user's name

From Dev

How to make bootstrap 3 dropdown menu working for no JS user

From Dev

Pop a message when user click on dropdown menu in configurable product?

From Dev

How to make bootstrap 3 dropdown menu working for no JS user

From Dev

Pop a message when user click on dropdown menu in configurable product?

Related Related

  1. 1

    Show / hide menu according to user privilege

  2. 2

    dropdown menu according to selected value in database

  3. 3

    Limit the number of options a user can select in D3 dropdown menu

  4. 4

    User menu dropdown inside a navbar

  5. 5

    Tkinter Dropdown options menu using Enum

  6. 6

    options under dropdown menu is not getting selected

  7. 7

    creating dynamic menu in JSF according to user credentials

  8. 8

    Display different contents in the menu according to the role of the user

  9. 9

    Dropdown Menu working error when used with multiple dropdown options

  10. 10

    Dropdown Menu working error when used with multiple dropdown options

  11. 11

    GNOME 3 Different menu options for root and user

  12. 12

    NiFi: Add dropdown menu for user input

  13. 13

    Show options in second menu based on user selection in first menu

  14. 14

    How to combine two fields to be displayed as options in a dropdown menu using jquery?

  15. 15

    How to expand options in a select in the same option/dropdown menu

  16. 16

    Dropdown menu closes when I hover the options in Firefox

  17. 17

    How to expand options in a select in the same option/dropdown menu

  18. 18

    Dropdown menu closes when I hover the options in Firefox

  19. 19

    Dropdown menu close when user click anywhere on body or outside dropdown menu

  20. 20

    Dropdown menu close when user click anywhere on body or outside dropdown menu

  21. 21

    What mean "user lacks privilege"?

  22. 22

    user lacks privilege or object not found

  23. 23

    What mean "user lacks privilege"?

  24. 24

    Access table user in sysdba privilege

  25. 25

    Dropdown menu links from logged in user's name

  26. 26

    How to make bootstrap 3 dropdown menu working for no JS user

  27. 27

    Pop a message when user click on dropdown menu in configurable product?

  28. 28

    How to make bootstrap 3 dropdown menu working for no JS user

  29. 29

    Pop a message when user click on dropdown menu in configurable product?

HotTag

Archive