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

schema06: Committing more work in progress on ACL.

Files:
1 modified

Legend:

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

    r2438 r2439  
    2424    const ACCESS_NONEXISTANT_PERMISSION = true; 
    2525 
     26    private static $permission_ids = array(); 
     27 
     28    /** 
     29     * Convert a permission access name (read, write, full, denied) into an ID 
     30     * @param string The access name 
     31     * @return mixed the ID of the permission, or boolean FALSE if it does not exist 
     32     **/ 
     33    public static function permission_id( $name ) 
     34    { 
     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 
    2645    /** 
    2746     * Create a new permission, and save it to the Permissions table 
     
    6382    { 
    6483        // make sure the permission exists, first 
    65         if ( ! ACL::permission_exists( $permission ) ) { 
    66             return false; 
    67         } 
    68  
    69         // grab permission ID 
    70         $permission = ACL::permission_id( $permission ); 
     84        if ( ! self::token_exists( $permission ) ) { 
     85            return false; 
     86        } 
     87 
     88        // grab token ID 
     89        $permission = self::token_id( $permission ); 
    7190 
    7291        $allow = true; 
     
    7796        } 
    7897        Plugins::act('permission_destroy_before', $permission ); 
    79         // capture the permission name 
     98        // capture the permission token name 
    8099        $name = DB::get_value( 'SELECT name FROM {tokens} WHERE id=?', array( $permission ) ); 
    81100        // remove all references to this permissions 
     
    98117     * @return array an array of QueryRecord objects containing all permissions 
    99118    **/ 
    100     public static function all_permissions( $order= 'id' ) 
    101     { 
    102         $order= strtolower( $order ); 
     119    public static function all_permission_tokens( $order= 'id' ) 
     120    { 
     121        $order = strtolower( $order ); 
    103122        if ( ( 'id' != $order ) && ( 'name' != $order ) && ( 'description' != $order ) ) { 
    104123            $order= 'id'; 
    105124        } 
    106         $permissions= DB::get_results( 'SELECT id, name, description FROM {tokens} ORDER BY ' . $order ); 
     125        $permissions = DB::get_results( 'SELECT id, name, description FROM {tokens} ORDER BY ' . $order ); 
    107126        return $permissions ? $permissions : array(); 
    108127    } 
    109128 
    110129    /** 
    111      * Get a permission's name by its ID 
     130     * Get a permission token's name by its ID 
    112131     * @param int a permission ID 
    113132     * @return string the name of the permission, or boolean FALSE 
    114133    **/ 
    115     public static function permission_name( $id ) 
     134    public static function token_name( $id ) 
    116135    { 
    117136        if ( ! is_int( $id ) ) { 
     
    123142 
    124143    /** 
    125      * Get a permission's ID by its name 
     144     * Get a permission token's ID by its name 
    126145     * @param string the name of the permission 
    127146     * @return int the permission's ID 
    128147    **/ 
    129     public static function permission_id( $name ) 
     148    public static function token_id( $name ) 
    130149    { 
    131150        if( is_integer($name) ) { 
     
    137156 
    138157    /** 
    139      * Fetch a permission description from the DB 
     158     * Fetch a permission token's description from the DB 
    140159     * @param mixed a permission name or ID 
    141160     * @return string the description of the permission 
    142161    **/ 
    143     public static function permission_description( $permission ) 
     162    public static function token_description( $permission ) 
    144163    { 
    145164        if ( is_int( $permission) ) { 
     
    153172 
    154173    /** 
    155      * Determine whether a permission exists 
     174     * Determine whether a permission token exists 
    156175     * @param mixed a permission name or ID 
    157176     * @return bool whether the permission exists or not 
    158177    **/ 
    159     public static function permission_exists( $permission ) 
     178    public static function token_exists( $permission ) 
    160179    { 
    161180        if ( is_int( $permission ) ) { 
     
    202221        // Use only numeric ids internally 
    203222        $group = UserGroup::id( $group ); 
    204         $permission = ACL::permission_id( $permission ); 
     223        $permission = self::token_id( $permission ); 
    205224        $sql = <<<SQL 
    206225SELECT p.name FROM {group_token_permissions} gp, {permissions} p WHERE 
    207226gp.group_id=? AND gp.token_id=? AND gp.permission_id=p.id; 
    208227SQL; 
    209         $result = DB::get_values( $sql ); 
     228        $result = DB::get_value( $sql ); 
    210229        if ( $result == $access ) { 
    211230            // the permission has been granted to this group 
     
    227246    { 
    228247        // Use only numeric ids internally 
    229         $permission= ACL::permission_id( $permission ); 
     248        $permission= self::token_id( $permission ); 
    230249        // if we were given a user ID, use that to fetch the group membership from the DB 
    231250        if ( is_int( $user) ) { 
     
    294313 
    295314    /** 
     315     * Grant a permission to a group 
     316     * @param integer $group_id The group ID 
     317     * @param integer $token_id The permission token to grant 
     318     * @param string $access The kind of access to assign the group 
     319     * @return Result of the DB query 
     320     **/ 
     321    public static function grant_group( $group_id, $token_id, $access = 'full' ) 
     322    { 
     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 ) ); 
     325        return $result; 
     326    } 
     327 
     328    /** 
     329     * Grant a permission to a user  
     330     * @param integer $user_id The user ID 
     331     * @param integer $token_id The permission token to grant 
     332     * @param string $access The kind of access to assign the group 
     333     * @return Result of the DB query 
     334     **/ 
     335    public static function grant_user( $user_id, $token_id, $access = 'full' ) 
     336    { 
     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 ) ); 
     339        return $result; 
     340    } 
     341     
     342 
     343    /** 
    296344     * Convert a permission name into a valid format 
    297345     *