2011-10-03 4 views
0

모듈을 검사하여 사용자가 해당 페이지를 볼 수있는 권한이 있는지 확인하는 사용자 클래스에 CheckModulePermission 함수를 만들었습니다. 함수는 다음과 같습니다.모듈 사용 권한 클래스 오류

public function CheckModulePermissions($moduleId) { 

     if(isset($_SESSION['userId'])) { 

      // If the user is admin, allow regardless 

      if($this->IsAdmin()) { 

       return true; 

      } 

      $sql = "SELECT `userModuleId` 

        FROM `userModules` 

        WHERE `userId` = " . $_SESSION['userId'] . " 

        AND `moduleId` = " . $moduleId . ";"; 

      mysql_select_db(DB_USER_DATABASE_NAME, $this->conn); 

      $result = mysql_query($sql, $this->conn); 

      $x = mysql_fetch_row($result); 

      if($x[0] == 1) { 

       return true; 

      } else { 

       return false; 

      } 

     } else { 

      return false; 

     } 

    } 

} 

이 방법은 실패한 페이지 하나를 제외한 모든 페이지에서 정상적으로 작동합니다. 드롭 다운 상자와 사용자 권한에 따라 업데이트 될 텍스트 상자가 있습니다. 사용 권한이있는 사용자로 로그온했지만 드롭 다운 상자가 나타나지 않습니다.

if(isset($_GET['orderNumber'])) { 

    // If post is set then update the prima reference and order status 

    // Only if user has sufficient privileges 

    if(isset($_POST['orderStatus'])) { 

     if($user->CheckModulePermissions(11)) { 

      $cid->UpdateOrderStatus($_GET['orderNumber'], $_POST['orderStatus']); 

      $cid->UpdateOrderReference($_GET['orderNumber'], $_POST['PReference']); 

     } 

    } 




if($user->CheckModulePermissions(11)) { 

          $content .= "<select name='orderStatus'> 

          <option value='1'"; 

          if($orderDetails['status'] == 1) $content .= " selected='selected'"; 

          $content .= ">Incomplete</option> 

          <option value='2'"; 

          if($orderDetails['status'] == 2) $content .= " selected='selected'"; 

          $content .= ">Submitted</option> 

          <option value='3'"; 

          if($orderDetails['status'] == 3) $content .= " selected='selected'"; 

          $content .= ">Processed</option> 

         </select>"; 

        } else { 

         if($orderDetails['status'] == 1) $content .= "Incomplete"; 

         if($orderDetails['status'] == 2) $content .= "Submitted"; 

         if($orderDetails['status'] == 3) $content .= "Processed"; 

        } 

        $content .= "</td> 

        </tr> 

        <tr> 

         <th>Prima Order Number</th> 

         <td>"; 

         if($user->CheckModulePermissions(11)) { 

          $content .= "<input type='text' name='pReference' value='" . $orderDetails['PReference'] . "' /></td> 

          </tr> 

          <tr> 

           <td colspan='2'><input type='submit' /></td> 

          </tr>"; 

         } else { 

          $content .= $orderDetails['PrimaReference'] . "</td></tr>"; 

         } 

         $content .= "</table> 

       </form> 

      </td> 

오류가 발생한 드롭 다운 상자의 논리입니까? 여기

답변

0
다음

당신의 CheckModulePermissions() 방법의보다 효율적인/읽을 버전은 ...

public function CheckModulePermissions ($moduleId) { 

    // Deny immmediately if no userId is set 
    if (!isset($_SESSION['userId'])) return FALSE; 

    // If the user is admin, allow regardless 
    if ($this->IsAdmin()) return TRUE; 

    // Generate an SQL statement - does this need sanitising? 
    $sql = "SELECT `userModuleId` 
      FROM `userModules` 
      WHERE `userId` = '{$_SESSION['userId']}' 
      AND `moduleId` = '$moduleId' 
      LIMIT 1"; 
    // Is this line really necessary? Are you actually working with more than one database? 
    // Even if you are, it's probably better to do it in the query, like this: 
    // SELECT whatever FROM DB_USER_DATABASE_NAME.tablename WHERE... 
    mysql_select_db(DB_USER_DATABASE_NAME, $this->conn); 
    // Since you only want one row, it's slightly more resource efficient 
    // to abandon the $result variable 
    $x = mysql_fetch_row(mysql_query($sql, $this->conn)); 
    // This means the same thing as your if ... else 
    return $x[0] == 1; 

} 

... 그리고는 HTML 생성 코드를 재 작성 버전입니다.

// Get this once, at the beginning, to minimise SQL traffic 
$hasPermissions = $user->CheckModulePermissions(11); 

// Uncomment this line to make sure that $user->CheckModulePermissions is returning the value you expect 
//var_dump($hasPermissions); 

if (isset($_GET['orderNumber'])) { 
    // If post is set then update the prima reference and order status 
    // Only if user has sufficient privileges 
    if (isset($_POST['orderStatus']) && $hasPermissions) { 
    $cid->UpdateOrderStatus($_GET['orderNumber'], $_POST['orderStatus']); 
    $cid->UpdateOrderReference($_GET['orderNumber'], $_POST['PReference']); 
    } 

    // Map of status numbers to string descriptions 
    $statusStrs = array(1 => 'Incomplete','Submitted','Processed'); 

    if ($hasPermissions) { 
    // Generate a <select> 
    $content .= "<select name='orderStatus'>"; 
    foreach ($statusStrs as $val => $str) { 
     $content .= "\n<option value='$val'".(($orderDetails['status'] == $val) ? " selected='selected'" : '').">$str</option>"; 
    } 
    $content .= "\n</select>"; 
    } else { 
    // Print the current status string 
    $content .= $statusStrs[$orderDetails['status']]; 
    } 

    // Close the table cell (layout tables are nasty nasty) 
    $content .= "</td> 
    </tr> 
    <tr> 
    <th>Prima Order Number</th> 
    <td>"; 

    if ($hasPermissions) { 
    // add an input for changing the reference number 
    $content .= "<input type='text' name='pReference' value='{$orderDetails['PReference']}' /></td> 
    </tr> 
    <tr> 
    <td colspan='2'><input type='submit' /></td> 
    </tr>"; 
    } else { 
    // Display the current reference number 
    $content .= $orderDetails['PrimaReference'] . "</td></tr>"; 
    } 
    $content .= "</table> 
    </form> 
</td> 

나는 문제의 가장 큰 원인은 당신이 TRUE를 반환 할 것으로 예상 할 때 CheckModulePermissions()FALSE를 반환하는 것입니다 생각합니다. 이것을 확인하려면 var_dump() 줄의 주석 처리를 제거하면 거기에서 가져갑니다.

+0

감사합니다. 그렇습니다. 권한이 있다는 사실에도 불구하고 관리자로부터 떨어져있는 모든 사용자에 대해 false를 반환하는 것은 제 기능입니다. –

+0

첫 번째 검사 (즉, $ _SESSION [ 'userId']'가 설정되지 않음) 또는 끝에있는 것인가 ('$ x [0] = = 1 '). if (! isset ($ _ SESSION [ 'userId']))가 -1을 반환하고 이전처럼'var_dump'를 호출하면, if (! isset ($ _ SESSION [ 'userId']) '$ _SESSION'에 문제가 있거나 DB 쿼리에 문제가 있는지 알 수 있습니다. 'session_start()'를 호출하는 것을 잊는 것만 큼 간단 할까? – DaveRandom