Добавлено поле username в модель Account, скорретирована форма входа и её тесты

This commit is contained in:
ErickSkrauch 2016-01-04 18:31:14 +03:00
parent c6a6f35be6
commit 45c31dfbbe
5 changed files with 76 additions and 35 deletions

View File

@ -7,17 +7,16 @@ use yii\base\Model;
class AuthenticationForm extends Model { class AuthenticationForm extends Model {
public $email; public $login;
public $password; public $password;
public $rememberMe = true; public $rememberMe = true;
private $_user; private $_account;
public function rules() { public function rules() {
return [ return [
['email', 'required', 'message' => 'error.email_required'], ['login', 'required', 'message' => 'error.login_required'],
['email', 'email', 'message' => 'error.email_invalid'], ['login', 'validateLogin'],
['email', 'exist', 'targetClass' => Account::class, 'skipOnError' => true, 'message' => 'error.email_not_exist'],
['password', 'required', 'when' => function(self $model) { ['password', 'required', 'when' => function(self $model) {
return !$model->hasErrors(); return !$model->hasErrors();
@ -28,11 +27,19 @@ class AuthenticationForm extends Model {
]; ];
} }
public function validateLogin($attribute) {
if (!$this->hasErrors()) {
if (!$this->getAccount()) {
$this->addError($attribute, 'error.' . $attribute . '_not_exist');
}
}
}
public function validatePassword($attribute) { public function validatePassword($attribute) {
if (!$this->hasErrors()) { if (!$this->hasErrors()) {
$account = $this->getAccount(); $account = $this->getAccount();
if (!$account || !$account->validatePassword($this->password)) { if (!$account || !$account->validatePassword($this->password)) {
$this->addError($attribute, 'error.password_incorrect'); $this->addError($attribute, 'error.' . $attribute . '_incorrect');
} }
} }
} }
@ -54,11 +61,12 @@ class AuthenticationForm extends Model {
* @return Account|null * @return Account|null
*/ */
protected function getAccount() { protected function getAccount() {
if ($this->_user === NULL) { if ($this->_account === NULL) {
$this->_user = Account::findByEmail($this->email); $attribute = strpos($this->login, '@') ? 'email' : 'username';
$this->_account = Account::findOne([$attribute => $this->login]);
} }
return $this->_user; return $this->_account;
} }
} }

View File

@ -13,10 +13,11 @@ use yii\web\IdentityInterface;
* Поля модели: * Поля модели:
* @property integer $id * @property integer $id
* @property string $uuid * @property string $uuid
* @property string $username
* @property string $email
* @property string $password_hash * @property string $password_hash
* @property integer $password_hash_strategy * @property integer $password_hash_strategy
* @property string $password_reset_token * @property string $password_reset_token
* @property string $email
* @property string $auth_key * @property string $auth_key
* @property integer $status * @property integer $status
* @property integer $created_at * @property integer $created_at

View File

@ -0,0 +1,15 @@
<?php
use console\db\Migration;
class m160104_150157_account_extended_info extends Migration {
public function safeUp() {
$this->addColumn('{{%accounts}}', 'username', $this->string()->unique()->notNull() . ' AFTER `uuid`');
}
public function safeDown() {
$this->dropColumn('{{%accounts}}', 'username');
}
}

View File

@ -3,10 +3,11 @@ return [
[ [
'id' => 1, 'id' => 1,
'uuid' => 'df936908-b2e1-544d-96f8-2977ec213022', 'uuid' => 'df936908-b2e1-544d-96f8-2977ec213022',
'username' => 'Admin',
'email' => 'admin@ely.by',
'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', # password_0 'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', # password_0
'password_hash_strategy' => 1, 'password_hash_strategy' => 1,
'password_reset_token' => NULL, 'password_reset_token' => NULL,
'email' => 'admin@ely.by',
'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv', 'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv',
'status' => 10, 'status' => 10,
'created_at' => 1451775316, 'created_at' => 1451775316,

View File

@ -25,60 +25,60 @@ class AuthenticationFormTest extends DbTestCase {
]; ];
} }
protected function createModel($email = '', $password = '') { protected function createModel($login = '', $password = '') {
return new AuthenticationForm([ return new AuthenticationForm([
'email' => $email, 'login' => $login,
'password' => $password, 'password' => $password,
]); ]);
} }
public function testLoginEmail() { public function testLoginEmailOrUsername() {
$model = $this->createModel(); $model = $this->createModel();
$this->specify('error.email_required expected if email is not set', function() use ($model) { $this->specify('error.login_required expected if login is not set', function() use ($model) {
expect($model->login())->false(); expect($model->login())->false();
expect($model->getErrors('email'))->equals(['error.email_required']); expect($model->getErrors('login'))->equals(['error.login_required']);
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
}); });
$model = $this->createModel('wrong-email-string'); $model = $this->createModel('non-exist-username');
$this->specify('error.email_invalid expected if email not correct', function() use ($model) { $this->specify('error.login_not_exist expected if username not exists in database', function() use ($model) {
expect($model->login())->false(); expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_invalid']); expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$model = $this->createModel('wrong@email');
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_invalid']);
}); });
$model = $this->createModel('not-exist@user.com'); $model = $this->createModel('not-exist@user.com');
$this->specify('error.email_not_exist expected if email not exists in database', function() use ($model) { $this->specify('error.login_not_exist expected if email not exists in database', function() use ($model) {
expect($model->login())->false(); expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_not_exist']); expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$model = $this->createModel('Admin');
$this->specify('no errors on login field if username is correct and exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('login'))->isEmpty();
}); });
$model = $this->createModel('admin@ely.by'); $model = $this->createModel('admin@ely.by');
$this->specify('no errors on email field if email is correct and exists in database', function() use ($model) { $this->specify('no errors on login field if email is correct and exists in database', function() use ($model) {
expect($model->login())->false(); expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->isEmpty(); expect($model->getErrors('login'))->isEmpty();
}); });
} }
public function testLoginPassword() { public function testLoginPassword() {
$model = $this->createModel(); $model = $this->createModel();
$this->specify('password don\'t has errors if email not set', function() use ($model) { $this->specify('password don\'t has errors if email or username not set', function() use ($model) {
expect($model->login())->false(); expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty(); expect($model->getErrors('password'))->isEmpty();
}); });
$model = $this->createModel('wrong-email-string', 'random-password'); $model = $this->createModel('non-exist-username', 'random-password');
$this->specify('password don\'t has errors if email invalid', function() use ($model) { $this->specify('password don\'t has errors if username not exists in database', function() use ($model) {
expect($model->login())->false(); expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty(); expect($model->getErrors('password'))->isEmpty();
@ -97,11 +97,27 @@ class AuthenticationFormTest extends DbTestCase {
expect(Yii::$app->user->isGuest)->true(); expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']); expect($model->getErrors('password'))->equals(['error.password_incorrect']);
}); });
$model = $this->createModel('Admin', 'wrong-password');
$this->specify('error.password_incorrect expected if username correct, but password wrong', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
} }
public function testLoginCorrect() { public function testLoginByUsernameCorrect() {
$model = $this->createModel('Admin', 'password_0');
$this->specify('user should be able to login with correct username and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
public function testLoginByEmailCorrect() {
$model = $this->createModel('admin@ely.by', 'password_0'); $model = $this->createModel('admin@ely.by', 'password_0');
$this->specify('user should be able to login with correct credentials', function () use ($model) { $this->specify('user should be able to login with correct email and password', function () use ($model) {
expect('model should login user', $model->login())->true(); expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty(); expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false(); expect('user should be logged in', Yii::$app->user->isGuest)->false();