Changeset 2862

Show
Ignore:
Timestamp:
11/24/08 04:23:30 (7 weeks ago)
Author:
ringmaster
Message:

User::identify() now always returns a User object. If a user is not logged in, an anonymouse User instance is created. This User instance will house default permissions used to fetch posts to which an anonymous visitor should have access.

Location:
trunk/htdocs/system
Files:
20 modified

Legend:

Unmodified
Added
Removed
  • trunk/htdocs/system/admin/db_profiling.php

    r2592 r2862  
    11<?php 
    2 if (! isset($_GET['db_profile']) ||  ( User::identify() == FALSE ) ) 
     2if (! isset($_GET['db_profile']) ||  ( !User::identify()->loggedin ) ) 
    33{ 
    44    return; 
     
    5050<?php }?> 
    5151</div> 
    52 <?php   
     52<?php 
    5353  $total_time_querying+= $profile->total_time; 
    5454} 
  • trunk/htdocs/system/classes/acl.php

    r2859 r2862  
    2424    const ACCESS_NONEXISTANT_PERMISSION = true; 
    2525 
    26     private static $access_names = array( 'read', 'write' ); 
     26    private static $access_names = array( 'read', 'write', 'delete' ); 
    2727 
    2828    /** 
     
    3333     */ 
    3434    public static function access_check( $permission, $access ) 
    35     {        
     35    { 
    3636        $bitmask = new Bitmask( self::$access_names, $permission ); 
    37          
     37 
    3838        if ( $access == 'full' ) { 
    3939            return $bitmask->read && $bitmask->write; 
     
    223223     * Determine whether a user can perform a specific action 
    224224     * @param mixed $user A user object, user ID or a username 
    225      * @param mixed $permission A permission ID or name 
     225     * @param mixed $token A permission ID or name 
    226226     * @param string $access Check for 'read', 'write', or 'delete' access 
    227227     * @return bool Whether the user can perform the action 
    228228    **/ 
    229     public static function user_can( $user, $permission, $access = 'write' ) 
     229    public static function user_can( $user, $token, $access = 'read' ) 
     230    { 
     231 
     232        $result = self::get_user_token_permissions( $user, $token ); 
     233 
     234        if ( isset( $result ) && self::access_check( $result, $access ) ) { 
     235            return true; 
     236        } 
     237 
     238        // either the permission hasn't been granted, or it's been 
     239        // explicitly denied. 
     240        return false; 
     241    } 
     242 
     243    /** 
     244     * Determine whether a user is denied permission to perform a specific action 
     245     * @param mixed $user A user object, user ID or a username 
     246     * @param mixed $token A permission ID or name 
     247     * @return bool Whether the user can perform the action 
     248     **/ 
     249    public static function user_cannot( $user, $token ) 
     250    { 
     251 
     252        $result = self::get_user_token_permissions( $user, $token ); 
     253 
     254        if ( isset( $result ) && $result == 0 ) { 
     255            return true; 
     256        } 
     257 
     258        // either the permission hasn't been granted, or it's been 
     259        // explicitly denied. 
     260        return false; 
     261    } 
     262 
     263 
     264    /** 
     265     * Return the permission to a specific token for a specific user 
     266     * 
     267     * @param mixed $user A User object instance or user id 
     268     * @param mixed $token A token string or if 
     269     * @return integer A permission bitmask integer 
     270     */ 
     271    public static function get_user_token_permissions( $user, $token ) 
    230272    { 
    231273        // Use only numeric ids internally 
    232         $permission = self::token_id( $permission ); 
     274        $token = self::token_id( $token ); 
    233275 
    234276        /** 
     
    236278         * When ACL is functional ACCESS_NONEXISTANT_PERMISSION should be false by default. 
    237279         */ 
    238         if ( is_null( $permission) ) { 
     280        if ( is_null( $token) ) { 
    239281            return self::ACCESS_NONEXISTANT_PERMISSION; 
    240282        } 
     
    283325  LIMIT 1; 
    284326SQL; 
    285         $result = DB::get_value( $sql, array( ':user_id' => $user_id, ':token_id' => $permission ) ); 
    286  
    287         if ( isset( $result ) && self::access_check( $result, $access ) ) { 
    288             return true; 
    289         } 
    290  
    291         // either the permission hasn't been granted, or it's been 
    292         // explicitly denied. 
    293         return false; 
     327        $result = DB::get_value( $sql, array( ':user_id' => $user_id, ':token_id' => $token ) ); 
     328 
     329        return $result; 
    294330    } 
    295331 
     
    305341        if ( is_numeric( $user ) ) { 
    306342            $user_id = $user; 
    307         } else { 
     343        } 
     344        else { 
    308345            if ( ! $user instanceof User ) { 
    309346                $user = User::get( $user ); 
     
    311348            $user_id = $user->id; 
    312349        } 
    313          
    314         $result = DB::get_results( 'SELECT token_id, permission_id FROM {user_token_permissions} WHERE user_id = ?', array( $user_id ) ); 
    315         $bitmask = new Bitmask ( self::$access_names ); 
     350 
     351        $sql = <<<SQL 
     352SELECT token_id, permission_id 
     353  FROM user_token_permissions 
     354  WHERE user_id = :user_id 
     355UNION ALL 
     356SELECT gp.token_id, gp.permission_id 
     357  FROM users_groups ug 
     358  INNER JOIN group_token_permissions gp 
     359  ON ug.group_id = gp.group_id 
     360  AND ug.user_id = :user_id 
     361  ORDER BY token_id ASC 
     362SQL; 
     363        $result = DB::get_results( $sql, array( ':user_id' => $user_id ) ); 
     364 
     365        $bitmask = new Bitmask ( self::$access_names, $access ); 
    316366        $tokens = array(); 
    317367 
     
    339389            $permission_id = 0; // default is 'deny' (bitmask 0) 
    340390        } 
    341          
     391 
    342392        $bitmask = new Bitmask( self::$access_names, $permission_id ); 
    343          
     393 
    344394        if ( $access == 'full' || $access == 'deny' ) { 
    345395            if ( $access == 'full' ) { 
     
    384434            $permission_id = 0; // default is 'deny' (bitmask 0) 
    385435        } 
    386          
     436 
    387437        $bitmask = new Bitmask( self::$access_names, $permission_id ); 
    388          
     438 
    389439        if ( $access == 'full' || $access == 'deny' ) { 
    390440            if ( $access == 'full' ) { 
  • trunk/htdocs/system/classes/adminhandler.php

    r2861 r2862  
    2121    { 
    2222        $user = User::identify(); 
    23         if ( !$user ) { 
     23        if ( !$user->loggedin ) { 
    2424            Session::add_to_set( 'login', $_SERVER['REQUEST_URI'], 'original' ); 
    2525            if( URL::get_matched_rule()->name == 'admin_ajax' ) { 
  • trunk/htdocs/system/classes/ajaxhandler.php

    r2606 r2862  
    1515     * The following example would set the context of 'foo' and trigger 
    1616     * the plugin action 'ajax_foo'. 
    17      *  
     17     * 
    1818     * <code>URL::get( 'ajax', 'context=foo' );</code> 
    19      *  
     19     * 
    2020     */ 
    2121    public function act_ajax() 
     
    2323        /** 
    2424         * Triggers the ajax plugin action for the context. 
    25          *  
     25         * 
    2626         * @see AjaxHandler::act_ajax() 
    2727         * @action ajax_{$context} 
     
    3333     * Handles incoming ajax requests for which the user must be authenticated. 
    3434     * Forwards the request to plugin actions for the "context" portion of the URL. 
    35      *  
     35     * 
    3636     * @see act_ajax() 
    3737     */ 
     
    3939    { 
    4040        $user = User::identify(); 
    41         if ($user !== FALSE) { 
     41        if ($user->loggedin) { 
    4242            /** 
    4343             * Triggers the ajax plugin action for the context if user is authenticated. 
    44              *  
     44             * 
    4545             * @see act_auth_ajax() 
    4646             * @action ajax_auth_{$context} 
  • trunk/htdocs/system/classes/atomhandler.php

    r2616 r2862  
    5252            } 
    5353 
    54             if ( ( $force != FALSE ) && ( !$this->user = User::identify() ) ) { 
     54            $this->user = User::identify(); 
     55            if ( ( $force != FALSE ) && ( !$this->user->loggedin ) ) { 
    5556                header( 'HTTP/1.1 401 Unauthorized' ); 
    5657                header( 'Status: 401 Unauthorized' ); 
     
    489490            $self = URL::get( 'atom_entry' ); 
    490491            $id = isset( $params['slug'] ) ? $params['slug'] : 'atom_entry'; 
    491              
     492 
    492493            $user = User::get_by_id( $post->user_id ); 
    493494            $title = ( $this->is_auth() ) ? htmlspecialchars( $post->title ) : htmlspecialchars( $post->title_atom ); 
    494495            $content = ( $this->is_auth() ) ? htmlspecialchars( $post->content ) : htmlspecialchars( $post->content_atom ); 
    495              
     496 
    496497            $xml = $this->create_atom_wrapper( $alternate, $self, $id ); 
    497              
     498 
    498499            $entry = $xml->addChild('entry'); 
    499500            $entry->addAttribute( 'xmlns', 'http://www.w3.org/2005/Atom' ); 
  • trunk/htdocs/system/classes/bitmask.php

    r2860 r2862  
    1414     * @param integer $value (optional) a combined bitmask value 
    1515     */ 
    16     public function __construct( $flags = null, $value = null ) { 
     16    public function __construct( $flags = null, $value = null ) 
     17    { 
    1718        if ( ! is_array( $flags ) ) { 
    1819            throw new InvalidArgumentException(_t('Bitmask constructor expects either no arguments or an array as a first argument')); 
     
    3233     * @param on    on or off? 
    3334     */ 
    34     public function __set( $bit, $on ) { 
     35    public function __set( $bit, $on ) 
     36    { 
    3537        switch( $bit ) { 
    3638            case 'value': 
     
    6365     * @return boolean 
    6466     */ 
    65     public function __get( $bit ) { 
     67    public function __get( $bit ) 
     68    { 
    6669        if ( is_string( $bit ) ) { 
    6770            $bit = array_search( $bit, $this->flags ); 
  • trunk/htdocs/system/classes/comment.php

    r2719 r2862  
    273273    private function get_post( $use_cache = TRUE ) 
    274274    { 
    275         if ( ! isset( $this->post_object ) || ( ! $use_cache)  ) 
    276         { 
     275        if ( ! isset( $this->post_object ) || ( ! $use_cache)  ) { 
    277276            $this->post_object = Posts::get( array('id' => $this->post_id, 'fetch_fn' => 'get_row') ); 
    278277        } 
     
    388387            return self::$comment_status_actions[$statuses[$name]]; 
    389388        } 
    390          
     389 
    391390        return ''; 
    392391    } 
  • trunk/htdocs/system/classes/eventlog.php

    r2736 r2862  
    8686            'ip' => sprintf("%u", ip2long( $_SERVER['REMOTE_ADDR'] ) ), 
    8787        ) ); 
    88         if ( $user = User::identify() ) { 
     88        $user = User::identify(); 
     89        if ( $user->loggedin ) { 
    8990            $log->user_id = $user->id; 
    9091        } 
     
    134135        // Put incoming parameters into the local scope 
    135136        $paramarray = Utils::get_params( $paramarray ); 
    136           
     137 
    137138        // Get any full-query parameters 
    138139        extract( $paramarray ); 
  • trunk/htdocs/system/classes/feedbackhandler.php

    r2822 r2862  
    1616    public function act_add_comment() 
    1717    { 
    18          
     18 
    1919        $defaults = array( 
    2020            'name' => '', 
     
    2323            'content' => '' 
    2424        ); 
    25          
     25 
    2626        // We need to get the post anyway to redirect back to the post page. 
    2727        $post = Post::get( array( 'id'=>$this->handler_vars['id'] ) ); 
     
    3131            die(); 
    3232        } 
    33          
     33 
    3434        // make sure all our default values are set so we don't throw undefined index errors 
    3535        foreach ( $defaults as $k => $v ) { 
     
    116116        // Should this really be here or in a default filter? 
    117117        // In any case, we should let plugins modify the status after we set it here. 
    118         if( ( $user = User::identify() ) && ( $comment->email == $user->email ) ) { 
     118        $user = User::identify(); 
     119        if( ( $user->loggedin ) && ( $comment->email == $user->email ) ) { 
    119120            $comment->status = Comment::STATUS_APPROVED; 
    120121        } 
  • trunk/htdocs/system/classes/logentry.php

    r2592 r2862  
    216216            'fetch_fn' => 'get_row', 
    217217        ); 
    218         if ( $user = User::identify() ) { 
     218        $user = User::identify(); 
     219        if ( $user->loggedin ) { 
    219220            $defaults['where'][]= array( 
    220221                'user_id' => $user->id, 
     
    273274        $allow = true; 
    274275        $allow = Plugins::filter( 'logentry_delete_allow', $allow, $this ); 
    275         if ( ! $allow ) {  
     276        if ( ! $allow ) { 
    276277            return; 
    277278        } 
  • trunk/htdocs/system/classes/post.php

    r2851 r2862  
    319319            'fetch_fn' => 'get_row', 
    320320        ); 
    321         if ( $user = User::identify() ) { 
     321        $user = User::identify(); 
     322        if ( $user->loggedin ) { 
    322323            $defaults['where'][]= array( 
    323324                'user_id' => $user->id, 
  • trunk/htdocs/system/classes/posts.php

    r2851 r2862  
    369369 
    370370                // Only show posts to which the current user has permission 
    371                 /* 
    372                 if( !isset( $paramset['ignore_permissions'] ) ) { 
    373                     $token_id_list = User::identify(); 
    374                     $joins['post_tokens__posts']= ' JOIN {post_tokens} ON {posts}.id= {post_tokens}.post_id AND {post_tokens}.token_id IN ()'; 
    375                 } 
    376                 */ 
     371                $paramset['ignore_permissions'] = true; 
     372                if(!isset($paramset['ignore_permissions'])) { 
     373                    $permission_token_ids = isset($paramset['override_permissions']) ? $paramset['override_permissions'] : ACL::user_tokens(User::identify(), 'read'); 
     374                    if(User::identify()->can('own_posts', 'read')) { 
     375 
     376                    } 
     377                    foreach(Post::list_active_post_types() as $name => $posttype) { 
     378                        if(User::identify()->can(Utils::slugify($name) . '_post_type', 'read')) { 
     379 
     380                        } 
     381                    } 
     382                    $joins['post_tokens__posts']= ' JOIN {post_tokens} ON {posts}.id= {post_tokens}.post_id AND ({post_tokens}.token_id IN ('.implode(',', $permission_token_ids).'))'; 
     383                } 
    377384 
    378385                // Concatenate the WHERE clauses 
  • trunk/htdocs/system/classes/theme.php

    r2851 r2862  
    107107        } 
    108108 
     109        if( !$this->template_engine->assigned( 'loggedin' ) ) { 
     110            $this->assign('loggedin', User::identify()->loggedin ); 
     111        } 
     112 
    109113        if( !$this->template_engine->assigned( 'page' ) ) { 
    110114            $this->assign('page', isset( $this->page ) ? $this->page : 1 ); 
     
    165169            unset( $where_filters['tag'] ); 
    166170        } 
    167         if ( User::identify() ) { 
     171        if ( User::identify()->loggedin ) { 
    168172            $where_filters['status']= isset( $_GET['preview'] ) ? Post::status( 'any' ) : Post::status( 'published' ); 
    169173        } 
     
    221225            $$key = $value; 
    222226        } 
    223          
     227 
    224228        $this->assign( 'page', isset($page)? $page:1 ); 
    225229 
  • trunk/htdocs/system/classes/user.php

    r2825 r2862  
    3030 
    3131    private $group_list = null; 
    32      
     32 
    3333    protected $url_args; 
    3434 
     
    8787            } 
    8888        } 
    89         return false; 
     89        $anonymous = new User(); 
     90        Plugins::act('create_anonymous_user', $anonymous); 
     91        return $anonymous; 
    9092    } 
    9193 
     
    412414    { 
    413415        return ACL::user_can( $this, $permission, $access ); 
     416    } 
     417 
     418    /** 
     419     * Determine if a user has been denied a specific permission 
     420     * 
     421     * @param string $permission The name of the permission to detect 
     422     * @return boolean True if this user has the requested permission, false if not 
     423     */ 
     424    public function cannot( $permission ) 
     425    { 
     426        return ACL::user_cannot( $this, $permission ); 
    414427    } 
    415428 
     
    527540            case 'displayname': 
    528541                return ( empty($this->info->displayname) ) ? $this->username : $this->info->displayname; 
     542            case 'loggedin': 
     543                return $this->id != 0; 
    529544            default: 
    530545                return parent::__get( $name ); 
  • trunk/htdocs/system/classes/userhandler.php

    r2752 r2862  
    1818        $name = InputFilter::filter( Controller::get_var( 'habari_username' ) ); 
    1919        $pass = InputFilter::filter( Controller::get_var( 'habari_password' ) ); 
    20          
    21          
     20 
     21 
    2222        if ( ( NULL != $name ) || ( NULL != $pass ) ) { 
    2323            $user = User::authenticate( $name, $pass ); 
     
    2828                $user->info->authenticate_time = date( 'Y-m-d H:i:s' ); 
    2929                $user->update(); 
    30                  
     30 
    3131                // Remove left over expired session error message. 
    3232                if ( Session::has_errors( 'expired_session' ) ) { 
     
    4343                        Session::add_to_set( 'last_form_data', $last_form_data['get'], 'get' ); 
    4444                    } 
    45                      
     45 
    4646                    /* Redirect to the correct admin page */ 
    4747                    $dest = explode( '/', substr( $login_session['original'], strpos( $login_session['original'], 'admin/' ) ) ); 
     
    9393    { 
    9494        // get the user from their cookie 
    95         if ( $user = User::identify() ) { 
     95        $user = User::identify(); 
     96        if ( $user->loggedin ) { 
    9697            Plugins::act( 'user_logout', $user ); 
    9798            // delete the cookie, and destroy the object 
  • trunk/htdocs/system/plugins/undelete/undelete.plugin.php

    r2592 r2862  
    33/** 
    44 * Undelete Class 
    5  *  
     5 * 
    66 * This class provides undelete functionality for posts and comments, and 
    77 * provides a trashcan interface for restoring items. 
     
    4141        } 
    4242    } 
    43      
     43 
    4444    /** 
    4545     * function actions_plugins_loaded 
     
    5353    /** 
    5454     * function filter_allow_post_delete 
    55      * This function is executed when the filter "before_post_delete" is  
     55     * This function is executed when the filter "before_post_delete" is 
    5656     * called just before a post is to be deleted. 
    5757     * This filter should return a boolean value to indicate whether 
     
    7979        } 
    8080    } 
    81      
     81 
    8282    function filter_post_actions($actions, $post) { 
    8383        if($post->status == Post::status('deleted')) { 
     
    8585            $actions['remove']['title']= _t('Permanently delete this post'); 
    8686        } 
    87          
     87 
    8888        return $actions; 
    8989    } 
     
    128128                $ui->out(); 
    129129                break; 
    130     &