Skip to content

Commit 93f5413

Browse files
kabudukazsaj
authored andcommitted
Access denied exception using appropriate http status code (#13)
* AccessDenied exception implementation * code formatting * keep uniform
1 parent 5fa812a commit 93f5413

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"homepage": "https://github.com/DocnetUK/php-japi",
77
"license": "Apache-2.0",
88
"require": {
9-
"php": ">=5.3.0"
9+
"php": ">=5.3.0",
10+
"ext-json": "*"
1011
},
1112
"autoload": {
1213
"classmap": [

src/Docnet/JAPI.php

+31-20
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
namespace Docnet;
1819

1920
use \Docnet\JAPI\Exceptions\Routing as RoutingException;
2021
use \Docnet\JAPI\Exceptions\Auth as AuthException;
2122
use \Docnet\JAPI\Exceptions\Maintenance as MaintenanceException;
23+
use \Docnet\JAPI\Exceptions\AccessDenied as AccessDeniedException;
2224

2325
/**
2426
* Front controller for our JSON APIs
@@ -35,35 +37,35 @@ class JAPI
3537
/**
3638
* @var JAPI\Config
3739
*/
38-
private static $obj_config = NULL;
40+
private static $obj_config = null;
3941

4042
/**
4143
* @var JAPI\Router
4244
*/
43-
private static $obj_router = NULL;
45+
private static $obj_router = null;
4446

4547
/**
4648
* @var JAPI\Logger
4749
*/
48-
private $obj_logger = NULL;
50+
private $obj_logger = null;
4951

5052
/**
5153
* @var null|float
5254
*/
53-
private static $flt_startup = NULL;
55+
private static $flt_startup = null;
5456

5557
/**
5658
* When creating a new JAPI, hook up the shutdown function and set Config
5759
*
5860
* @param null|JAPI\Config $obj_config
5961
*/
60-
public function __construct($obj_config = NULL)
62+
public function __construct($obj_config = null)
6163
{
6264
register_shutdown_function(array($this, 'timeToDie'));
63-
if(NULL !== $obj_config) {
65+
if (null !== $obj_config) {
6466
self::$obj_config = $obj_config;
6567
}
66-
self::$flt_startup = (isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE));
68+
self::$flt_startup = (isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(true));
6769
}
6870

6971
/**
@@ -85,6 +87,9 @@ public function run()
8587
} catch (AuthException $obj_ex) {
8688
$this->jsonError($obj_ex, 401);
8789

90+
} catch (AccessDeniedException $obj_ex) {
91+
$this->jsonError($obj_ex, 403);
92+
8893
} catch (\Exception $obj_ex) {
8994
$this->jsonError($obj_ex);
9095
}
@@ -107,26 +112,30 @@ public function timeToDie()
107112
* Whatever went wrong, let 'em have it in JSON
108113
*
109114
* One day...
115+
*
110116
* @see http://www.php.net/manual/en/function.http-response-code.php
111117
*
112118
* @param string|\Exception $mix_message
113119
* @param int $int_code
114120
*/
115-
protected function jsonError($mix_message = NULL, $int_code = 500)
121+
protected function jsonError($mix_message = null, $int_code = 500)
116122
{
117123
switch ($int_code) {
118124
case 401:
119-
header($_SERVER["SERVER_PROTOCOL"] . " 401 Unauthorized", TRUE, 401);
125+
header($_SERVER["SERVER_PROTOCOL"] . " 401 Unauthorized", true, 401);
126+
break;
127+
case 403:
128+
header($_SERVER["SERVER_PROTOCOL"] . " 403 Forbidden", true, 401);
120129
break;
121130
case 404:
122-
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", TRUE, 404);
131+
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);
123132
break;
124133
case 503:
125-
header($_SERVER["SERVER_PROTOCOL"] . " 503 Service Unavailable", TRUE, 503);
134+
header($_SERVER["SERVER_PROTOCOL"] . " 503 Service Unavailable", true, 503);
126135
break;
127136
case 500:
128137
default:
129-
header($_SERVER["SERVER_PROTOCOL"] . " 500 Internal Server Error", TRUE, 500);
138+
header($_SERVER["SERVER_PROTOCOL"] . " 500 Internal Server Error", true, 500);
130139
}
131140
if ($mix_message instanceof \Exception) {
132141
$str_log = get_class($mix_message) . ': ' . $mix_message->getMessage();
@@ -137,10 +146,12 @@ protected function jsonError($mix_message = NULL, $int_code = 500)
137146
$str_log = $str_message = 'Unknown error';
138147
}
139148
header('Content-type: application/json');
140-
echo json_encode(array(
141-
'response' => (int)$int_code,
142-
'msg' => $str_message
143-
));
149+
echo json_encode(
150+
array(
151+
'response' => (int)$int_code,
152+
'msg' => $str_message,
153+
)
154+
);
144155
$this->log(LOG_ERR, "[JAPI exiting with {$int_code}] " . $str_log);
145156
exit();
146157
}
@@ -152,7 +163,7 @@ protected function jsonError($mix_message = NULL, $int_code = 500)
152163
*/
153164
public static function getRouter()
154165
{
155-
if (NULL === self::$obj_router) {
166+
if (null === self::$obj_router) {
156167
self::$obj_router = new JAPI\Router();
157168
}
158169
return self::$obj_router;
@@ -175,7 +186,7 @@ public function setRouter(JAPI\Interfaces\Router $obj_router)
175186
*/
176187
public static function getConfig()
177188
{
178-
if(NULL === self::$obj_config) {
189+
if (null === self::$obj_config) {
179190
self::$obj_config = new JAPI\Config();
180191
}
181192
return self::$obj_config;
@@ -189,7 +200,7 @@ public static function getConfig()
189200
*/
190201
public static function getDuration($int_dp = 4)
191202
{
192-
return round(microtime(TRUE) - self::$flt_startup, $int_dp);
203+
return round(microtime(true) - self::$flt_startup, $int_dp);
193204
}
194205

195206
/**
@@ -200,7 +211,7 @@ public static function getDuration($int_dp = 4)
200211
*/
201212
protected function log($int_level, $str_message)
202213
{
203-
if(NULL === $this->obj_logger) {
214+
if (null === $this->obj_logger) {
204215
$this->obj_logger = new JAPI\Logger();
205216
}
206217
$this->obj_logger->log($int_level, $str_message);
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+
}

0 commit comments

Comments
 (0)