Skip to content

Commit 2cbe8b4

Browse files
authored
Merge pull request #737 from cakephp/port-734
DebugKitController beforeFilter check if debug is enabled
2 parents b8ee5de + f03cc17 commit 2cbe8b4

File tree

12 files changed

+174
-93
lines changed

12 files changed

+174
-93
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"require-dev": {
3434
"cakephp/cakephp-codesniffer": "^4.0",
35+
"cakephp/authorization": "^2.0",
3536
"phpunit/phpunit": "^8.0"
3637
},
3738
"autoload": {

docs/en/index.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ Configuration
4444
// Allow e.g. http://foo.bar.dev or http://my-shop.local domains locally
4545
Configure::write('DebugKit.safeTld', ['dev', 'local', 'example']);
4646

47-
* ``DebugKit.forceEnable`` - Force DebugKit to display. Careful with this, it is usually
47+
* ``DebugKit.forceEnable`` - Force DebugKit to display. Careful with this, it is usually
4848
safer to simply whitelist your local TLDs. Example usage::
4949

5050
// Before loading DebugKit
5151
Configure::write('DebugKit.forceEnable', true);
5252

53+
* ``DebugKit.ignoreAuthorization`` - Set to true to ignore Cake Authorization plugin for DebugKit requests. Disabled by default.
54+
5355
Database Configuration
5456
----------------------
5557

@@ -77,7 +79,7 @@ connection in the ``Datasources`` variable in your **config/app.php** file. For
7779
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
7880
],
7981

80-
You can safely remove the **tmp/debug_kit.sqlite** file at any point.
82+
You can safely remove the **tmp/debug_kit.sqlite** file at any point.
8183
DebugKit will regenerate it when necessary.
8284

8385
Toolbar Usage

docs/fr/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Ensuite, vous devez activer le plugin en exécutant la ligne suivante::
3030

3131
bin/cake plugin load DebugKit
3232

33+
Configuration
34+
=============
35+
36+
* ``DebugKit.ignoreAuthorization`` - Définie à true pour ignorer le plugin Cake Authorization uniquement pour les requêtes DebugKit. Par défaut à false.
37+
3338
Stockage de DebugKit
3439
====================
3540

src/Controller/ComposerController.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
namespace DebugKit\Controller;
1717

