Show
Ignore:
Timestamp:
09/04/08 03:47:39 (4 months ago)
Author:
bjohnson
Message:

schema06: Initial go at re-writing ACL class to work with new permission schema.

Files:
1 modified

Legend:

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

    r1688 r2438  
    4444        } 
    4545        Plugins::act('permission_create_before', $name, $description); 
    46         $result= DB::query('INSERT INTO {permissions} (name, description) VALUES (?, ?)', array( $name, $description) ); 
     46        $result= DB::query('INSERT INTO {tokens} (name, description) VALUES (?, ?)', array( $name, $description) ); 
     47 
    4748        if ( ! $result ) { 
    4849            // if it didn't work, don't bother trying to log it 
     
    6667        } 
    6768 
    68         // Use ids internall for permissions 
    69         $permission= ACL::permission_id( $permission ); 
    70  
    71         $allow= true; 
     69        // grab permission ID 
     70        $permission = ACL::permission_id( $permission ); 
     71 
     72        $allow = true; 
    7273        // plugins have the opportunity to prevent deletion 
    73         $allow= Plugins::filter('permission_destroy_allow', $allow, $permission); 
     74        $allow = Plugins::filter('permission_destroy_allow', $allow, $permission); 
    7475        if ( ! $allow ) { 
    7576            return false; 
     
    7778        Plugins::act('permission_destroy_before', $permission ); 
    7879        // capture the permission name 
    79         $name= DB::get_value( 'SELECT name FROM {permissions} WHERE id=?', array( $permission ) ); 
     80        $name = DB::get_value( 'SELECT name FROM {tokens} WHERE id=?', array( $permission ) ); 
    8081        // remove all references to this permissions 
    81         $result= DB::query( 'DELETE FROM {groups_permissions} WHERE permission_id=?', array( $permission ) ); 
     82        $result = DB::query( 'DELETE FROM {group_token_permissions} WHERE permission_id=?', array( $permission ) ); 
     83        $result = DB::query( 'DELETE FROM {user_token_permissions} WHERE permission_id=?', array( $permission ) ); 
    8284        // remove this permission 
    83         $result= DB::query( 'DELETE FROM {permissions} WHERE permissions_id=?', array( $permission ) ); 
     85        $result = DB::query( 'DELETE FROM {tokens} WHERE id=?', array( $permission ) ); 
    8486        if ( ! $result ) { 
    8587            // if it didn't work, don't bother trying to log it 
     
    102104            $order= 'id'; 
    103105        } 
    104         $permissions= DB::get_results( 'SELECT id, name, description FROM {permissions} ORDER BY ' . $order ); 
     106        $permissions= DB::get_results( 'SELECT id, name, description FROM {tokens} ORDER BY ' . $order ); 
    105107        return $permissions ? $permissions : array(); 
    106108    } 
     
    116118            return false; 
    117119        } else { 
    118             return DB::get_value( 'SELECT name FROM {permissions} WHERE id=?', array( $id ) ); 
     120            return DB::get_value( 'SELECT name FROM {tokens} WHERE id=?', array( $id ) ); 
    119121        } 
    120122    } 
     
    131133        } 
    132134        $name= self::normalize_permission( $name ); 
    133         return DB::get_value( 'SELECT id FROM {permissions} WHERE name=?', array( $name ) ); 
     135        return DB::get_value( 'SELECT id FROM {tokens} WHERE name=?', array( $name ) ); 
    134136    } 
    135137 
     
    147149            $permission= self::normalize_permission( $permission ); 
    148150        } 
    149         return DB::get_value( "SELECT description FROM {permissions} WHERE $query=?", array( $permission ) ); 
     151        return DB::get_value( "SELECT description FROM {tokens} WHERE $query=?", array( $permission ) ); 
    150152    } 
    151153 
     
    164166            $permission= self::normalize_permission( $permission ); 
    165167        } 
    166         return ( DB::get_value( "SELECT COUNT(id) FROM {permissions} WHERE $query=?", array( $permission ) ) > 0 ); 
     168        return ( DB::get_value( "SELECT COUNT(id) FROM {tokens} WHERE $query=?", array( $permission ) ) > 0 ); 
    167169    } 
    168170 
     
    193195     * @param mixed $group A group ID or name 
    194196     * @param mixed $permission An action ID or name 
     197     * @param string $access Check for 'read', 'write', or 'full' access 
    195198     * @return bool Whether the group can perform the action 
    196199    **/ 
    197     public static function group_can( $group, $permission ) 
     200    public static function group_can( $group, $permission, $access = 'full' ) 
    198201    { 
    199202        // Use only numeric ids internally 
    200         $group= UserGroup::id( $group ); 
    201         $permission= ACL::permission_id( $permission ); 
    202         $result= DB::get_value( 'SELECT denied FROM {groups_permissions} WHERE permission_id=? AND group_id=?', array( $permission, $group ) ); 
    203         if ( 0 === intval($result) ) { 
     203        $group = UserGroup::id( $group ); 
     204        $permission = ACL::permission_id( $permission ); 
     205        $sql = <<<SQL 
     206SELECT p.name FROM {group_token_permissions} gp, {permissions} p WHERE 
     207gp.group_id=? AND gp.token_id=? AND gp.permission_id=p.id; 
     208SQL; 
     209        $result = DB::get_values( $sql ); 
     210        if ( $result == $access ) { 
    204211            // the permission has been granted to this group 
    205212            return true; 
     
    214221     * @param mixed $user A user object, user ID or a username 
    215222     * @param mixed $permission A permission ID or name 
     223     * @param string $access Check for 'read', 'write', or 'full' access 
    216224     * @return bool Whether the user can perform the action 
    217225    **/ 
    218     public static function user_can( $user, $permission ) 
     226    public static function user_can( $user, $permission, $access = 'full' ) 
    219227    { 
    220228        // Use only numeric ids internally 
     
    232240        } 
    233241 
    234         // we select the "denied" value from all the permissions 
    235         // assigned to all the groups to which this user is a member. 
    236         // array_unique() should consolidate this down to, at most, 
    237         // two values: 0 and 1. 
    238         $permissions= DB::get_column('SELECT gp.denied from {groups_permissions} gp, {users_groups} g where gp.group_id = g.group_id and g.user_id=? and permission_id=?', array( $user_id, $permission ) ); 
    239  
    240         // if any group is explicitly denied access to this permission, 
    241         // this user is denied access to that permission 
    242         if ( in_array( 1, $permissions ) ) { 
    243             return false; 
    244         } 
    245         // if the permission is not explicitly denied, make sure it's 
    246         // explicitly granted.  If it is, the user can do this. 
    247         if ( in_array( 0, $permissions, true ) ) { 
     242        /** 
     243         * Jay Pipe's explanation of the following SQL 
     244         * 1) Look into user_permissions for the user and the token.   
     245         * If exists, use that permission flag for the check. If not,  
     246         * go to 2) 
     247         * 
     248         * 2) Look into the group_permissions joined to  
     249         * users_groups for the user and the token.  Order the results  
     250         * by the permission_id flag. The lower the flag value, the  
     251         * fewest permissions that group has. Use the first record's  
     252         * permission flag to check the ACL. 
     253         * 
     254         * This gives the system very fine grained control and grabbing  
     255         * the permission flag and can be accomplished in a single SQL  
     256         * call. 
     257         */  
     258        $sql = <<<SQL 
     259SELECT COALESCE(permission_id, 0) as permission_id 
     260FROM ( 
     261( 
     262  SELECT permission_id 
     263  FROM {user_token_permissions} 
     264  WHERE user_id = :user_id 
     265  AND token_id = :token_id 
     266) AS up 
     267UNION ALL 
     268( 
     269  SELECT gp.permission_id 
     270  FROM {users_groups} ug 
     271  INNER JOIN {group_token_permissions} gp 
     272  ON ug.group_id = gp.group_id 
     273  AND ug.user_id = :user_id 
     274  AND gp.token_id = :token_id 
     275  ORDER BY permission_id ASC 
     276  LIMIT 1 
     277) 
     278) 
     279LIMIT 1;  
     280SQL; 
     281        $result = DB::get_value( $sql, array( ':user_id' => $user_id, ':token_id' => $permission ); 
     282 
     283        // TODO: modify above call to return the permission name rather than the ID 
     284        // For now, I'll just look for a result > 0 
     285        if ( $result !== FALSE && intval($result) > 0 ) { 
    248286            return true; 
    249287        } 
     288 
    250289        // if the permission is neither denied nor granted, they're not 
    251290        // allowed to do it.