Changeset 961

Show
Ignore:
Timestamp:
10/19/07 00:38:46 (1 year ago)
Author:
mikey
Message:

made the password filter configurable with a different encoder

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubPasswordFilterAnnotation.php

    r731 r961  
    2828     * @var  int 
    2929     */ 
    30     protected $minLength = 6; 
     30    protected $minLength    = 6; 
     31    /** 
     32     * the encoder class to be used 
     33     * 
     34     * @var  stubReflectionClass 
     35     */ 
     36    protected $encoderClass = null; 
    3137 
    3238    /** 
     
    4147 
    4248    /** 
     49     * sets the encoder class to be used 
     50     * 
     51     * @param  stubReflectionClass  $encoder 
     52     */ 
     53    public function setEncoder(stubReflectionClass $encoderClass) 
     54    { 
     55        $this->encoderClass = $encoderClass; 
     56    } 
     57 
     58    /** 
    4359     * returns the filter defined by the annotation 
    4460     * 
     
    4864    protected function doGetFilter() 
    4965    { 
    50         $passwordFilter  = new stubPasswordFilter($this->createRVEFactory(), new stubMinLengthValidator($this->minLength)); 
     66        $passwordFilter = new stubPasswordFilter($this->createRVEFactory(), new stubMinLengthValidator($this->minLength)); 
     67        if (null !== $this->encoderClass) { 
     68            $passwordFilter->setEncoder($this->encoderClass->newInstance()); 
     69        } 
     70         
    5171        return $passwordFilter; 
    5272    } 
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubPasswordFilter.php

    r880 r961  
    77 * @subpackage  ipo_request_filters 
    88 */ 
    9 stubClassLoader::load('net.stubbles.ipo.request.filters.stubAbstractFilter'); 
     9stubClassLoader::load('net.stubbles.ipo.request.filters.stubAbstractFilter', 
     10                      'net.stubbles.php.string.stubStringEncoder' 
     11); 
    1012/** 
    1113 * Class for filtering passwords. 
     
    1416 * for a password. A minimum length validator can check the length of the password. 
    1517 * Additionally its possible to check against a list of non-allowed passwords 
    16  * (e.g. the username or the login name). The returned value is a md5- 
    17  * representation of the password. 
     18 * (e.g. the username or the login name). The returned value is an encoded 
     19 * representation of the password. By default the password will be encoded in 
     20 * md5. This can be changed by setting an instance of 
     21 * net.stubbles.php.string.stubStringEncoder via the setEncoder() method. 
    1822 * If the value is an array the fields with key 0 and 1 are compared. If they are 
    1923 * not equal the password is not allowed (can be used to prevent mistyped 
     
    4448     */ 
    4549    protected $nonAllowedValues = array(); 
     50    /** 
     51     * the encoder to be used for encoding the password 
     52     * 
     53     * @var  stubStringEncoder 
     54     */ 
     55    protected $encoder; 
    4656 
    4757    /** 
     
    100110 
    101111    /** 
     112     * sets the encoder to be used to encode the password 
     113     * 
     114     * @param  stubStringEncoder  $encoder 
     115     */ 
     116    public function setEncoder($encoder) 
     117    { 
     118        $this->encoder = $encoder; 
     119    } 
     120 
     121    /** 
     122     * returns the encoder to be used to encode the password 
     123     * 
     124     * @return  stubStringEncoder 
     125     */ 
     126    public function getEncoder() 
     127    { 
     128        return $this->encoder; 
     129    } 
     130 
     131    /** 
    102132     * check if entered passwords fulfill password conditions 
    103133     * 
     
    108138    public function execute($value) 
    109139    { 
    110         if (is_array($value) == true) { 
    111             if ($value[0] != $value[1]) { 
     140        if (is_array($value) === true) { 
     141            if ($value[0] !== $value[1]) { 
    112142                throw new stubFilterException($this->rveFactory->create('PASSWORDS_NOT_EQUAL')); 
    113143            } 
     
    116146        } 
    117147 
    118         if (false == $this->isRequired && strlen($value) == 0) { 
     148        if (false === $this->isRequired && strlen($value) === 0) { 
    119149            return null; 
    120150        } 
    121151 
    122         if ($this->minLength->validate($value) == false) { 
     152        if ($this->minLength->validate($value) === false) { 
    123153            throw new stubFilterException($this->rveFactory->create('STRING_TOO_SHORT')->setValues($this->minLength->getCriteria())); 
    124154        } 
    125155 
    126         if (in_array($value, $this->nonAllowedValues) == true) { 
     156        if (in_array($value, $this->nonAllowedValues) === true) { 
    127157            throw new stubFilterException($this->rveFactory->create('PASSWORD_INVALID')); 
    128158        } 
     
    145175    protected function secure($password) 
    146176    { 
     177        if (null !== $this->encoder) { 
     178            return $this->encoder->encode($password); 
     179        } 
     180         
    147181        return md5($password); 
    148182    } 
  • trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubPasswordFilterAnnotationTestCase.php

    r731 r961  
    3737    { 
    3838        $passwordFilter = $this->passwordFilterAnnotation->getFilter(); 
    39         $passwordFilter = $this->passwordFilterAnnotation->getFilter(); 
    4039        $this->assertIsA($passwordFilter, 'stubPasswordFilter'); 
    4140        $this->assertIsA($passwordFilter->getMinLengthValidator(), 'stubMinLengthValidator'); 
    4241        $this->assertEqual($passwordFilter->getMinLengthValidator()->getValue(), 6); 
     42        $this->assertNull($passwordFilter->getEncoder()); 
    4343    } 
    4444 
     
    5353        $this->assertIsA($passwordFilter->getMinLengthValidator(), 'stubMinLengthValidator'); 
    5454        $this->assertEqual($passwordFilter->getMinLengthValidator()->getValue(), 8); 
     55        $this->assertNull($passwordFilter->getEncoder()); 
     56    } 
     57 
     58    /** 
     59     * assert that the encoder is set correct 
     60     */ 
     61    public function testWithEncoder() 
     62    { 
     63        $this->passwordFilterAnnotation->setEncoder(new stubReflectionClass('net.stubbles.php.string.stubMd5Encoder')); 
     64        $passwordFilter = $this->passwordFilterAnnotation->getFilter(); 
     65        $this->assertIsA($passwordFilter, 'stubPasswordFilter'); 
     66        $this->assertIsA($passwordFilter->getEncoder(), 'stubMd5Encoder'); 
    5567    } 
    5668} 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubPasswordFilterTestCase.php

    r880 r961  
    1010Mock::generate('stubRequestValueErrorFactory'); 
    1111Mock::generate('stubValidator'); 
     12Mock::generate('stubStringEncoder'); 
    1213/** 
    1314 * Tests for ipo.request.filters.stubPasswordFilter 
     
    110111        $this->passwordFilter->execute(array('abcdd', 'abcdd')); 
    111112    } 
     113 
     114    /** 
     115     * test that the encoder is used if set 
     116     */ 
     117    public function testWithEncoder() 
     118    { 
     119        $this->mockMinLengthValidator->setReturnValue('validate', true); 
     120        $mockEncoder = new MockstubStringEncoder(); 
     121        $mockEncoder->setReturnValue('encode', 'encodedValue'); 
     122        $mockEncoder->expectOnce('encode'); 
     123        $this->passwordFilter->setEncoder($mockEncoder); 
     124        $this->assertEqual($this->passwordFilter->execute('foo'), 'encodedValue'); 
     125    } 
    112126} 
    113127?>