Changeset 2458

Show
Ignore:
Timestamp:
09/08/08 21:53:43 (4 months ago)
Author:
bjohnson
Message:

schema06: A bunch of fixes based on unit testing. A lot of tests still fail,
so there is more work to do here.

Location:
branches/schema06/system/classes
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/schema06/system/classes/acl.php

    r2451 r2458  
    3232    { 
    3333        self::$permission_ids = DB::get_keyvalue( 'SELECT name, id FROM {permissions};' ); 
    34         if ( self::$permission_ids === FALSE ) { 
     34 
     35        if ( ! isset(self::$permission_ids) ) { 
    3536            self::$permission_ids = array(); 
    3637        } 
     
    6667        $name= self::normalize_permission( $name ); 
    6768        // first, make sure this isn't a duplicate 
    68         if ( ACL::permission_exists( $name ) ) { 
     69        if ( ACL::token_exists( $name ) ) { 
    6970            return false; 
    7071        } 
     
    161162    public static function token_id( $name ) 
    162163    { 
    163         if( is_integer($name) ) { 
     164        if( is_numeric($name) ) { 
    164165            return $name; 
    165166        } 
     
    238239            group_id=? AND token_id=?;'; 
    239240 
    240         $result = DB::get_value( $sql ); 
    241         if ( isset( $result ) && self::$permission_ids[$result] == $access ) { 
     241        $result = DB::get_value( $sql, array( $group, $permission) ); 
     242        if ( isset( $result ) && $result == self::$permission_ids[$access] ) { 
    242243            // the permission has been granted to this group 
    243244            return true; 
     
    289290        $sql = <<<SQL 
    290291SELECT permission_id 
    291 FROM ( 
    292 ( 
    293   SELECT permission_id 
    294292  FROM {user_token_permissions} 
    295293  WHERE user_id = :user_id 
    296294  AND token_id = :token_id 
    297 ) AS up 
    298295UNION ALL 
    299 ( 
    300   SELECT gp.permission_id 
     296SELECT gp.permission_id 
    301297  FROM {users_groups} ug 
    302298  INNER JOIN {group_token_permissions} gp 
    303299  ON ug.group_id = gp.group_id 
    304300  AND ug.user_id = :user_id 
    305   AND gp.token_id = :token_id 
    306   ORDER BY permission_id ASC 
    307   LIMIT 1 
    308 ) 
    309 ) 
    310 LIMIT 1;  
     301  AND gp.token_id = :token_id; 
    311302SQL; 
    312303        $result = DB::get_value( $sql, array( ':user_id' => $user_id, ':token_id' => $permission ) ); 
    313304 
    314         if ( isset( $result ) && self::$permission_ids[$result] == $access ) { 
     305        if ( isset( $result ) && $result == self::$permission_ids[$access] ) { 
    315306            return true; 
    316307        } 
     
    332323        // DB::update will insert if the token is not already in the group tokens table 
    333324        $result = DB::update( 
    334             '{group_tokens_permissions}', 
     325            '{group_token_permissions}', 
    335326            array( 'permission_id' => self::$permission_ids[$access] ), 
    336327            array( 'group_id' => $group_id, 'token_id' => self::token_id( $token_id ) ) 
    337328        ); 
     329 
     330        $ug = UserGroup::get_by_id( $group_id ); 
     331        $ug->clear_permissions_cache(); 
    338332 
    339333        return $result; 
     
    350344    { 
    351345        $result = DB::update( 
    352             '{user_tokens_permissions}', 
     346            '{user_token_permissions}', 
    353347            array( 'permission_id' => self::$permission_ids[$access] ), 
    354348            array( 'user_id' => $user_id, 'token_id' => self::token_id( $token_id ) ) 
     
    388382    public static function revoke_group_permission( $group_id, $token_id ) 
    389383    { 
    390         $result = DB::delete( '{group_tokens_permissions}', 
     384        $result = DB::delete( '{group_token_permissions}', 
    391385            array( 'group_id' => $group_id, 'token_id' => $token_id ) ); 
     386 
     387        $ug = UserGroup::get_by_id( $group_id ); 
     388        $ug->clear_permissions_cache(); 
    392389 
    393390        return $result; 
     
    402399    public static function revoke_user_permission( $user_id, $token_id ) 
    403400    { 
    404         $result = DB::delete( '{user_tokens_permissions}', 
     401        $result = DB::delete( '{user_token_permissions}', 
    405402            array( 'user_id' => $user_id, 'token_id' => $token_id ) ); 
    406403 
  • branches/schema06/system/classes/usergroup.php

    r2451 r2458  
    4040                $this->member_ids= $result; 
    4141            } 
    42  
    43             if ( $results= DB::get_results( 'SELECT token_id, permission_id FROM {group_token_permissions} WHERE group_id=?', array( $this->id ) ) ) { 
    44                 foreach ( $results as $result ) { 
    45                     $this->permissions[$result->token_id] = $result->permission_id; 
    46                 } 
    47             } 
    4842        } 
    4943 
     
    212206        $permissions = array_map(array('ACL', 'token_id'), $permissions); 
    213207 
    214         // Merge and grant the new permissions 
     208        // grant the new permissions 
    215209        foreach ( $permissions as $permission ) { 
    216             $this->permissions[$permission] = $access; 
    217210            ACL::grant_group( $this->id, $permission, $access ); 
    218211        } 
     
    236229        $permissions = Utils::single_array( $permissions ); 
    237230        $permissions = array_map(array('ACL', 'token_id'), $permissions); 
     231        if ( ! isset( $this->permissions ) ) { 
     232            $this->load_permissions_cache(); 
     233        } 
    238234        // Remove permissions from the granted list 
    239235        $this->permissions = array_diff_key( $this->permissions, $permissions ); 
     
    254250    { 
    255251        $permission= ACL::token_id( $permission ); 
     252        if ( ! isset( $this->permissions ) ) { 
     253            $this->load_permissions_cache(); 
     254        } 
    256255        if ( isset( $this->permissions[$permission] ) && $this->permissions[$permission] == $access ) { 
    257256            return true; 
    258257        } 
    259258        return false; 
     259    } 
     260     
     261    /** 
     262     * Clear permissions cache. 
     263     */ 
     264    public function clear_permissions_cache() 
     265    { 
     266        unset( $this->permissions ); 
     267    } 
     268     
     269    /** 
     270     * Load permissions cache. 
     271     */ 
     272    public function load_permissions_cache() 
     273    { 
     274        if ( $results= DB::get_results( 'SELECT token_id, permission_id FROM {group_token_permissions} WHERE group_id=?', array( $this->id ) ) ) { 
     275            foreach ( $results as $result ) { 
     276                $this->permissions[$result->token_id] = $result->permission_id; 
     277            } 
     278        } 
    260279    } 
    261280 
     
    313332    public static function name( $id ) 
    314333    { 
    315         $check_field = is_int( $id ) ? 'id' : 'name'; 
     334        $check_field = is_numeric( $id ) ? 'id' : 'name'; 
    316335        $name = DB::get_value( "SELECT name FROM {groups} WHERE {$check_field}=?", array( $id ) ); 
    317336        return $name;  // get_value returns false if no record is returned 
     
    325344    public static function id( $name ) 
    326345    { 
    327         $check_field = is_int( $name ) ? 'id' : 'name'; 
    328         $id = DB::get_value( "SELECT id FROM {groups} WHERE {$check_field}=?", array( $name ) ); 
     346        if( is_numeric($name) ) { 
     347            return $name; 
     348        } 
     349        $id = DB::get_value( "SELECT id FROM {groups} WHERE name=?", array( $name ) ); 
    329350        return $id; // get_value returns false if no record is returned 
    330351    }