Show
Ignore:
Timestamp:
09/05/08 03:39:36 (4 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.

Files:
1 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    /**