mirror of
https://github.com/elyby/accounts.git
synced 2024-12-26 15:10:22 +05:30
Добавлен экшен для получения инфы о текущем аутентифицированном пользователе
This commit is contained in:
parent
1fff80c7e8
commit
d9987a3185
44
api/controllers/UsersController.php
Normal file
44
api/controllers/UsersController.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
namespace api\controllers;
|
||||||
|
|
||||||
|
use common\models\Account;
|
||||||
|
use Yii;
|
||||||
|
use yii\filters\AccessControl;
|
||||||
|
use yii\helpers\ArrayHelper;
|
||||||
|
|
||||||
|
class UsersController extends Controller {
|
||||||
|
|
||||||
|
public function behaviors() {
|
||||||
|
return ArrayHelper::merge(parent::behaviors(), [
|
||||||
|
'access' => [
|
||||||
|
'class' => AccessControl::class,
|
||||||
|
'rules' => [
|
||||||
|
[
|
||||||
|
'actions' => ['current'],
|
||||||
|
'allow' => true,
|
||||||
|
'roles' => ['@'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verbs() {
|
||||||
|
return [
|
||||||
|
'current' => ['GET'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function actionCurrent() {
|
||||||
|
/** @var Account $account */
|
||||||
|
$account = Yii::$app->user->identity;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => $account->id,
|
||||||
|
'username' => $account->username,
|
||||||
|
'email' => $account->email,
|
||||||
|
'shouldChangePassword' => $account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
16
tests/codeception/api/_pages/UsersRoute.php
Normal file
16
tests/codeception/api/_pages/UsersRoute.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
namespace tests\codeception\api\_pages;
|
||||||
|
|
||||||
|
use yii\codeception\BasePage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property \tests\codeception\api\FunctionalTester $actor
|
||||||
|
*/
|
||||||
|
class UsersRoute extends BasePage {
|
||||||
|
|
||||||
|
public function current() {
|
||||||
|
$this->route = ['users/current'];
|
||||||
|
$this->actor->sendGET($this->getUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
tests/codeception/api/functional/UsersCest.php
Normal file
36
tests/codeception/api/functional/UsersCest.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
namespace tests\codeception\api\functional;
|
||||||
|
|
||||||
|
use Codeception\Scenario;
|
||||||
|
use Codeception\Specify;
|
||||||
|
use tests\codeception\api\_pages\UsersRoute;
|
||||||
|
use tests\codeception\api\functional\_steps\AccountSteps;
|
||||||
|
use tests\codeception\api\FunctionalTester;
|
||||||
|
|
||||||
|
class UsersCest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var UsersRoute
|
||||||
|
*/
|
||||||
|
private $route;
|
||||||
|
|
||||||
|
public function _before(FunctionalTester $I) {
|
||||||
|
$this->route = new UsersRoute($I);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCurrent(FunctionalTester $I, Scenario $scenario) {
|
||||||
|
$I = new AccountSteps($scenario);
|
||||||
|
$I->loggedInAsActiveAccount();
|
||||||
|
|
||||||
|
$this->route->current();
|
||||||
|
$I->canSeeResponseCodeIs(200);
|
||||||
|
$I->canSeeResponseIsJson();
|
||||||
|
$I->canSeeResponseContainsJson([
|
||||||
|
'id' => 1,
|
||||||
|
'username' => 'Admin',
|
||||||
|
'email' => 'admin@ely.by',
|
||||||
|
'shouldChangePassword' => false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user