Changeset 121

Show
Ignore:
Timestamp:
08/27/08 18:50:48 (3 months ago)
Author:
mdawaffe
Message:

Simplify BP_Roles class. No need to access DB, no need for crazy self references, more obvious init action

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/includes/class.bp-roles.php

    r65 r121  
    22 
    33class BP_Roles { 
    4         var $db; 
    5  
    6         var $roles; 
    7  
    84        var $role_objects = array(); 
    95        var $role_names = array(); 
    10         var $role_key; 
    11         var $use_db = true; 
    126 
    13         function BP_Roles( &$db ) { 
     7        function BP_Roles() { 
    148                $this->__construct(); 
    159        } 
    1610 
    17         function __construct() { 
    18                 $this->db =& $db; 
    19                 $this->role_key =& $this->db->prefix; 
    20  
    21                 $this->get_roles(); 
    22  
    23                 if ( empty($this->roles) ) 
    24                         return; 
    25  
    26                 $this->role_objects = array(); 
    27                 $this->role_names =  array(); 
    28                 foreach ($this->roles as $role => $data) { 
    29                         $this->role_objects[$role] = new BP_Role($role, $this->roles[$role]['capabilities'], $this); 
    30                         $this->role_names[$role] = $this->roles[$role]['name']; 
    31                 } 
    32         } 
    33  
    34         function get_roles() { 
    35                 $this->roles = apply_filters( 'get_roles', array() ); 
     11        function __construct( &$db ) { 
     12                do_action_ref_array('init_roles', array(&$this) ); 
    3613        } 
    3714 
     
    3916                if ( isset($this->roles[$role]) ) 
    4017                        return; 
    41  
    42                 $this->roles[$role] = array( 
    43                         'name' => $display_name, 
    44                         'capabilities' => $capabilities); 
    4518 
    4619                $this->role_objects[$role] = new BP_Role($role, $capabilities, $this); 
     
    5528                unset($this->role_objects[$role]); 
    5629                unset($this->role_names[$role]); 
    57                 unset($this->roles[$role]); 
    5830        } 
    5931 
    6032        function add_cap($role, $cap, $grant = true) { 
    61                 $this->roles[$role]['capabilities'][$cap] = $grant; 
     33                if ( isset($this->role_objects[$role]) ) 
     34                        $this->role_objects[$role]->add_cap($cap, $grant); 
    6235        } 
    6336 
    6437        function remove_cap($role, $cap) { 
    65                 unset($this->roles[$role]['capabilities'][$cap]); 
     38                if ( isset($this->role_objects[$role]) ) 
     39                        $this->role_objects[$role]->remove_cap($cap, $grant); 
    6640        } 
    6741 
     
    8862 
    8963class BP_Role { 
    90         var $bp_roles; 
    91  
    9264        var $name; 
    9365        var $capabilities; 
    9466 
    95         function BP_Role($role, $capabilities, &$bp_roles) { 
    96                 $this->bp_roles =& $bp_roles; 
     67        function BP_Role($role, $capabilities) { 
    9768                $this->name = $role; 
    9869                $this->capabilities = $capabilities; 
     
    10172        function add_cap($cap, $grant = true) { 
    10273                $this->capabilities[$cap] = $grant; 
    103                 $this->bp_roles->add_cap($this->name, $cap, $grant); 
    10474        } 
    10575 
    10676        function remove_cap($cap) { 
    10777                unset($this->capabilities[$cap]); 
    108                 $this->bp_roles->remove_cap($this->name, $cap); 
    10978        } 
    11079 
    11180        function has_cap($cap) { 
    11281                $capabilities = apply_filters('role_has_cap', $this->capabilities, $cap, $this->name); 
    113                 if ( !empty($capabilities[$cap]) ) 
    114                         return $capabilities[$cap]; 
    115                 else 
    116                         return false; 
     82                $grant = !empty( $capabilities[$cap] ); 
     83                return apply_filters("{$this->name}_has_cap", $grant); 
    11784        } 
    11885 
    11986} 
    120  
    121 ?>