Changeset 13490

Show
Ignore:
Timestamp:
06/08/08 17:59:58 (5 years ago)
Author:
kumatch
Message:

Refactored.

Location:
events/phpframework/piece_framework/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • events/phpframework/piece_framework/trunk/libs/Phwittr/Service.php

    r13488 r13490  
    1313require_once 'Phwittr/Config.php'; 
    1414require_once 'Phwittr/Service/Exception.php'; 
     15require_once 'Piece/Right/Validator/Email.php'; 
    1516 
    1617// {{{ Phwittr 
     
    117118    public function isAvailableUserName($userName) 
    118119    { 
    119         $mapper = Piece_ORM::getMapper('Users'); 
     120        Phwittr_Config::configurePieceORM(); 
     121        $mapper = Piece_ORM::getMapper('Users'); 
     122 
    120123        $user = $mapper->findByUserName($userName); 
    121124        if (is_null($user)) { 
     
    126129    } 
    127130 
     131    // }}} 
     132    // {{{ authenticate 
     133 
     134    /** 
     135     * 認証を行う 
     136     *  
     137     * @param string $user 
     138     * @param string $password 
     139     * @return boolean 
     140     */ 
     141    public function authenticate($name, $password) 
     142    { 
     143        Phwittr_Config::configurePieceORM(); 
     144        $mapper = Piece_ORM::getMapper('Users'); 
     145 
     146        if (Phwittr_Service::isEmailString($name)) { 
     147            $criteria->email = $name; 
     148            $criteria->password = $password; 
     149            $user = $mapper->findByEmailAndPassword($criteria); 
     150        } else { 
     151            $criteria->userName = $name; 
     152            $criteria->password = $password; 
     153            $user = $mapper->findByUserNameAndPassword($criteria); 
     154        } 
     155 
     156        if (!$user) { 
     157            return false; 
     158        } 
     159 
     160        return $user; 
     161    } 
     162 
    128163    /**#@-*/ 
    129164 
     
    131166     * @access private 
    132167     */ 
     168 
     169    // }}} 
     170    // {{{ _isEmailString 
     171 
     172    /** 
     173     * メールアドレス値かどうかを確認する 
     174     *  
     175     * @param string $email 
     176     * @return boolean 
     177     */ 
     178    public function isEmailString($email) 
     179    { 
     180        $validator = &new Piece_Right_Validator_Email(); 
     181        $validator->setRules(array('allowDotBeforeAtmark' => true)); 
     182 
     183        return $validator->validate($email); 
     184    } 
    133185 
    134186    /**#@-*/ 
  • events/phpframework/piece_framework/trunk/specs/Phwittr/AuthenticationSpec.php

    r12943 r13490  
    1212 
    1313require_once dirname(__FILE__) . '/SpecCommon.php'; 
    14 require_once 'Phwittr/Config.php'; 
    15 require_once 'Phwittr/Authentication.php'; 
     14require_once 'Phwittr/Service.php'; 
    1615 
    1716// {{{ DescribeSigninout 
     
    5251     */ 
    5352 
    54     public function it認証はユーザ名とパスワードで行() 
     53    public function it認証はユーザ名とパスワードで行い、成功するとユーザ情報が得られる() 
    5554    { 
    5655        $this->_createUserRecord($this->_foo); 
    5756 
    58         $authenticator = new Phwittr_Authentication(); 
    59         $result = $authenticator->authenticate($this->_foo->userName, 
    60                                                $this->_foo->password 
    61                                                ); 
     57        $user = Phwittr_Service::authenticate($this->_foo->userName, 
     58                                              $this->_foo->password 
     59                                              ); 
    6260 
    63         $this->spec($result)->should->beTrue(); 
     61        $this->spec($user->userName)->should->be($this->_foo->userName); 
     62        $this->spec($user->email)->should->be($this->_foo->email); 
    6463    } 
    6564 
    66     public function it認証はメールアドレスとパスワードでも行える() 
     65    public function it認証はメールアドレスとパスワードでも行え、成功するとユーザ情報が得られる() 
    6766    { 
    6867        $this->_createUserRecord($this->_foo); 
    6968 
    70         $authenticator = new Phwittr_Authentication(); 
    71         $result = $authenticator->authenticate($this->_foo->email, 
    72                                                $this->_foo->password 
    73                                                ); 
     69        $user = Phwittr_Service::authenticate($this->_foo->email, 
     70                                              $this->_foo->password 
     71                                              ); 
    7472 
    75         $this->spec($result)->should->beTrue(); 
     73        $this->spec($user->userName)->should->be($this->_foo->userName); 
     74        $this->spec($user->email)->should->be($this->_foo->email); 
    7675    } 
    7776 
     
    8079        $this->_createUserRecord($this->_foo); 
    8180 
    82         $authenticator = new Phwittr_Authentication(); 
    83  
    84         $this->spec($authenticator->authenticate('dummy', 'dummy'))->should->beFalse(); 
    85         $this->spec($authenticator->authenticate('dummy@example.com', 'dummy'))->should->beFalse(); 
    86         $this->spec($authenticator->authenticate($this->_foo->userName, 'dummy'))->should->beFalse(); 
    87         $this->spec($authenticator->authenticate($this->_foo->email, 'dummy'))->should->beFalse(); 
    88         $this->spec($authenticator->authenticate('dummy', $this->_foo->password))->should->beFalse(); 
    89         $this->spec($authenticator->authenticate('dummy@example.com', $this->_foo->password))->should->beFalse(); 
     81        $this->spec(Phwittr_Service::authenticate('dummy', 'dummy'))->should->beFalse(); 
     82        $this->spec(Phwittr_Service::authenticate('dummy@example.com', 'dummy'))->should->beFalse(); 
     83        $this->spec(Phwittr_Service::authenticate($this->_foo->userName, 'dummy'))->should->beFalse(); 
     84        $this->spec(Phwittr_Service::authenticate($this->_foo->email, 'dummy'))->should->beFalse(); 
     85        $this->spec(Phwittr_Service::authenticate('dummy', $this->_foo->password))->should->beFalse(); 
     86        $this->spec(Phwittr_Service::authenticate('dummy@example.com', $this->_foo->password))->should->beFalse(); 
    9087    } 
    9188 
     
    9693        $this->_foo->flag = 1; 
    9794 
    98         $authenticator = new Phwittr_Authentication(); 
    99         $result1 = $authenticator->authenticate($this->_foo->userName, 
    100                                                 $this->_foo->password 
    101                                                 ); 
    102         $result2 = $authenticator->authenticate($this->_foo->email, 
    103                                                 $this->_foo->password 
    104                                                 ); 
     95        $result1 = Phwittr_Service::authenticate($this->_foo->userName, 
     96                                                 $this->_foo->password 
     97                                                 ); 
     98        $result2 = Phwittr_Service::authenticate($this->_foo->email, 
     99                                                 $this->_foo->password 
     100                                                 ); 
    105101 
    106102        $this->spec($result1)->should->beFalse(); 
    107103        $this->spec($result2)->should->beFalse(); 
    108     } 
    109  
    110     public function it認証後にユーザ情報が取得できる() 
    111     { 
    112         $this->_createUserRecord($this->_foo); 
    113  
    114         $authenticator = new Phwittr_Authentication(); 
    115         $authenticator->authenticate($this->_foo->userName, 
    116                                      $this->_foo->password 
    117                                      ); 
    118         $user = $authenticator->getAuthenticatedUser(); 
    119  
    120         $this->spec($user->userName)->should->be($this->_foo->userName); 
    121         $this->spec($user->email)->should->be($this->_foo->email); 
    122     } 
    123  
    124     public function it認証前にユーザ情報を取得しても空() 
    125     { 
    126         $this->_createUserRecord($this->_foo); 
    127  
    128         $authenticator = new Phwittr_Authentication(); 
    129         $user = $authenticator->getAuthenticatedUser(); 
    130  
    131         $this->spec($user)->should->beNull(); 
    132     } 
    133  
    134     public function it認証に失敗した後にユーザ情報を取得しても空() 
    135     { 
    136         $this->_createUserRecord($this->_foo); 
    137  
    138         $authenticator = new Phwittr_Authentication(); 
    139         $this->spec($authenticator->authenticate('dummy', 'dummy'))->should->beFalse(); 
    140         $user = $authenticator->getAuthenticatedUser(); 
    141  
    142         $this->spec($user)->should->beNull(); 
    143104    } 
    144105