CRM-14370 - API Kernel - Add UnauthorizedException, NotImplementedException
authorTim Otten <totten@civicrm.org>
Fri, 28 Mar 2014 06:20:35 +0000 (23:20 -0700)
committerTim Otten <totten@civicrm.org>
Sun, 6 Apr 2014 04:19:41 +0000 (21:19 -0700)
Conflicts:

tests/phpunit/Civi/API/Provider/DoctrineCrudProviderTest.php

Civi/API/Exception/NotImplementedException.php [new file with mode: 0644]
Civi/API/Exception/UnauthorizedException.php [new file with mode: 0644]
Civi/API/Kernel.php
Civi/API/Subscriber/PermissionCheck.php

diff --git a/Civi/API/Exception/NotImplementedException.php b/Civi/API/Exception/NotImplementedException.php
new file mode 100644 (file)
index 0000000..69db92e
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+namespace Civi\API\Exception;
+
+require_once 'api/Exception.php';
+class NotImplementedException extends \API_Exception {
+  public function __construct($message, $extraParams = array(), Exception $previous = NULL) {
+    parent::__construct($message, \API_Exception::NOT_IMPLEMENTED, $extraParams, $previous);
+  }
+}
\ No newline at end of file
diff --git a/Civi/API/Exception/UnauthorizedException.php b/Civi/API/Exception/UnauthorizedException.php
new file mode 100644 (file)
index 0000000..5595dcf
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+namespace Civi\API\Exception;
+
+require_once 'api/Exception.php';
+class UnauthorizedException extends \API_Exception {
+  public function __construct($message, $extraParams = array(), Exception $previous = NULL) {
+    parent::__construct($message, \API_Exception::UNAUTHORIZED, $extraParams, $previous);
+  }
+}
\ No newline at end of file
index e5c098f9bec93b115f2d464e0e41e9d9560cd98a..1189bde7f45358872f97fc20b61b9e0c11841695 100644 (file)
@@ -234,7 +234,7 @@ class Kernel {
     $resolveEvent = $this->dispatcher->dispatch(Events::RESOLVE, new ResolveEvent($apiRequest));
     $apiRequest = $resolveEvent->getApiRequest();
     if (!$resolveEvent->getApiProvider()) {
-      throw new \API_Exception("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)", \API_Exception::NOT_IMPLEMENTED);
+      throw new \Civi\API\Exception\NotImplementedException("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)");
     }
     return array($resolveEvent->getApiProvider(), $apiRequest);
   }
@@ -249,7 +249,7 @@ class Kernel {
   public function authorize($apiProvider, $apiRequest) {
     $event = $this->dispatcher->dispatch(Events::AUTHORIZE, new AuthorizeEvent($apiProvider, $apiRequest));
     if (!$event->isAuthorized()) {
-      throw new \API_Exception("Authorization failed", \API_Exception::UNAUTHORIZED);
+      throw new \Civi\API\Exception\UnauthorizedException("Authorization failed");
     }
   }
 
index f108d7b152b7b83d9f2d84d91b1d0b7f9e859fe6..00bc3fef88c6c63ca96229a7302975c191d04359 100644 (file)
@@ -67,7 +67,7 @@ class PermissionCheck implements EventSubscriberInterface {
           $permissions = implode(' and ', $permissions);
         }
         // FIXME: Generating the exception ourselves allows for detailed error but doesn't play well with multiple authz subscribers.
-        throw new \API_Exception("API permission check failed for {$apiRequest['entity']}/{$apiRequest['action']} call; insufficient permission: require $permissions", \API_Exception::UNAUTHORIZED);
+        throw new \Civi\API\Exception\UnauthorizedException("API permission check failed for {$apiRequest['entity']}/{$apiRequest['action']} call; insufficient permission: require $permissions");
       }
 
       $event->authorize();