Changeset 4871

Show
Ignore:
Timestamp:
09/02/08 17:35:22 (10 months ago)
Author:
jwage
Message:

Fixing coding standards.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/docs/cookbook/en/record-based-retrieval-security-template.txt

    r4855 r4871  
    1313 
    1414++ Template 
    15 <code> 
    16  
     15 
     16<code type="php"> 
    1717class gsSecurityTemplate extends Doctrine_Template 
    1818{ 
     
    2727    public function __construct(array $options) 
    2828    { 
    29         if( !isset($options['conditions']) || empty($options['conditions']) ) 
     29        if (!isset($options['conditions']) || empty($options['conditions'])) { 
    3030            throw new Doctrine_Exception('Unable to create security template without conditions'); 
     31        } 
    3132 
    3233        $this->_options = $options; 
     
    6566        $params  = $event->getParams(); 
    6667 
    67         if($class == $params['alias']) 
     68        if($class == $params['alias']) { 
    6869            return; 
     70        } 
    6971 
    7072        $q       = $event->getQuery(); 
    7173 
    7274        // only apply to the main protected table not chained tables... may break some situations 
    73         if(!$q->contains('FROM '.$class)) 
     75        if(!$q->contains('FROM '.$class)) { 
    7476            return; 
     77        } 
    7578 
    7679        $wheres = array(); 
     
    7982        $from = $q->getDqlPart('from'); 
    8083 
    81         foreach($this->_options['conditions'] as $rel_name => $conditions) 
    82         { 
     84        foreach ($this->_options['conditions'] as $rel_name => $conditions) { 
    8385            $apply = false; 
    84             foreach($conditions['apply_to'] as $val) 
    85             { 
    86                 if(in_array($val,self::$_credentials)) 
    87                 { 
     86            foreach ($conditions['apply_to'] as $val) { 
     87                if (in_array($val,self::$_credentials)) { 
    8888                    $apply = true; 
    8989                    break; 
     
    9191            } 
    9292 
    93             if($apply) 
    94             { 
     93            if ($apply) { 
    9594                $alias = $params['alias']; 
    9695                $aliases = array(); 
    9796                $aliases[] = $alias; 
    9897 
    99                 foreach($conditions['through'] as $key => $table) 
    100                 { 
     98                foreach ($conditions['through'] as $key => $table) { 
    10199                    $index = 0; 
    102100                    $found = false; 
    103                     foreach($from as $index => $val) 
    104                     { 
    105                         if(strpos($val,$table) !== false) 
    106                         { 
     101                    foreach ($from as $index => $val) { 
     102                        if (strpos($val,$table) !== false) { 
    107103                            $found = true; 
    108104                            break; 
     
    111107                    } 
    112108 
    113                     if($found) 
    114                     { 
     109                    if ($found) { 
    115110                        $vals = explode(' ', substr($from[$index],strpos($from[$index],$table))); 
    116111                        $alias = (count($vals) == 2) ? $vals[1]:$vals[0]; 
    117112                        $aliases[] = $alias; 
    118                     } 
    119                     else 
    120                     { 
     113                    } else { 
    121114                        $newalias = strtolower(substr($table,0,3)).self::$_alias_count++; 
    122115                        $q->leftJoin(end($aliases).'.'.$table.' '.$newalias); 
     
    130123        } 
    131124 
    132         if(!empty($wheres)) 
     125        if(!empty($wheres)) { 
    133126            $q->addWhere( '('.implode(' OR ',$wheres).')',$pars); 
     127        } 
    134128    } 
    135129 
     
    151145the box without the indexes - YMMV. 
    152146 
    153 <code> 
     147<code type="yaml"> 
    154148--- 
    155149Account: 
     
    236230    is_active: { type: boolean, default: true } 
    237231 
    238 #------------------------------------------------------------------------------------- 
    239232User: 
    240233  relations: 
     
    306299 
    307300Once you've built your models from the schema, you should see something like the following in your model's setUp function. 
    308 <code> 
    309  
     301 
     302<code type="php"> 
    310303$gssecuritytemplate0 = new gsSecurityTemplate(array('conditions' => array('Division' =>  array( 'through' =>  array( 0 => 'Division',  1 => 'UserDivision',  ),  'field' => 'user_id',  'apply_to' =>  array( 0 => 'division_manager',  ),  'exclude_for' =>  array( 0 => 'admin',  ), ), 'Branch' =>  array( 'through' =>  array( 0 => 'Branch',  1 => 'UserBranch',  ),  'field' => 'user_id',  'apply_to' =>  array( 0 => 'branch_manager',  ),  'exclude_for' =>  array( 0 => 'admin',  1 => 'division_manager',  2 => 'district_manager',  ), ), 'Salesperson' =>  array( 'through' =>  array( 0 => 'Salesperson',  1 => 'UserSalesperson',  ),  'field' => 'user_id',  'apply_to' =>  array( 0 => 'salesperson',  ),  'exclude_for' =>  array( 0 => 'admin',  1 => 'division_manager',  2 => 'district_manager',  3 => 'branch_manager',  ), ), 'District' =>  array( 'through' =>  array( 0 => 'Branch',  1 => 'District',  2 => 'UserDistrict',  ),  'field' => 'user_id',  'apply_to' =>  array( 0 => 'district_manager',  ),  'exclude_for' =>  array( 0 => 'admin',  1 => 'division_manager',  ), )))); 
    311304$this->actAs($gssecuritytemplate0); 
    312  
    313305</code> 
    314306 
    315307The last part you need to use is to provide the template with the running user's credentials and id. In my project's session bootstrapping I have the following ( I use the symfony MVC framework ). 
    316 <code> 
     308 
     309<code type="php"> 
    317310public function initialize($context, $parameters = null) 
    318311{ 
     
    326319This provides the credentials the user was given when they logged in as well as their id. 
    327320 
    328  
    329321++ User setup 
    330322 
     
    338330 
    339331The query below 
     332 
     333<code type="php"> 
     334  $accounts = Doctrine_Query::create()->from('Account a')->leftJoin('a.Branches b')->where('a.company_name LIKE ?','A%')->execute(); 
     335</code> 
     336 
     337produces the resulting sql. 
     338 
    340339<code> 
    341   $accounts = Doctrine_Query::create()->from('Account a')->leftJoin('a.Branches b')->where('a.company_name LIKE ?','A%')->execute(); 
    342 </code> 
    343  
    344 produces the resulting sql. 
    345  
    346 <code> 
    347 </code> 
    348  
    349340SELECT ... FROM accounts a2 LEFT JOIN branches b2 ON a2.branch_id = b2.id LEFT JOIN divisions d2 ON a2.division_id = d2.id LEFT JOIN user_divisions u2 ON d2.id = u2.division_id WHERE a2.company_name LIKE ? AND u2.user_id = ? ORDER BY a2.company_name 
    350  
    351341<code> 
    352342 
     
    355345returned to choose from. 
    356346 
    357  
    358347++ Restrictions 
    359348