Добавлен обработчик для удаления сессии при логауте

This commit is contained in:
ErickSkrauch 2016-07-17 19:38:04 +03:00
parent 2063d7daa0
commit ae3c3b88de
5 changed files with 128 additions and 0 deletions

View File

@ -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());

View 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;
}
}

View File

@ -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(), [

View 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,
]);
}
}

View File

@ -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',
];
}
}