Добавлено запоминание регистрационного IP адреса

This commit is contained in:
ErickSkrauch 2016-08-18 02:55:52 +03:00
parent 433f560f48
commit 59f51451d0
5 changed files with 63 additions and 0 deletions

View File

@ -94,6 +94,7 @@ class RegistrationForm extends ApiForm {
$account->lang = $this->lang;
$account->status = Account::STATUS_REGISTERED;
$account->rules_agreement_version = LATEST_RULES_VERSION;
$account->setRegistrationIp(Yii::$app->request->getUserIP());
if (!$account->save()) {
throw new ErrorException('Account not created.');
}

View File

@ -22,6 +22,7 @@ use const common\LATEST_RULES_VERSION;
* @property string $lang
* @property integer $status
* @property integer $rules_agreement_version
* @property string $registration_ip
* @property integer $created_at
* @property integer $updated_at
* @property integer $password_changed_at
@ -198,4 +199,12 @@ class Account extends ActiveRecord {
return $this->rules_agreement_version === LATEST_RULES_VERSION;
}
public function setRegistrationIp($ip) {
$this->registration_ip = $ip === null ? null : inet_pton($ip);
}
public function getRegistrationIp() {
return $this->registration_ip === null ? null : inet_ntop($this->registration_ip);
}
}

View File

@ -0,0 +1,15 @@
<?php
use console\db\Migration;
class m160817_225019_registration_ip extends Migration {
public function safeUp() {
$this->addColumn('{{%accounts}}', 'registration_ip', 'VARBINARY(16) AFTER rules_agreement_version');
}
public function safeDown() {
$this->dropColumn('{{%accounts}}', 'registration_ip');
}
}

View File

@ -9,6 +9,7 @@ use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
use const common\LATEST_RULES_VERSION;
use yii\web\Request;
/**
* @property array $accounts
@ -23,6 +24,7 @@ class RegistrationFormTest extends DbTestCase {
$mailer->fileTransportCallback = function () {
return 'testing_message.eml';
};
$this->mockRequest();
}
protected function tearDown() {
@ -100,6 +102,7 @@ class RegistrationFormTest extends DbTestCase {
expect('user should be valid', $account)->isInstanceOf(Account::class);
expect('password should be correct', $account->validatePassword('some_password'))->true();
expect('uuid is set', $account->uuid)->notEmpty();
expect('registration_ip is set', $account->registration_ip)->notNull();
expect('actual rules version is set', $account->rules_agreement_version)->equals(LATEST_RULES_VERSION);
expect('user model exists in database', Account::find()->andWhere([
'username' => 'some_username',
@ -114,6 +117,21 @@ class RegistrationFormTest extends DbTestCase {
// TODO: там в самой форме есть метод sendMail(), который рано или поздно должен переехать. К нему нужны будут тоже тесты
private function mockRequest($ip = '88.225.20.236') {
$request = $this->getMockBuilder(Request::class)
->setMethods(['getUserIP'])
->getMock();
$request
->expects($this->any())
->method('getUserIP')
->will($this->returnValue($ip));
Yii::$app->set('request', $request);
return $request;
}
private function getMessageFile() {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;

View File

@ -202,4 +202,24 @@ class AccountTest extends DbTestCase {
});
}
public function testSetRegistrationIp() {
$account = new Account();
$account->setRegistrationIp('42.72.205.204');
$this->assertEquals('42.72.205.204', inet_ntop($account->registration_ip));
$account->setRegistrationIp('2001:1620:28:1:b6f:8bca:93:a116');
$this->assertEquals('2001:1620:28:1:b6f:8bca:93:a116', inet_ntop($account->registration_ip));
$account->setRegistrationIp(null);
$this->assertNull($account->registration_ip);
}
public function testGetRegistrationIp() {
$account = new Account();
$account->setRegistrationIp('42.72.205.204');
$this->assertEquals('42.72.205.204', $account->getRegistrationIp());
$account->setRegistrationIp('2001:1620:28:1:b6f:8bca:93:a116');
$this->assertEquals('2001:1620:28:1:b6f:8bca:93:a116', $account->getRegistrationIp());
$account->setRegistrationIp(null);
$this->assertNull($account->getRegistrationIp());
}
}