Changeset 1900 in ExiteCMS


Ignore:
Timestamp:
10/22/08 18:08:43 (3 years ago)
Author:
hverton
Message:

add group rights caching, to reduce database I/O

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/user_functions.php

    r1885 r1900  
    285285    global $groups, $db_prefix; 
    286286 
    287     // every user is a member 
    288     if ($group_id == "101") { return true; } 
    289     // get the group rights from the user record 
    290     $result = dbquery("SELECT user_groups, user_level FROM ".$db_prefix."users WHERE user_id = '".$user_id."'"); 
    291     if ($data = dbarray($result)) { 
    292         // check if the requested group matches a user level 
    293         if ($group_id == $data['user_level']) { return true; } 
    294         // if group memberships are defined, get the users own group memberships into an array 
    295         if (!empty($data['user_groups'])) { 
    296             $groups = explode(".", substr($data['user_groups'], 1)); 
    297             foreach ($groups as $group) { 
    298                 // check if this groups has subgroups. If so, add them to the array 
    299                 getsubgroups($group); 
    300             } 
    301             // now that we have all groups, check for a match 
    302             foreach ($groups as $group) { 
    303                 if ($group == $group_id) { return true; } 
    304             } 
    305         } 
    306     } 
    307     // user not found or no group match 
    308     return false; 
     287    // result cache 
     288    static $resultcache; 
     289    if (isset($resultcache[$user_id][$group_id])) { 
     290        return $resultcache[$user_id][$group_id]; 
     291    } 
     292 
     293    $check = false; 
     294    if ($group_id == "101") {  
     295        // every user is a member 
     296        $check = true; 
     297    } else { 
     298        // get the group rights from the user record 
     299        $result = dbquery("SELECT user_groups, user_level FROM ".$db_prefix."users WHERE user_id = '".$user_id."'"); 
     300        if ($data = dbarray($result)) { 
     301            // check if the requested group matches a user level 
     302            if ($group_id == $data['user_level']) {  
     303                $check = true; 
     304            } else { 
     305                // if group memberships are defined, get the users own group memberships into an array 
     306                if (!empty($data['user_groups'])) { 
     307                    $groups = explode(".", substr($data['user_groups'], 1)); 
     308                    foreach ($groups as $group) { 
     309                        // check if this groups has subgroups. If so, add them to the array 
     310                        getsubgroups($group); 
     311                    } 
     312                    // now that we have all groups, check for a match 
     313                    foreach ($groups as $group) { 
     314                        if ($group == $group_id) {  
     315                            $check = true; 
     316                            break; 
     317                        } 
     318                    } 
     319                } 
     320            } 
     321        } 
     322    } 
     323 
     324    if (!isset($resultcache[$user_id])) $resultcache[$user_id] = array(); 
     325    $resultcache[$user_id][$group_id] = $check; 
     326 
     327    return $check; 
    309328} 
    310329 
     
    461480 
    462481    global $groups, $settings, $db_prefix; 
     482 
     483    // result cache 
     484    static $resultcache; 
     485    if (isset($resultcache[$group_id])) { 
     486        return $resultcache[$group_id]; 
     487    } 
    463488     
    464489    // gather the group and it's sub-groups into an array 
     
    489514        $members[] = $data; 
    490515    } 
     516 
     517    if (!isset($resultcache)) $resultcache = array(); 
     518    $resultcache[$group_id] = $members; 
     519 
    491520    return $members; 
    492521} 
Note: See TracChangeset for help on using the changeset viewer.