Fix for Fix for my module is showing bug .. not sure why ..

  1.        
  2. By fkaufusi
  3.  
  4. Drupal version 6:
  5.  
  6. function user_access($string, $account = NULL, $reset = FALSE) {
  7.   global $user;
  8.   static $perm = array();
  9.   if ($reset) {
  10.     $perm = array();
  11.   }
  12.  
  13.   if(is_null($account)){
  14.       $account = $user;      
  15.   }
  16.  
  17.   // User #1 has all privileges:
  18.   if ($account->uid == 1) {
  19.     return TRUE;
  20.   }
  21.   // To reduce the number of SQL queries, we cache the user's permissions
  22.   // in a static variable.
  23.   if (!isset($perm[$account->uid])) {
  24.     $result = db_query("SELECT p.perm FROM {role} r INNER JOIN {permission} p ON p.rid = r.rid WHERE r.rid IN (". db_placeholders($account->roles) .")", array_keys($account->roles));
  25.     $perms = array();
  26.     while ($row = db_fetch_object($result)) {
  27.       $perms += array_flip(explode(', ', $row->perm));
  28.     }
  29.     $perm[$account->uid] = $perms;
  30.   }
  31.   return isset($perm[$account->uid][$string]);
  32. }
  33.  
  34. Have a look at the function above which is use to check the user access permission.
  35.  
  36. Look at how the drupal validate the $account variable, $account variable is suppose to be an object and here's how drupal validate it.
  37.  
  38. if(is_null($account)){
  39.      $account = $user;      
  40. }
  41.  
  42. They only check to see if the $account variable is null, then if not null then they think is an object. Okay how about if the $account type is not an object.
  43.  
  44. Here's a small fix and it would help to implement in the next version of drupal.
  45.  
  46. if(is_null($account) || !is_object($account)){
  47.       $account = $user;      
  48. }
  49.  
  50. ?>

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.