Changeset 2848

Show
Ignore:
Timestamp:
11/23/08 19:57:54 (7 weeks ago)
Author:
bjohnson
Message:

Add a user_tokens() method to the ACL class which will return all the token
IDs for which a given user has a specific permission type (read, write, etc).

Also fixing formatting in the bitmask class.

Location:
trunk/htdocs/system/classes
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/htdocs/system/classes/acl.php

    r2834 r2848  
    3232    public static function __static() 
    3333    { 
    34         self::$access_ids = DB::get_keyvalue( 'SELECT name, id FROM {permissions};' ); 
     34        /*self::$access_ids = DB::get_keyvalue( 'SELECT name, id FROM {permissions};' ); 
    3535 
    3636        if ( ! isset(self::$access_ids) ) { 
    3737            self::$access_ids = array(); 
    3838        } 
     39        */ 
     40        self::$access_ids = array( 0 => 'read', 1 => 'write', 2 => 'deny' ); 
    3941        self::$access_names = array_flip( self::$access_ids ); 
    4042    } 
    4143 
    4244    /** 
    43      * Convert a permission access name (read, write, full, denied) into an ID 
     45     * Convert a permission access name (read, write, denied) into an ID 
    4446     * @param string The access name 
    4547     * @return mixed the ID of the permission, or boolean FALSE if it does not exist 
     
    351353 
    352354    /** 
     355     * Get all the tokens for a given user with a particular kind of access 
     356     * @param mixed $user A user object, user ID or a username 
     357     * @param string $access Check for 'read', 'write', or 'full' access 
     358     * @return array of token IDs 
     359    **/ 
     360    public static function user_tokens( $user, $access = 'write' ) 
     361    { 
     362        // convert $user and $access to IDs 
     363        if ( is_numeric( $user ) ) { 
     364            $user_id = $user; 
     365        } else { 
     366            if ( ! $user instanceof User ) { 
     367                $user = User::get( $user ); 
     368            } 
     369            $user_id = $user->id; 
     370        } 
     371        $access_id = self::access_id( $access ); 
     372         
     373        $tokens = DB::get_results( 'SELECT token_id FROM {user_token_permissions} WHERE user_id = ? AND permission_id = ?', array( $user_id, $access_id) ); 
     374        return $tokens; 
     375    } 
     376 
     377    /** 
    353378     * Grant a permission to a group 
    354379     * @param integer $group_id The group ID 
  • trunk/htdocs/system/classes/bitmask.php

    r2847 r2848  
    44 */ 
    55class Bitmask { 
    6   public $flags = array();  // set of flag bit masks 
    7   protected $value = 0;        // internal integer value of the bitmask 
     6    public $flags = array();  // set of flag bit masks 
     7    protected $value = 0;        // internal integer value of the bitmask 
    88 
    9   /** 
    10   * Constructor.  Takes an optional array parameter 
    11   * of bit flags to mask on. 
    12   * 
    13   * @param array $flags An array of flag names 
     9    /** 
     10    * Constructor.  Takes an optional array parameter 
     11    * of bit flags to mask on. 
     12    * 
     13    * @param array $flags An array of flag names 
    1414     * @param integer $value (optional) a combined bitmask value 
    1515     */ 
    16   public function __construct($flags = null, $value = null) { 
    17         if (! is_array($flags)) 
     16    public function __construct( $flags = null, $value = null ) { 
     17        if ( ! is_array( $flags ) ) { 
    1818            throw new InvalidArgumentException(_t('Bitmask constructor expects either no arguments or an array as a first argument')); 
     19        } 
    1920 
    2021        $this->flags = $flags; 
    21         if(!is_null($value)) { 
     22        if( ! is_null( $value ) ) { 
    2223            $this->value = $value; 
    2324        } 
    2425 
    25   } 
     26    } 
    2627 
    27   /** 
    28   * Magic setter method for flag values. 
    29   * 
    30   * @param bit   integer representing the mask bit 
    31   * @param on    on or off? 
    32   */ 
    33   public function __set($bit, $on) { 
    34         switch($bit) { 
     28    /** 
     29    * Magic setter method for flag values. 
     30    * 
     31    * @param bit   integer representing the mask bit 
     32    * @param on    on or off? 
     33    */ 
     34    public function __set( $bit, $on ) { 
     35        switch( $bit ) { 
    3536            case 'value': 
    3637                $this->value = $on; 
    3738                break; 
    3839            default: 
    39                 if (! is_bool($flags)) 
     40                if ( ! is_bool( $flags ) ) 
    4041                    throw new InvalidArgumentException(_t('Bitmask values must be boolean')); 
    41                 if( is_string($bit) ) { 
    42                     $bit = array_search($bit, $this->value); 
     42                if( is_string( $bit ) ) { 
     43                    $bit = array_search( $bit, $this->value ); 
    4344                } 
    44                 if( !is_int($bit)) 
     45                if( !is_int( $bit ) ) 
    4546                    throw new InvalidArgumentException(_t('Bitmask names must be pre-defined strings or bitmask indexes')); 
    46                 if($on) { 
    47                     $this->value |= pow(2, $bit); 
     47                if( $on ) { 
     48                    $this->value |= pow( 2, $bit ); 
    4849                } 
    4950                else { 
    50                     $this->value &= ~pow(2, $bit); 
     51                    $this->value &= ~pow( 2, $bit ); 
    5152                } 
    5253                break; 
     
    5556  } 
    5657 
    57   /** 
    58   * Magic getter method for flag status 
    59   * 
    60   * @param bit   integer representing th emask bit to test 
    61   * @return boolean 
    62   */ 
    63   public function __get($bit) { 
    64     if ( is_int($bit) ) { 
    65         $flags = array_values($this->flags); 
    66     } 
     58    /** 
     59    * Magic getter method for flag status 
     60    * 
     61    * @param bit   integer representing th emask bit to test 
     62    * @return boolean 
     63    */ 
     64    public function __get( $bi t) { 
     65        if ( is_int( $bit ) ) { 
     66            $flags = array_values( $this->flags ); 
     67        } 
    6768        else { 
    6869            $flags = $this->flags; 
    6970        } 
    70     if (!isset($flags[$bit])) 
    71       return false; 
    72     return (($this->value & $this->flags[$bit]) == $this->flags[$bit]); 
    73   } 
     71        if ( ! isset( $flags[$bit] ) ) { 
     72            return false; 
     73        } 
     74        return ( ( $this->value & $this->flags[$bit] ) == $this->flags[$bit] ); 
     75    } 
    7476 
    7577}