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