Changeset 13490
- Timestamp:
- 06/08/08 17:59:58 (5 years ago)
- Location:
- events/phpframework/piece_framework/trunk
- Files:
-
- 2 modified
-
libs/Phwittr/Service.php (modified) (4 diffs)
-
specs/Phwittr/AuthenticationSpec.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
events/phpframework/piece_framework/trunk/libs/Phwittr/Service.php
r13488 r13490 13 13 require_once 'Phwittr/Config.php'; 14 14 require_once 'Phwittr/Service/Exception.php'; 15 require_once 'Piece/Right/Validator/Email.php'; 15 16 16 17 // {{{ Phwittr … … 117 118 public function isAvailableUserName($userName) 118 119 { 119 $mapper = Piece_ORM::getMapper('Users'); 120 Phwittr_Config::configurePieceORM(); 121 $mapper = Piece_ORM::getMapper('Users'); 122 120 123 $user = $mapper->findByUserName($userName); 121 124 if (is_null($user)) { … … 126 129 } 127 130 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 128 163 /**#@-*/ 129 164 … … 131 166 * @access private 132 167 */ 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 } 133 185 134 186 /**#@-*/ -
events/phpframework/piece_framework/trunk/specs/Phwittr/AuthenticationSpec.php
r12943 r13490 12 12 13 13 require_once dirname(__FILE__) . '/SpecCommon.php'; 14 require_once 'Phwittr/Config.php'; 15 require_once 'Phwittr/Authentication.php'; 14 require_once 'Phwittr/Service.php'; 16 15 17 16 // {{{ DescribeSigninout … … 52 51 */ 53 52 54 public function it認証はユーザ名とパスワードで行 う()53 public function it認証はユーザ名とパスワードで行い、成功するとユーザ情報が得られる() 55 54 { 56 55 $this->_createUserRecord($this->_foo); 57 56 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 ); 62 60 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); 64 63 } 65 64 66 public function it認証はメールアドレスとパスワードでも行え る()65 public function it認証はメールアドレスとパスワードでも行え、成功するとユーザ情報が得られる() 67 66 { 68 67 $this->_createUserRecord($this->_foo); 69 68 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 ); 74 72 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); 76 75 } 77 76 … … 80 79 $this->_createUserRecord($this->_foo); 81 80 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(); 90 87 } 91 88 … … 96 93 $this->_foo->flag = 1; 97 94 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 ); 105 101 106 102 $this->spec($result1)->should->beFalse(); 107 103 $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->password117 );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();143 104 } 144 105
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)