18-
use Cake\Controller\Controller;
19-
use Cake\Core\Configure;
20-
use Cake\Event\EventInterface;
21-
use Cake\Http\Exception\NotFoundException;
2218
use Cake\View\JsonView;
2319
use Composer\Console\Application;
2420
use Symfony\Component\Console\Input\ArrayInput;
@@ -27,7 +23,7 @@
2723
/**
2824
* Provides utility features need by the toolbar.
2925
*/
30-
class ComposerController extends Controller
26+
class ComposerController extends DebugKitController
3127
{
3228
/**
3329
* {@inheritDoc}
@@ -39,20 +35,6 @@ public function initialize(): void
3935
$this->viewBuilder()->setClassName(JsonView::class);
4036
}
4137

42-
/**
43-
* Before filter handler.
44-
*
45-
* @param \Cake\Event\EventInterface $event The event.
46-
* @return void
47-
* @throws \Cake\Http\Exception\NotFoundException
48-
*/
49-
public function beforeFilter(EventInterface $event)
50-
{
51-
if (!Configure::read('debug')) {
52-
throw new NotFoundException();
53-
}
54-
}
55-
5638
/**
5739
* Check outdated composer dependencies
5840
*

src/Controller/DashboardController.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,24 @@
1414
*/
1515
namespace DebugKit\Controller;
1616

17-
use Cake\Controller\Controller;
18-
use Cake\Core\Configure;
1917
use Cake\Event\EventInterface;
20-
use Cake\Http\Exception\NotFoundException;
2118

2219
/**
2320
* Dashboard and common DebugKit backend.
2421
*
2522
* @property \DebugKit\Model\Table\RequestsTable $Requests
2623
*/
27-
class DashboardController extends Controller
24+
class DashboardController extends DebugKitController
2825
{
2926
/**
3027
* Before filter handler.
3128
*
3229
* @param \Cake\Event\EventInterface $event The event.
3330
* @return void
34-
* @throws \Cake\Http\Exception\NotFoundException
3531
*/
3632
public function beforeFilter(EventInterface $event)
3733
{
38-
// TODO add config override.
39-
if (!Configure::read('debug')) {
40-
throw new NotFoundException('Not available without debug mode on.');
41-
}
34+
parent::beforeFilter($event);
4235

4336
$this->viewBuilder()->setLayout('dashboard');
4437
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12+
* @link http://cakephp.org CakePHP(tm) Project
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace DebugKit\Controller;
16+
17+
use Cake\Controller\Controller;
18+
use Cake\Core\Configure;
19+
use Cake\Event\EventInterface;
20+
use Cake\Http\Exception\NotFoundException;
21+
use Cake\Log\Log;
22+
23+
/**
24+
* DebugKit Controller.
25+
*/
26+
class DebugKitController extends Controller
27+
{
28+
/**
29+
* Before filter handler.
30+
*
31+
* @param \Cake\Event\EventInterface $event The event.
32+
* @return void
33+
* @throws \Cake\Http\Exception\NotFoundException
34+
*/
35+
public function beforeFilter(EventInterface $event)
36+
{
37+
if (!Configure::read('debug')) {
38+
throw new NotFoundException('Not available without debug mode on.');
39+
}
40+
41+
// If CakePHP Authorization\Authorization plugin is enabled,
42+
// ignore it, only if `DebugKit.ignoreAuthorization` is set to true
43+
$authorizationService = $this->getRequest()->getAttribute('authorization');
44+
if ($authorizationService instanceof \Authorization\AuthorizationService) {
45+
if (Configure::read('DebugKit.ignoreAuthorization')) {
46+
$authorizationService->skipAuthorization();
47+
} else {
48+
Log::info(
49+
"Cake Authorization plugin is enabled. If you would like " .
50+
"to force DebugKit to ignore it, set `DebugKit.ignoreAuthorization` " .
51+
" Configure option to true."
52+
);
53+
}
54+
}
55+
}
56+
}

src/Controller/MailPreviewController.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
namespace DebugKit\Controller;
1717

18-
use Cake\Controller\Controller;
1918
use Cake\Core\App;
20-
use Cake\Core\Configure;
2119
use Cake\Core\Plugin as CorePlugin;
2220
use Cake\Event\EventInterface;
2321
use Cake\Http\Exception\NotFoundException;
@@ -32,22 +30,8 @@
3230
*
3331
* @property \DebugKit\Model\Table\PanelsTable $Panels
3432
*/
35-
class MailPreviewController extends Controller
33+
class MailPreviewController extends DebugKitController
3634
{
37-
/**
38-
* Before filter callback.
39-
*
40-
* @param \Cake\Event\EventInterface $event The beforeFilter event.
41-
* @return void
42-
* @throws \Cake\Http\Exception\NotFoundException
43-
*/
44-
public function beforeFilter(EventInterface $event)
45-
{
46-
if (!Configure::read('debug')) {
47-
throw new NotFoundException();
48-
}
49-
}
50-
5135
/**
5236
* Before render handler.
5337
*

src/Controller/PanelsController.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
*/
1515
namespace DebugKit\Controller;
1616

17-
use Cake\Controller\Controller;
18-
use Cake\Core\Configure;
1917
use Cake\Event\EventInterface;
2018
use Cake\Http\Exception\NotFoundException;
2119

@@ -24,7 +22,7 @@
2422
*
2523
* @property \DebugKit\Model\Table\PanelsTable $Panels
2624
*/
27-
class PanelsController extends Controller
25+
class PanelsController extends DebugKitController
2826
{
2927
/**
3028
* Initialize controller
@@ -36,21 +34,6 @@ public function initialize(): void
3634
$this->loadComponent('RequestHandler');
3735
}
3836

39-
/**
40-
* Before filter handler.
41-
*
42-
* @param \Cake\Event\EventInterface $event The event.
43-
* @return void
44-
* @throws \Cake\Http\Exception\NotFoundException
45-
*/
46-
public function beforeFilter(EventInterface $event)
47-
{
48-
// TODO add config override.
49-
if (!Configure::read('debug')) {
50-
throw new NotFoundException();
51-
}
52-
}
53-
5437
/**
5538
* Before render handler.
5639
*

src/Controller/RequestsController.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,24 @@
1414
*/
1515
namespace DebugKit\Controller;
1616

17-
use Cake\Controller\Controller;
18-
use Cake\Core\Configure;
1917
use Cake\Event\EventInterface;
20-
use Cake\Http\Exception\NotFoundException;
2118

2219
/**
2320
* Provides access to panel data.
2421
*
2522
* @property \DebugKit\Model\Table\RequestsTable $Requests
2623
*/
27-
class RequestsController extends Controller
24+
class RequestsController extends DebugKitController
2825
{
2926
/**
3027
* Before filter handler.
3128
*
3229
* @param \Cake\Event\EventInterface $event The event.
3330
* @return void
34-
* @throws \Cake\Http\Exception\NotFoundException
3531
*/
3632
public function beforeFilter(EventInterface $event)
3733
{
38-
// TODO add config override
39-
if (!Configure::read('debug')) {
40-
throw new NotFoundException();
41-
}
34+
parent::beforeFilter($event);
4235

4336
$this->response = $this->response->withHeader('Content-Security-Policy', '');
4437
}

src/Controller/ToolbarController.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
namespace DebugKit\Controller;
1616

1717
use Cake\Cache\Cache;
18-
use Cake\Controller\Controller;
19-
use Cake\Core\Configure;
20-
use Cake\Event\EventInterface;
2118
use Cake\Http\Exception\NotFoundException;
2219

2320
/**
2421
* Provides utility features need by the toolbar.
2522
*/
26-
class ToolbarController extends Controller
23+
class ToolbarController extends DebugKitController
2724
{
2825
/**
2926
* View class
@@ -42,21 +39,6 @@ public function initialize(): void
4239
$this->loadComponent('RequestHandler');
4340
}
4441

45-
/**
46-
* Before filter handler.
47-
*
48-
* @param \Cake\Event\EventInterface $event The event.
49-
* @return void
50-
* @throws \Cake\Http\Exception\NotFoundException
51-
*/
52-
public function beforeFilter(EventInterface $event)
53-
{
54-
// TODO add config override.
55-
if (!Configure::read('debug')) {
56-
throw new NotFoundException();
57-
}
58-
}
59-
6042
/**
6143
* Clear a named cache.
6244
*

0 commit comments

Comments
 (0)