Skip to content

Commit ca93510

Browse files
committed
API Add onBeforeApi and onAfterApi extension hooks
1 parent ecc4630 commit ca93510

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/Controllers/RestApiEndpoint.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ abstract class RestApiEndpoint extends Controller
4646
public const DELIMITER = '_';
4747
public const CREATE_EDIT_DELETE_ACTION = 'CREATE_EDIT_DELETE_ACTION';
4848
public const VIEW_CREATE_EDIT_DELETE_ACTION = 'VIEW_CREATE_EDIT_DELETE_ACTION';
49+
// other constants
50+
public const CSRF_TOKEN_HEADER = 'x-csrf-token';
4951

5052
private static array $url_handlers = [
5153
'$@' => 'api',
@@ -63,6 +65,7 @@ abstract class RestApiEndpoint extends Controller
6365
public function api(): HTTPResponse
6466
{
6567
try {
68+
$this->invokeWithExtensions('onBeforeApi');
6669
// Allow extensions or subclasses to update $api_config
6770
$apiConfig = $this->config()->get('api_config');
6871
$this->invokeWithExtensions('updateApiConfig', $apiConfig);
@@ -107,6 +110,8 @@ public function api(): HTTPResponse
107110
// In dev mode this gives feedback to the developer
108111
// In prod mode it doesn't give any feedback though it will show in error logs
109112
throw $e;
113+
} finally {
114+
$this->invokeWithExtensions('onAfterApi');
110115
}
111116
}
112117

@@ -177,7 +182,7 @@ private function canAccess(string $subSchemaAccess = ''): bool
177182
}
178183
// CSRF-Token check only on non-subSchemaAccess aka root level
179184
if (SecurityToken::is_enabled() && $subSchemaAccess === '') {
180-
$token = $this->getRequest()->getHeader('x-csrf-token');
185+
$token = $this->getRequest()->getHeader(self::CSRF_TOKEN_HEADER);
181186
if (!$token) {
182187
throw new RestApiEndpointException('Missing x-csrf-token header', 400);
183188
}

0 commit comments

Comments
 (0)