mirror of
https://github.com/elyby/accounts.git
synced 2024-12-02 11:41:05 +05:30
Форма смены ника теперь принимает аккаунт через конструктор
This commit is contained in:
parent
cb068b9dc0
commit
1169097adb
@ -90,7 +90,8 @@ class AccountsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function actionChangeUsername() {
|
public function actionChangeUsername() {
|
||||||
$model = new ChangeUsernameForm();
|
$account = Yii::$app->user->identity;
|
||||||
|
$model = new ChangeUsernameForm($account);
|
||||||
$model->load(Yii::$app->request->post());
|
$model->load(Yii::$app->request->post());
|
||||||
if (!$model->change()) {
|
if (!$model->change()) {
|
||||||
return [
|
return [
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace api\models\profile;
|
namespace api\models\profile;
|
||||||
|
|
||||||
use api\models\AccountIdentity;
|
|
||||||
use api\models\base\ApiForm;
|
use api\models\base\ApiForm;
|
||||||
use api\validators\PasswordRequiredValidator;
|
use api\validators\PasswordRequiredValidator;
|
||||||
use common\helpers\Amqp;
|
use common\helpers\Amqp;
|
||||||
|
use common\models\Account;
|
||||||
use common\models\amqp\UsernameChanged;
|
use common\models\amqp\UsernameChanged;
|
||||||
use common\models\UsernameHistory;
|
use common\models\UsernameHistory;
|
||||||
use common\validators\UsernameValidator;
|
use common\validators\UsernameValidator;
|
||||||
@ -19,21 +19,31 @@ class ChangeUsernameForm extends ApiForm {
|
|||||||
|
|
||||||
public $password;
|
public $password;
|
||||||
|
|
||||||
public function rules() {
|
/**
|
||||||
|
* @var Account
|
||||||
|
*/
|
||||||
|
private $account;
|
||||||
|
|
||||||
|
public function __construct(Account $account, array $config = []) {
|
||||||
|
parent::__construct($config);
|
||||||
|
$this->account = $account;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array {
|
||||||
return [
|
return [
|
||||||
['username', UsernameValidator::class, 'accountCallback' => function() {
|
['username', UsernameValidator::class, 'accountCallback' => function() {
|
||||||
return $this->getAccount()->id;
|
return $this->account->id;
|
||||||
}],
|
}],
|
||||||
['password', PasswordRequiredValidator::class],
|
['password', PasswordRequiredValidator::class],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function change() : bool {
|
public function change(): bool {
|
||||||
if (!$this->validate()) {
|
if (!$this->validate()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$account = $this->getAccount();
|
$account = $this->account;
|
||||||
if ($this->username === $account->username) {
|
if ($this->username === $account->username) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -85,8 +95,4 @@ class ChangeUsernameForm extends ApiForm {
|
|||||||
Amqp::sendToEventsExchange('accounts.username-changed', $message);
|
Amqp::sendToEventsExchange('accounts.username-changed', $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAccount() : AccountIdentity {
|
|
||||||
return Yii::$app->user->identity;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,12 +23,11 @@ class ChangeUsernameFormTest extends TestCase {
|
|||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$account = AccountIdentity::findOne($this->getAccountId());
|
Yii::$app->user->setIdentity($this->getAccount());
|
||||||
Yii::$app->user->setIdentity($account);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChange() {
|
public function testChange() {
|
||||||
$model = new ChangeUsernameForm([
|
$model = new ChangeUsernameForm($this->getAccount(), [
|
||||||
'password' => 'password_0',
|
'password' => 'password_0',
|
||||||
'username' => 'my_new_nickname',
|
'username' => 'my_new_nickname',
|
||||||
]);
|
]);
|
||||||
@ -39,8 +38,9 @@ class ChangeUsernameFormTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testChangeWithoutChange() {
|
public function testChangeWithoutChange() {
|
||||||
$username = $this->tester->grabFixture('accounts', 'admin')['username'];
|
$account = $this->getAccount();
|
||||||
$model = new ChangeUsernameForm([
|
$username = $account->username;
|
||||||
|
$model = new ChangeUsernameForm($account, [
|
||||||
'password' => 'password_0',
|
'password' => 'password_0',
|
||||||
'username' => $username,
|
'username' => $username,
|
||||||
]);
|
]);
|
||||||
@ -56,7 +56,7 @@ class ChangeUsernameFormTest extends TestCase {
|
|||||||
|
|
||||||
public function testChangeCase() {
|
public function testChangeCase() {
|
||||||
$newUsername = mb_strtoupper($this->tester->grabFixture('accounts', 'admin')['username']);
|
$newUsername = mb_strtoupper($this->tester->grabFixture('accounts', 'admin')['username']);
|
||||||
$model = new ChangeUsernameForm([
|
$model = new ChangeUsernameForm($this->getAccount(), [
|
||||||
'password' => 'password_0',
|
'password' => 'password_0',
|
||||||
'username' => $newUsername,
|
'username' => $newUsername,
|
||||||
]);
|
]);
|
||||||
@ -71,7 +71,7 @@ class ChangeUsernameFormTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateTask() {
|
public function testCreateTask() {
|
||||||
$model = new ChangeUsernameForm();
|
$model = new ChangeUsernameForm($this->getAccount());
|
||||||
$model->createEventTask(1, 'test1', 'test');
|
$model->createEventTask(1, 'test1', 'test');
|
||||||
$message = $this->tester->grabLastSentAmqpMessage('events');
|
$message = $this->tester->grabLastSentAmqpMessage('events');
|
||||||
$body = json_decode($message->getBody(), true);
|
$body = json_decode($message->getBody(), true);
|
||||||
@ -80,8 +80,12 @@ class ChangeUsernameFormTest extends TestCase {
|
|||||||
$this->assertEquals('test', $body['oldUsername']);
|
$this->assertEquals('test', $body['oldUsername']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getAccount(): AccountIdentity {
|
||||||
|
return AccountIdentity::findOne($this->getAccountId());
|
||||||
|
}
|
||||||
|
|
||||||
private function getAccountId() {
|
private function getAccountId() {
|
||||||
return $this->tester->grabFixture('accounts', 'admin')['id'];
|
return $this->tester->grabFixture('accounts', 'admin')->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user