Search code examples
phpmysqlprivileges

Set file privileges in navigational menu with php


Hello and thanks for the help in advance. I have a more robust version of this that i am working on but I need something for the short term. We created a 2 tier user access to an administrative panel. Employee and Administrators. The code below is a short version example of the longer version we are implementing in the short term. First I want to check their access level if it is level 1 or level 0. This is a method in OOP.

    function menuPriviledge($dbCon){

        $username = $_REQUEST['username'];

        if($accessLevel = $this->dbConnection->query("SELECT access FROM UserProfiles WHERE username = '$username' LIMIT 0, 1")){
            while($accessData = $accessLevel->fetch_assoc()){
                $access = $accessData['access'];
            }
        }

After i get done echoing this top portion i am getting the correct answer, I am getting an access level 1 associated with the current username in the account. That is what I was looking for so we're good but then on the next if statement i test that the variable $access is = to 1 (Statement below) if it is equal to 1 I call on printf and display a present list of nav menus prepared for administrators else a different preset menu for employees with limited page access is displayed.

            if(isset($access)){

                switch($access){

                case 1: 
                    $query = "SELECT * FROM CVCUserFilePrivileges";
                    if($files = $this->dbConnection->query($query)){
                        while($access = $files->fetch_assoc()){
                            printf("<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Directory Listings</a></li>
                                <li><a title=\"Ads Manager\"><i class=\"icon-laptop\"></i>Ads Manager<span>5</span></a>
                                    <ul>
                                        <li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Dealer Paid Listings</a></li>
                                        <li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Listing Side Ads</a></li>
                                        <li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Scrap Metal Page Ads</a></li>
                                        <li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Coin Request Page Ads</a></li>
                                        <li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Dealer Form Page Ads</a></li>
                                    </ul>                   
                                </li>", $access['directoryList'], $access['dlrPaidListing'], $access['listingSideAds'], $access['scrapMetalAds'], $access['coinRequestAds'], $access['dlrFormAds']);
                        }
                    }
                break;
                case 0:
                    $query = "SELECT * FROM CVCUserFilePrivileges";
                    if($files = $this->dbConnection->query($query)){
                        while($access = $files->fetch_assoc()){
                            printf("<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Directory Listings</a></li>
                                <li><a title=\"Ads Manager\"><i class=\"icon-laptop\"></i>Ads Manager<span>1</span></a>
                                    <ul>
                                        <li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Dealer Paid Listings</a></li>
                                    </ul>                   
                                </li>", $access['directory'], $access['dlrPaidListing']);
                        }
                    }
                break;
                default:
                    echo "There is an error, no access level set, contact your database administrator";
                }
            }

When it semi works, it continues to add the first page link to every single nav option underneath it, so every nav option leads to the same page. When it doesn't work it just completely disappears and the nav doesn't even exist. I hope I was able to explain this well enough. The problem seems to be on the if statement where I test their level of access, before displaying the nav menu.


Solution

  • Change the code to:

    if(isset($access)) {
    
        switch($access) {
    
            case 1:
               // do the code
               break;
    
            case 2:
               // do the code
               break;
    
        }
    }else{
        echo "access not set";
    }
    

    Try this.

    Ps.: 1 its different from "1". Did you put your sql field as int?