Skip to content

Commit a0cf186

Browse files
kabudukazsaj
authored andcommitted
Use 403 status code on AccessDenied exceptions (#12)
* use the authentication code provided by the exception thrown if available * be more explicit with response codes and exception type * update phpdoc * Adding an additional test for access denied exceptions
1 parent 362a92b commit a0cf186

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"license": "Apache-2.0",
1414
"require": {
1515
"php": ">=5.4.0",
16-
"psr/log": "~1.0"
16+
"psr/log": "~1.0",
17+
"ext-json": "*"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "~4.0",

src/Docnet/JAPI.php

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Docnet\JAPI\Controller;
2020
use Docnet\JAPI\Exceptions\Routing as RoutingException;
2121
use Docnet\JAPI\Exceptions\Auth as AuthException;
22+
use Docnet\JAPI\Exceptions\AccessDenied as AccessDeniedException;
2223
use Psr\Log\LoggerAwareInterface;
2324

2425
/**
@@ -68,6 +69,8 @@ public function bootstrap($controller_source)
6869
$this->jsonError($obj_ex, 404);
6970
} catch (AuthException $obj_ex) {
7071
$this->jsonError($obj_ex, 401);
72+
} catch (AccessDeniedException $obj_ex) {
73+
$this->jsonError($obj_ex, 403);
7174
} catch (\Exception $obj_ex) {
7275
$this->jsonError($obj_ex, $obj_ex->getCode());
7376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Venditan Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Docnet\JAPI\Exceptions;
18+
19+
/**
20+
* AccessDenied Exception
21+
*
22+
* @author Kamba Abudu <[email protected]>
23+
*/
24+
class AccessDenied extends \Exception
25+
{
26+
}

tests/Controllers/AccessDenied.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class AccessDenied extends \Docnet\JAPI\Controller
4+
{
5+
public function dispatch(){
6+
throw new \Docnet\JAPI\Exceptions\AccessDenied('Error Message', 403);
7+
}
8+
}

tests/JAPITest.php

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once('Controllers/Example.php');
44
require_once('Controllers/Exceptional.php');
55
require_once('Controllers/Whoops.php');
6+
require_once('Controllers/AccessDenied.php');
67

78
class JAPITest extends PHPUnit_Framework_TestCase
89
{
@@ -134,4 +135,21 @@ public function testNoLogger()
134135
$obj_japi->bootstrap(new Exceptional());
135136
}
136137

138+
/**
139+
* Test an AccessDenied Exception codes are correctly passed to jsonError from the bootstrap() method
140+
*/
141+
public function testBootstrapAccessDeniedErrorCycle()
142+
{
143+
// Mock JAPI
144+
$obj_japi = $this->getMockBuilder('\\Docnet\\JAPI')->setMethods(['sendResponse', 'jsonError'])->getMock();
145+
$obj_japi->expects($this->never())->method('sendResponse');
146+
$obj_japi->expects($this->once())->method('jsonError')->with(
147+
$this->equalTo(new \Docnet\JAPI\Exceptions\AccessDenied('Error Message', 403)),
148+
$this->equalTo(403)
149+
);
150+
151+
// Dispatch
152+
$obj_japi->bootstrap(new AccessDenied());
153+
}
154+
137155
}

0 commit comments

Comments
 (0)