Merge branch 'profile'

This commit is contained in:
SleepWalker 2016-03-21 20:28:18 +02:00
commit d8a2cc21b8
6 changed files with 31 additions and 13 deletions

View File

@ -38,10 +38,12 @@ class AccountsController extends Controller {
return [ return [
'id' => $account->id, 'id' => $account->id,
'uuid' => $account->uuid,
'username' => $account->username, 'username' => $account->username,
'email' => $account->email, 'email' => $account->email,
'shouldChangePassword' => $account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY, 'shouldChangePassword' => $account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY,
'isActive' => $account->status === Account::STATUS_ACTIVE, 'isActive' => $account->status === Account::STATUS_ACTIVE,
'passwordChangedAt' => $account->password_changed_at,
]; ];
} }

View File

@ -73,7 +73,6 @@ class RegistrationForm extends ApiForm {
$account->username = $this->username; $account->username = $this->username;
$account->password = $this->password; $account->password = $this->password;
$account->status = Account::STATUS_REGISTERED; $account->status = Account::STATUS_REGISTERED;
$account->generateAuthKey();
if (!$account->save()) { if (!$account->save()) {
throw new ErrorException('Account not created.'); throw new ErrorException('Account not created.');
} }

View File

@ -19,10 +19,10 @@ use yii\web\IdentityInterface;
* @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 $auth_key
* @property integer $status * @property integer $status
* @property integer $created_at * @property integer $created_at
* @property integer $updated_at * @property integer $updated_at
* @property integer $password_changed_at
* *
* Геттеры-сеттеры: * Геттеры-сеттеры:
* @property string $password пароль пользователя (только для записи) * @property string $password пароль пользователя (только для записи)
@ -133,7 +133,7 @@ class Account extends ActiveRecord implements IdentityInterface {
* @inheritdoc * @inheritdoc
*/ */
public function getAuthKey() { public function getAuthKey() {
return $this->auth_key; throw new NotSupportedException('This method used for cookie auth, except we using JWT tokens');
} }
/** /**
@ -177,13 +177,7 @@ class Account extends ActiveRecord implements IdentityInterface {
public function setPassword($password) { public function setPassword($password) {
$this->password_hash_strategy = self::PASS_HASH_STRATEGY_YII2; $this->password_hash_strategy = self::PASS_HASH_STRATEGY_YII2;
$this->password_hash = Yii::$app->security->generatePasswordHash($password); $this->password_hash = Yii::$app->security->generatePasswordHash($password);
} $this->password_changed_at = time();
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey() {
$this->auth_key = Yii::$app->security->generateRandomString();
} }
/** /**

View File

@ -0,0 +1,21 @@
<?php
use console\db\Migration;
class m160311_211107_password_change_time extends Migration {
public function safeUp() {
$this->addColumn('{{%accounts}}', 'password_changed_at', $this->integer()->notNull());
$this->getDb()->createCommand('
UPDATE {{%accounts}}
SET password_changed_at = created_at
')->execute();
$this->dropColumn('{{%accounts}}', 'auth_key');
}
public function safeDown() {
$this->dropColumn('{{%accounts}}', 'password_changed_at');
$this->addColumn('{{%accounts}}', 'auth_key', $this->string(32)->notNull() . ' AFTER `status`');
}
}

View File

@ -60,6 +60,7 @@ class ChangePasswordFormTest extends DbTestCase {
$this->specify('successfully change password with modern hash strategy', function() use ($model, $account) { $this->specify('successfully change password with modern hash strategy', function() use ($model, $account) {
expect('form should return true', $model->changePassword())->true(); expect('form should return true', $model->changePassword())->true();
expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true(); expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true();
expect('password change time updated', $account->password_changed_at)->greaterOrEquals(time());
}); });
/** @var Account $account */ /** @var Account $account */
@ -72,6 +73,7 @@ class ChangePasswordFormTest extends DbTestCase {
$this->specify('successfully change password with legacy hash strategy', function() use ($model, $account) { $this->specify('successfully change password with legacy hash strategy', function() use ($model, $account) {
expect('form should return true', $model->changePassword())->true(); expect('form should return true', $model->changePassword())->true();
expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true(); expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true();
expect('password change time updated', $account->password_changed_at)->greaterOrEquals(time());
}); });
} }

View File

@ -8,10 +8,10 @@ return [
'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', # password_0 'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2, 'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'password_reset_token' => null, 'password_reset_token' => null,
'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv',
'status' => \common\models\Account::STATUS_ACTIVE, 'status' => \common\models\Account::STATUS_ACTIVE,
'created_at' => 1451775316, 'created_at' => 1451775316,
'updated_at' => 1451775316, 'updated_at' => 1451775316,
'password_changed_at' => 1451775316,
], ],
'user-with-old-password-type' => [ 'user-with-old-password-type' => [
'id' => 2, 'id' => 2,
@ -21,10 +21,10 @@ return [
'password_hash' => '133c00c463cbd3e491c28cb653ce4718', # 12345678 'password_hash' => '133c00c463cbd3e491c28cb653ce4718', # 12345678
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_OLD_ELY, 'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_OLD_ELY,
'password_reset_token' => null, 'password_reset_token' => null,
'auth_key' => 'ltTNae9t34OmnK6l4vT4IeaTk-YWI2Rv',
'status' => \common\models\Account::STATUS_ACTIVE, 'status' => \common\models\Account::STATUS_ACTIVE,
'created_at' => 1385225069, 'created_at' => 1385225069,
'updated_at' => 1385225069, 'updated_at' => 1385225069,
'password_changed_at' => 1385225069,
], ],
'not-activated-account' => [ 'not-activated-account' => [
'id' => 3, 'id' => 3,
@ -34,10 +34,10 @@ return [
'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0 'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2, 'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'password_reset_token' => null, 'password_reset_token' => null,
'auth_key' => '3AGc12Q7U8lU9umIyCWk5iCnpdPvZ8Up',
'status' => \common\models\Account::STATUS_REGISTERED, 'status' => \common\models\Account::STATUS_REGISTERED,
'created_at' => 1453146616, 'created_at' => 1453146616,
'updated_at' => 1453146616, 'updated_at' => 1453146616,
'password_changed_at' => 1453146616,
], ],
'not-activated-account-with-expired-message' => [ 'not-activated-account-with-expired-message' => [
'id' => 4, 'id' => 4,