mirror of
https://github.com/elyby/accounts.git
synced 2024-12-25 22:59:53 +05:30
Добавлен обработчик для удаления сессии при логауте
This commit is contained in:
parent
2063d7daa0
commit
ae3c3b88de
@ -3,6 +3,7 @@ namespace api\controllers;
|
||||
|
||||
use api\models\authentication\ForgotPasswordForm;
|
||||
use api\models\authentication\LoginForm;
|
||||
use api\models\authentication\LogoutForm;
|
||||
use api\models\authentication\RecoverPasswordForm;
|
||||
use api\models\authentication\RefreshTokenForm;
|
||||
use common\helpers\Error as E;
|
||||
@ -26,6 +27,11 @@ class AuthenticationController extends Controller {
|
||||
'allow' => true,
|
||||
'roles' => ['?'],
|
||||
],
|
||||
[
|
||||
'actions' => ['logout'],
|
||||
'allow' => true,
|
||||
'roles' => ['@'],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
@ -34,6 +40,7 @@ class AuthenticationController extends Controller {
|
||||
public function verbs() {
|
||||
return [
|
||||
'login' => ['POST'],
|
||||
'logout' => ['POST'],
|
||||
'forgot-password' => ['POST'],
|
||||
'recover-password' => ['POST'],
|
||||
'refresh-token' => ['POST'],
|
||||
@ -61,6 +68,15 @@ class AuthenticationController extends Controller {
|
||||
], $result->getAsResponse());
|
||||
}
|
||||
|
||||
public function actionLogout() {
|
||||
$form = new LogoutForm();
|
||||
$form->logout();
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function actionForgotPassword() {
|
||||
$model = new ForgotPasswordForm();
|
||||
$model->load(Yii::$app->request->post());
|
||||
|
20
api/models/authentication/LogoutForm.php
Normal file
20
api/models/authentication/LogoutForm.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
|
||||
class LogoutForm extends ApiForm {
|
||||
|
||||
public function logout() : bool {
|
||||
$component = \Yii::$app->user;
|
||||
$session = $component->getActiveSession();
|
||||
if ($session === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$session->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -22,6 +22,11 @@ class AuthenticationRoute extends BasePage {
|
||||
$this->actor->sendPOST($this->getUrl(), $params);
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
$this->route = ['authentication/logout'];
|
||||
$this->actor->sendPOST($this->getUrl());
|
||||
}
|
||||
|
||||
public function forgotPassword($login = '') {
|
||||
$this->route = ['authentication/forgot-password'];
|
||||
$this->actor->sendPOST($this->getUrl(), [
|
||||
|
18
tests/codeception/api/functional/LogoutCest.php
Normal file
18
tests/codeception/api/functional/LogoutCest.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace tests\codeception\api;
|
||||
|
||||
use tests\codeception\api\_pages\AuthenticationRoute;
|
||||
|
||||
class LogoutCest {
|
||||
|
||||
public function testLoginEmailOrUsername(FunctionalTester $I) {
|
||||
$route = new AuthenticationRoute($I);
|
||||
|
||||
$I->loggedInAsActiveAccount();
|
||||
$route->logout();
|
||||
$I->canSeeResponseContainsJson([
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\models\authentication;
|
||||
|
||||
use api\components\User\Component;
|
||||
use api\models\AccountIdentity;
|
||||
use api\models\authentication\LogoutForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\AccountSession;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use Yii;
|
||||
|
||||
class LogoutFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public function testValidateLogout() {
|
||||
$this->specify('No actions if active session is not exists', function () {
|
||||
$userComp = $this
|
||||
->getMockBuilder(Component::class)
|
||||
->setConstructorArgs([$this->getComponentArgs()])
|
||||
->setMethods(['getActiveSession'])
|
||||
->getMock();
|
||||
$userComp
|
||||
->expects($this->any())
|
||||
->method('getActiveSession')
|
||||
->will($this->returnValue(null));
|
||||
|
||||
Yii::$app->set('user', $userComp);
|
||||
|
||||
$model = new LogoutForm();
|
||||
expect($model->logout())->true();
|
||||
});
|
||||
|
||||
$this->specify('if active session is presented, then delete should be called', function () {
|
||||
$session = $this
|
||||
->getMockBuilder(AccountSession::class)
|
||||
->setMethods(['delete'])
|
||||
->getMock();
|
||||
$session
|
||||
->expects($this->once())
|
||||
->method('delete')
|
||||
->willReturn(true);
|
||||
|
||||
$userComp = $this
|
||||
->getMockBuilder(Component::class)
|
||||
->setConstructorArgs([$this->getComponentArgs()])
|
||||
->setMethods(['getActiveSession'])
|
||||
->getMock();
|
||||
$userComp
|
||||
->expects($this->any())
|
||||
->method('getActiveSession')
|
||||
->will($this->returnValue($session));
|
||||
|
||||
Yii::$app->set('user', $userComp);
|
||||
|
||||
$model = new LogoutForm();
|
||||
$model->logout();
|
||||
});
|
||||
}
|
||||
|
||||
private function getComponentArgs() {
|
||||
return [
|
||||
'identityClass' => AccountIdentity::class,
|
||||
'enableSession' => false,
|
||||
'loginUrl' => null,
|
||||
'secret' => 'secret',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user