Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Make menu items visible different user groups

    Hello,

    I have created a humble CMS sort of thing where I have users logging in and out. And there is of course me, the admin.

    I am working on a menu system where I first create menus and then add menu items into them.

    Here is my menuitems table:

    PHP Code:
    CREATE TABLE `menuitems` (
      `
    IDint(11NOT NULL auto_increment,
      `
    namevarchar(255NOT NULL default '',
      `
    menuint(11NOT NULL default '0',
      `
    targetvarchar(16NOT NULL default '',
      `
    link_typeenum('0','1','2'NOT NULL default '0',
      `
    link_pageint(11NOT NULL default '0',
      `
    link_moduleint(11NOT NULL default '0',
      `
    link_urlvarchar(255NOT NULL default '',
      `
    rankvarchar(2) default NULL,
      `
    activeenum('0','1'NOT NULL default '0',
      `
    deletedenum('0','1'NOT NULL default '0',
      `
    accesschar(1NOT NULL default '',
      
    PRIMARY KEY  (`ID`)
    ENGINE=MyISAM  DEFAULT CHARSET=utf8
    and this is how I generate the menu:

    PHP Code:
    function GenerateMenu($menu_id,$style) {
        include(
    "db.php");
        
    $result mysql_query("SELECT name,target,link_type,link_page,link_module,link_url,access FROM menuitems WHERE menu='$menu_id' AND active='1' AND deleted='0' ORDER BY rank") or die(mysql_error());
        
    $menu '<div id="' $style '">';
        
    $menu .= '<ul>';
        while(
    $row mysql_fetch_array($result)) {
            if(
    $row['link_type'] == 0) { // page
                
    $link GetPageURL($row['link_page']);
            }
            if(
    $row['link_type'] == 1) { // module
                
    $link GetModuleURL($row['link_module']);
            }
            if(
    $row['link_type'] == 2) { // url
                
    $link $row['link_url'];
            }
            
    $menu .= '<li><a href="' $link '" target="' $row['target'] . '">' $row['name'] . '</a></li>';
        }
        
    $menu .= '</ul>';
        
    $menu .= '</div>';
        echo 
    $menu;

    Now the question:

    I would like some menu items available to logged-in users only (such as Log Out) and some menu items available to visitors only (such as Log-In), some to Admin only (Administration Menu) etc...

    Therefore I created an access field in menuitems table. Upon login, I could add users group variable into a $_SESSION['usergroup'] and compare this with the access field and if it equals then print it, if not, not print it.

    What can be the code to do this?
    Last edited by guvenck; 03-30-2007 at 08:20 PM.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Take your CMS up a notch! Setup permissions (profiles) that each user will get. For example, an admin profile, guest profile, standard profile... etc. Then when a user comes to your site:

    //check if logged in... NO? your have a guest profile.
    $_SESSION['profile'] == "guest";

    //you are lodded in eh? OK well what profile do you have?
    $_SESSION['profile'] == $logged_in_users_profile;

    Then setup your permissions table to allow for certain actions to be preformed.

    Then in your code:
    if(logged_in_user(can_view_link) { show link }
    if(logged_in_user(can_perform_action) { allow action }

    etc... you get the idea.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    I've been digging a little further and solved the problem using this code, may not be the best solution but anyway:

    PHP Code:
    function GetPermissions($item,$ID) {
        global 
    $db_prefix;
        include(
    "db.php");
        switch (
    $item) {
            case 
    "menuitem":
                
    $q "SELECT profile FROM " $db_prefix "permissions_menuitems WHERE miid='$ID'";
                
    $r mysql_query($q) or die(mysql_error());
                
    $profiles = array();
                while(
    $ro mysql_fetch_array($r)) {
                    
    $profiles[] = $ro['profile'];
                }
                break;
            case 
    "page":
                
    // $q = "SELECT miid,profile FROM " . $db_prefix . "permissions_menuitems WHERE miid='$ID'";
                
    break;
            case 
    "module":
                
    // $q = "SELECT miid,profile FROM " . $db_prefix . "permissions_menuitems WHERE miid='$ID'";
                
    break;
        }
        return 
    $profiles;
    }





    function 
    GenerateMenu($menu_id,$style) {
        global 
    $db_prefix;
        
    $myprofile $_SESSION['profile'];
        include(
    "db.php");
        
    $result mysql_query("SELECT ID,name,target,link_type,link_page,link_module,link_url,access FROM " $db_prefix "menuitems WHERE menu='$menu_id' AND active='1' AND deleted='0' ORDER BY rank") or die(mysql_error());
        
    $menu '<div id="' $style '">';
        
    $menu .= '<ul>';
        while(
    $row mysql_fetch_array($result)) {
            
    // get the permissions for the menuitem in an array
            
    $permissions GetPermissions('menuitem',$row['ID']);
            if(
    in_array($myprofile,$permissions)) {
                if(
    $row['link_type'] == 0) { // page
                    
    $link GetPageURL($row['link_page']);
                }
                if(
    $row['link_type'] == 1) { // module
                    
    $link GetModuleURL($row['link_module']);
                }
                if(
    $row['link_type'] == 2) { // url
                    
    $link $row['link_url'];
                }
                
    $menu .= '<li><a href="' $link '" target="' $row['target'] . '">' $row['name'] . '</a></li>';
            }
        }
        
    $menu .= '</ul>';
        
    $menu .= '</div>';
        echo 
    $menu;


  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It's a good start!!! As you progress and come acrossed different situations with permissions, you will build onto this and add more fields, and do this, do that... This is a better way to do this and before you know it, you have a solid permissions setup


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •