Changeset 2445

Show
Ignore:
Timestamp:
09/05/08 03:39:36 (3 months ago)
Author:
bjohnson
Message:

Schema06: Committing more work in progress on ACL. Nothing has been tested yet, but I've finished the first pass through the ACL and UserGroup classes.

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

Legend:

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

    r2439 r2445  
    2727 
    2828    /** 
     29     * Static initializer to fill the $permission_ids array 
     30     */ 
     31    public static function __static() 
     32    { 
     33        $result = DB::get_results( 'SELECT id, name FROM {permissions};' ); 
     34        foreach ( $result as $r ) { 
     35            self::$permission_ids[$r->name] = $r->id; 
     36        } 
     37    } 
     38 
     39    /** 
    2940     * Convert a permission access name (read, write, full, denied) into an ID 
    3041     * @param string The access name 
     
    3344    public static function permission_id( $name ) 
    3445    { 
    35         if ( count( $access_ids ) == 0 ) { 
    36             $result = DB::query( 'SELECT id, name FROM {permissions};' ); 
    37             foreach ( $result as $r ) { 
    38                 $access_ids[$r->name] = $r->id; 
    39             } 
    40         } 
    41  
    42         return ( isset( $access_ids[$name] ) ? $access_ids[$name] : FALSE; 
    43     } 
    44  
    45     /** 
    46      * Create a new permission, and save it to the Permissions table 
     46        return ( isset( self::$permission_ids[$name] ) ? self::$permission_ids[$name] : FALSE; 
     47    } 
     48 
     49    /** 
     50     * Create a new permission, and save it to the permission tokens table 
    4751     * @param string The name of the permission 
    4852     * @param string The description of the permission 
     
    117121     * @return array an array of QueryRecord objects containing all permissions 
    118122    **/ 
    119     public static function all_permission_tokens( $order= 'id' ) 
     123    public static function all_permissions( $order= 'id' ) 
    120124    { 
    121125        $order = strtolower( $order ); 
     
    222226        $group = UserGroup::id( $group ); 
    223227        $permission = self::token_id( $permission ); 
    224         $sql = <<<SQL 
    225 SELECT p.name FROM {group_token_permissions} gp, {permissions} p WHERE 
    226 gp.group_id=? AND gp.token_id=? AND gp.permission_id=p.id; 
    227 SQL; 
     228        $sql = 'SELECT permission_id FROM {group_token_permissions} WHERE 
     229            group_id=? AND token_id=?;'; 
     230 
    228231        $result = DB::get_value( $sql ); 
    229         if ( $result == $access ) { 
     232        if ( $result !== FALSE && self::$permission_ids[$result] == $access ) { 
    230233            // the permission has been granted to this group 
    231234            return true; 
     
    276279         */  
    277280        $sql = <<<SQL 
    278 SELECT COALESCE(permission_id, 0) as permission_id 
     281SELECT permission_id 
    279282FROM ( 
    280283( 
     
    300303        $result = DB::get_value( $sql, array( ':user_id' => $user_id, ':token_id' => $permission ); 
    301304 
    302         // TODO: modify above call to return the permission name rather than the ID 
    303         // For now, I'll just look for a result > 0 
    304         if ( $result !== FALSE && intval($result) > 0 ) { 
     305        if ( $result !== FALSE && self::permission_ids[$result] == $access ) { 
    305306            return true; 
    306307        } 
    307308 
    308         // if the permission is neither denied nor granted, they're not 
    309         // allowed to do it. 
    310         return self::ACCESS_NONEXISTANT_PERMISSION; 
     309        // either the permission hasn't been granted, or it's been 
     310        // explicitly denied. 
    311311        return false; 
    312312    } 
     
    315315     * Grant a permission to a group 
    316316     * @param integer $group_id The group ID 
    317      * @param integer $token_id The permission token to grant 
     317     * @param mixed $token_id The name or ID of the permission token to grant 
    318318     * @param string $access The kind of access to assign the group 
    319319     * @return Result of the DB query 
     
    321321    public static function grant_group( $group_id, $token_id, $access = 'full' ) 
    322322    { 
    323         $result = DB::query( 'INSERT INTO {group_tokens_permissions} (group_id, token_id, permission_id) VALUES (?, ?, ?);', 
    324             array( $group_id, $token_id, self::permission_ids( $access ) ); 
     323        // DB::update will insert if the token is not already in the group tokens table 
     324        $result = DB::update( 
     325            '{group_tokens_permissions}', 
     326            array( 'permission_id' => self::$permission_ids[$access] ), 
     327            array( 'group_id' => $group_id, 'token_id' => self::token_id( $token_id ) ) 
     328        ); 
     329 
    325330        return $result; 
    326331    } 
     
    329334     * Grant a permission to a user  
    330335     * @param integer $user_id The user ID 
    331      * @param integer $token_id The permission token to grant 
     336     * @param integer $token_id The name or ID of the permission token to grant 
    332337     * @param string $access The kind of access to assign the group 
    333338     * @return Result of the DB query 
     
    335340    public static function grant_user( $user_id, $token_id, $access = 'full' ) 
    336341    { 
    337         $result = DB::query( 'INSERT INTO {user_tokens_permissions} (user_id, token_id, permission_id) VALUES (?, ?, ?);', 
    338             array( $user_id, $token_id, self::permission_ids( $access ) ); 
     342        $result = DB::update( 
     343            '{user_tokens_permissions}', 
     344            array( 'permission_id' => self::$permission_ids[$access] ), 
     345            array( 'user_id' => $user_id, 'token_id' => self::token_id( $token_id ) ) 
     346        ); 
     347 
    339348        return $result; 
    340349    } 
    341350     
     351    /** 
     352     * Deny permission to a group 
     353     * @param integer $group_id The group ID 
     354     * @param mixed $token_id The name or ID of the permission token 
     355     * @return Result of the DB query 
     356     **/ 
     357    public static function deny_group( $group_id, $token_id ) 
     358    { 
     359        self::grant_group( $group_id, $token_id, 'deny' ); 
     360    } 
     361 
     362    /** 
     363     * Deny permission to a user  
     364     * @param integer $user_id The user ID 
     365     * @param mixed $token_id The name or ID of the permission token 
     366     * @return Result of the DB query 
     367     **/ 
     368    public static function deny_user( $user_id, $token_id ) 
     369    { 
     370        self::grant_user( $group_id, $token_id, 'deny' ); 
     371    }    
     372 
     373    /** 
     374     * Remove a permission from the group permissions table 
     375     * @param integer $group_id The group ID 
     376     * @param mixed $token_id The name or ID of the permission token 
     377     * @return the result of the DB query 
     378     **/ 
     379    public static function revoke_group_permission( $group_id, $token_id ) 
     380    { 
     381        $result = DB::delete( '{group_tokens_permissions}', 
     382            array( 'group_id' => $group_id, 'token_id' => $token_id ) ); 
     383 
     384        return $result; 
     385    } 
     386 
     387    /** 
     388     * Remove a permission from the user permissions table 
     389     * @param integer $user_id The user ID 
     390     * @param mixed $token_id The name or ID of the permission token 
     391     * @return the result of the DB query 
     392     **/ 
     393    public static function revoke_user_permission( $user_id, $token_id ) 
     394    { 
     395        $result = DB::delete( '{user_tokens_permissions}', 
     396            array( 'user_id' => $user_id, 'token_id' => $token_id ) ); 
     397 
     398        return $result; 
     399    } 
    342400 
    343401    /** 
  • branches/schema06/system/classes/usergroup.php

    r2439 r2445  
    77{ 
    88    // These arrays hold the current membership and permission settings for this group 
    9     // These arrays are NOT matched key and value pairs (the are not stored like array('foo'=>'foo') ) 
     9    // $member_ids is not NOT matched key and value pairs ( like array('foo'=>'foo') ) 
    1010    private $member_ids = array(); 
    1111    private $permissions = array(); 
     
    4343            if ( $results= DB::get_results( 'SELECT token_id, permission_id FROM {group_token_permissions} WHERE group_id=?', array( $this->id ) ) ) { 
    4444                foreach ( $results as $result ) { 
    45                     $this->permissions[] = array( 
    46                         'token_id' => $result->token_id, 
    47                         'permission_id' => $result->permission_id, 
    48                     ); 
     45                    $this->permissions[$result->token_id] = $result->permission_id; 
    4946                } 
    5047            } 
     
    209206     * @param mixed A permission token ID, name, or array of the same 
    210207    **/ 
    211     public function grant( $permissions ) 
     208    public function grant( $permissions, $access = 'full' ) 
    212209    { 
    213210        $permissions = Utils::single_array( $permissions ); 
    214211        // Use ids internally for all permissions 
    215212        $permissions = array_map(array('ACL', 'token_id'), $permissions); 
    216         // Merge the new permissions 
    217         $this->permissions_granted = $this->permissions_granted + $permissions; 
    218         // List each permission exactly once 
    219         $this->permissions_granted = array_unique($this->permissions_granted); 
    220         // Remove granted permissions from the denied list 
    221         $this->permissions_denied = array_diff($this->permissions_denied, $this->permissions_granted); 
     213 
     214        // Merge and grant the new permissions 
     215        foreach ( $permissions as $permission ) { 
     216            $this->permissions[$permission] = $access; 
     217            ACL::grant_group( $this->id, $permission, $access ); 
     218        } 
    222219    } 
    223220 
     
    228225    public function deny( $permissions ) 
    229226    { 
    230         $permissions = Utils::single_array( $permissions ); 
    231         // Use ids internally for all permissions 
    232         $permissions = array_map(array('ACL', 'permission_id'), $permissions); 
    233         // Merge the new permissions 
    234         $this->permissions_denied = $this->permissions_denied + $permissions; 
    235         // List each permission exactly once 
    236         $this->permissions_denied = array_unique($this->permissions_denied); 
    237         // Remove denied permissions from the granted list 
    238         $this->permissions_granted = array_diff($this->permissions_granted, $this->permissions_denied); 
     227        $this->grant( $permissions, 'deny' ); 
    239228    } 
    240229 
     
    247236        $permissions = Utils::single_array( $permissions ); 
    248237        // Remove permissions from the granted list 
    249         $this->permissions_granted = array_diff($this->permissions_granted, $permissions); 
    250         // Remove permissions from the denied list 
    251         $this->permissions_denied = array_diff($this->permissions_denied, $permissions); 
     238        $this->permissions = array_diff_key( $this->permissions, $permissions ); 
     239        foreach ( $permissions as $permission ) { 
     240            ACL::revoke_group_permission( $this->id, $permission ); 
     241        } 
    252242    } 
    253243 
     
    260250     * @see ACL::user_can() 
    261251    **/ 
    262     public function can( $permission ) 
     252    public function can( $permission, $access = 'full' ) 
    263253    { 
    264254        $permission= ACL::token_id( $permission ); 
    265         if ( in_array( $permission, $this->permissions_denied ) ) { 
    266             return false; 
    267         } 
    268         if ( in_array( $permission, $this->permissions_granted ) ) { 
     255        if ( isset( $this->permissions[$permission] ) && $this->permissions[$permission] == $access ) { 
    269256            return true; 
    270257        }