Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* @link http://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license http://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yii\filters; | ||
|
||
use Yii; | ||
use yii\base\ActionFilter; | ||
use yii\web\BadRequestHttpException; | ||
use yii\web\Request; | ||
|
||
/** | ||
* AjaxFilter allow to limit access only for ajax requests. | ||
* | ||
* ```php | ||
* public function behaviors() | ||
* { | ||
* return [ | ||
* [ | ||
* 'class' => 'yii\filters\AjaxFilter', | ||
* 'only' => ['index'] | ||
* ], | ||
* ]; | ||
* } | ||
* ``` | ||
* | ||
* @author Dmitry Dorogin <dmirogin@ya.ru> | ||
* @since 2.0.13 | ||
*/ | ||
class AjaxFilter extends ActionFilter | ||
{ | ||
/** | ||
* @var string the message to be displayed when request isn't ajax | ||
*/ | ||
public $errorMessage = 'Request must be XMLHttpRequest.'; | ||
/** | ||
* @var Request the current request. If not set, the `request` application component will be used. | ||
*/ | ||
public $request; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
if ($this->request === null) { | ||
$this->request = Yii::$app->getRequest(); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function beforeAction($action) | ||
{ | ||
if ($this->request->getIsAjax()) { | ||
return true; | ||
} | ||
|
||
throw new BadRequestHttpException($this->errorMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* @link http://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license http://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yiiunit\framework\filters; | ||
|
||
use Yii; | ||
use yii\base\Action; | ||
use yii\filters\AjaxFilter; | ||
use yii\web\BadRequestHttpException; | ||
use yii\web\Controller; | ||
use yii\web\Request; | ||
use yiiunit\TestCase; | ||
|
||
/** | ||
* @group filters | ||
*/ | ||
class AjaxFilterTest extends TestCase | ||
{ | ||
/** | ||
* @param boolean $isAjax | ||
* @return Request | ||
*/ | ||
protected function mockRequest($isAjax) | ||
{ | ||
/** @var Request $request */ | ||
$request = $this->getMockBuilder('\yii\web\Request') | ||
->setMethods(['getIsAjax']) | ||
->getMock(); | ||
$request->method('getIsAjax')->willReturn($isAjax); | ||
|
||
return $request; | ||
} | ||
|
||
public function testFilter() | ||
{ | ||
$this->mockWebApplication(); | ||
$controller = new Controller('id', Yii::$app); | ||
$action = new Action('test', $controller); | ||
$filter = new AjaxFilter(); | ||
|
||
$filter->request = $this->mockRequest(true); | ||
$this->assertTrue($filter->beforeAction($action)); | ||
|
||
$filter->request = $this->mockRequest(false); | ||
$this->expectException('yii\web\BadRequestHttpException'); | ||
$filter->beforeAction($action); | ||
} | ||
} |