Development

Changeset 7105

You must first sign up to be able to contribute.

Changeset 7105

Show
Ignore:
Timestamp:
01/20/08 08:06:10 (8 months ago)
Author:
fabien
Message:

refactored sfBasicSecurityFilter to allow more flexibility when extending the class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/filter/sfBasicSecurityFilter.class.php

    r5002 r7105  
    3030  public function execute($filterChain) 
    3131  { 
    32     // get the cool stuff 
    33     $controller = $this->context->getController(); 
    34     $user       = $this->context->getUser(); 
    35  
    36     // get the current action instance 
    37     $actionEntry    = $controller->getActionStack()->getLastEntry(); 
    38     $actionInstance = $actionEntry->getActionInstance(); 
    39  
    40     // disable security on [sf_login_module] / [sf_login_action] 
     32    // disable security on login and secure actions 
    4133    if ( 
    4234      (sfConfig::get('sf_login_module') == $this->context->getModuleName()) && (sfConfig::get('sf_login_action') == $this->context->getActionName()) 
     
    5042    } 
    5143 
    52     // get the credential required for this action 
    53     $credential = $actionInstance->getCredential(); 
    54  
    55     // for this filter, the credentials are a simple privilege array 
    56     // where the first index is the privilege name and the second index 
    57     // is the privilege namespace 
    58     // 
    5944    // NOTE: the nice thing about the Action class is that getCredential() 
    6045    //       is vague enough to describe any level of security and can be 
    6146    //       used to retrieve such data and should never have to be altered 
    62     if ($user->isAuthenticated()) 
    63     { 
    64       // the user is authenticated 
    65       if ($credential === null || $user->hasCredential($credential)) 
    66       { 
    67         // the user has access, continue 
    68         $filterChain->execute(); 
    69       } 
    70       else 
    71       { 
    72         // the user doesn't have access, exit stage left 
    73         $controller->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action')); 
    74  
    75         throw new sfStopException(); 
    76       } 
    77     } 
    78     else 
     47    if (!$this->context->getUser()->isAuthenticated()) 
    7948    { 
    8049      // the user is not authenticated 
    81       $controller->forward(sfConfig::get('sf_login_module'), sfConfig::get('sf_login_action')); 
     50      $this->forwardToLoginAction(); 
     51    } 
    8252 
    83       throw new sfStopException(); 
     53    // the user is authenticated 
     54    $credential = $this->getUserCredential(); 
     55    if (!is_null($credential) && !$this->context->getUser()->hasCredential($credential)) 
     56    { 
     57      // the user doesn't have access 
     58      $this->forwardToSecureAction(); 
    8459    } 
     60 
     61    // the user has access, continue 
     62    $filterChain->execute(); 
     63  } 
     64 
     65  /** 
     66   * Forwards the current request to the secure action. 
     67   * 
     68   * @throws sfStopException 
     69   */ 
     70  protected function forwardToSecureAction() 
     71  { 
     72    $this->context->getController()->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action')); 
     73 
     74    throw new sfStopException(); 
     75  } 
     76 
     77  /** 
     78   * Forwards the current request to the login action. 
     79   * 
     80   * @throws sfStopException 
     81   */ 
     82  protected function forwardToLoginAction() 
     83  { 
     84    $this->context->getController()->forward(sfConfig::get('sf_login_module'), sfConfig::get('sf_login_action')); 
     85 
     86    throw new sfStopException(); 
     87  } 
     88 
     89  /** 
     90   * Returns the credential required for this action. 
     91   * 
     92   * @return mixed The credential required for this action 
     93   */ 
     94  protected function getUserCredential() 
     95  { 
     96    return $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance()->getCredential(); 
    8597  } 
    8698}