Permalink
Browse files

add hook filter before response

  • Loading branch information...
WellingGuzman committed Feb 17, 2017
1 parent 37084ca commit f825c8c9e870b57c8fbf870c186b5b1a9fe6bb5d

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -10,6 +10,8 @@
namespace Directus\Application;
use Directus\Bootstrap;
use Directus\Util\ArrayUtils;
use Slim\Http\Util;
use Slim\Slim;
@@ -39,6 +41,10 @@ public function __construct(array $userSettings)
{
parent::__construct($userSettings);
$this->container->singleton('response', function () {
return new BaseResponse();
});
$request = $this->request();
// @NOTE: Slim request do not parse a json request body
// We need to parse it ourselves
@@ -87,6 +93,49 @@ public function boot()
$this->booted = true;
}
public function response()
{
$response = parent::response();
if (func_num_args() > 0) {
$data = ArrayUtils::get(func_get_args(), 0);
$options = ArrayUtils::get(func_get_args(), 1);
$data = $this->triggerFilter('response', $data, (array) $options);
// @TODO: Response will support xml
$response->setBody(json_encode($data));
}
return $response;
}
/**
* Trigger Filter by name with Options as payload
*
* @param $name
* @param $data
* @param $options
*
* @return mixed
*/
protected function triggerFilter($name, $data, array $options = [])
{
$emitter = Bootstrap::get('hookEmitter');
$payload = (object) array_merge($options, [
'data' => $data,
'request' => [
'path' => $this->request()->getResourceUri(),
'method' => $this->request()->getMethod()
]
]);
$payload = $emitter->apply($name, $payload);
return $payload->data;
}
/**
* @inheritdoc
*/
@@ -0,0 +1,22 @@
<?php
namespace Directus\Application;
use Slim\Http\Response;
class BaseResponse extends Response
{
public function withHeaders(array $headers)
{
foreach($headers as $name => $value) {
$this->headers->set($name, $value);
}
return $this;
}
public function setHeader($name, $value)
{
return $this->withHeaders([$name => $value]);
}
}
@@ -23,6 +23,6 @@ public function activity()
$data = $Activity->fetchFeed($params);
return JsonView::render($data);
return $this->app->response($data);
}
}
}
@@ -40,7 +40,7 @@ public function requestToken()
}
}
return JsonView::render($response);
return $this->app->response($response);
}
public function login()
@@ -59,7 +59,7 @@ public function login()
if ($auth->loggedIn()) {
$response['success'] = true;
unset($response['error']);
return JsonView::render($response);
return $this->app->response($response);
}
$req = $app->request();
@@ -69,18 +69,18 @@ public function login()
$user = $Users->findOneBy('email', $email);
if (!$user) {
return JsonView::render($response);
return $this->app->response($response);
}
// ------------------------------
// Check if group needs whitelist
$groupId = $user['group'];
$directusGroupsTableGateway = new DirectusGroupsTableGateway($ZendDb, $acl);
if (!$directusGroupsTableGateway->acceptIP($groupId, $app->request->getIp())) {
return JsonView::render([
return $this->app->response([
'message' => 'Request not allowed from IP address',
'success' => false,
'all_nonces' => $requestNonceProvider->getAllNonces()
'success' => false
// 'all_nonces' => $requestNonceProvider->getAllNonces()
]);
}
@@ -107,7 +107,7 @@ public function login()
$response['success'] = false;
$response['error']['message'] = __t('login_error_user_is_not_active');
return JsonView::render($response);
return $this->app->response($response);
}
if ($response['success']) {
@@ -128,7 +128,7 @@ public function login()
$Activity->recordLogin($user['id']);
}
JsonView::render($response);
return $this->app->response($response);
}
public function logout($inactive = null)
@@ -138,6 +138,7 @@ public function logout($inactive = null)
if ($auth->loggedIn()) {
$auth->logout();
}
if ($inactive) {
$app->redirect(DIRECTUS_PATH . 'login.php?inactive=1');
} else {
@@ -186,7 +187,6 @@ public function resetPassword($token)
});
$app->halt(200, __t('password_reset_new_temporary_password_sent'));
}
public function forgotPassword()
@@ -198,7 +198,7 @@ public function forgotPassword()
$email = $app->request()->post('email');
if (!isset($email)) {
return JsonView::render([
return $this->app->response([
'success' => false,
'error' => [
'message' => __t('password_forgot_invalid_email')
@@ -210,7 +210,7 @@ public function forgotPassword()
$user = $DirectusUsersTableGateway->findOneBy('email', $email);
if (false === $user) {
return JsonView::render([
return $this->app->response([
'success' => false,
'error' => [
'message' => __t('password_forgot_no_account_found')
@@ -227,7 +227,7 @@ public function forgotPassword()
$affectedRows = $DirectusUsersTableGateway->update($set, ['id' => $user['id']]);
if (1 !== $affectedRows) {
return JsonView::render([
return $this->app->response([
'success' => false
]);
}
@@ -238,7 +238,7 @@ public function forgotPassword()
$message->setTo($user['email']);
});
return JsonView::render([
return $this->app->response([
'success' => true
]);
}
@@ -247,14 +247,14 @@ public function permissions()
{
$acl = $this->app->container->get('acl');
JsonView::render([
return $this->app->response([
'data' => $acl->getGroupPrivileges()
]);
}
public function session()
{
JsonView::render($_SESSION);
return $this->app->response($_SESSION);
}
public function clearSession()
@@ -267,7 +267,8 @@ public function clearSession()
$params['secure'], $params['httponly']
);
}
session_destroy();
JsonView::render($_SESSION);
return $this->app->response($_SESSION);
}
}
}
@@ -39,7 +39,7 @@ public function bookmarks($id = null)
];
}
return JsonView::render($response);
return $this->app->response($response);
}
if (!is_null($id)) {
@@ -48,7 +48,7 @@ public function bookmarks($id = null)
$jsonResponse = $bookmarks->fetchByUserId($currentUserId);
}
return JsonView::render([
return $this->app->response([
'meta' => [
'table' => 'directus_bookmarks',
'type' => is_null($id) ? 'collection' : 'item'
@@ -85,7 +85,7 @@ public function selfBookmarks()
$jsonResponse = $bookmarks->fetchByUserId($currentUserId);
return JsonView::render([
return $this->app->response([
'meta' => [
'table' => 'directus_bookmarks',
'type' => 'collection'
@@ -122,7 +122,7 @@ public function userBookmarks($currentUserId)
$jsonResponse = $bookmarks->fetchByUserId($currentUserId);
return JsonView::render([
return $this->app->response([
'meta' => [
'table' => 'directus_bookmarks',
'type' => 'collection'
@@ -139,6 +139,6 @@ public function allBookmarks()
$tableGateway = new RelationalTableGateway('directus_bookmarks', $ZendDb, $acl);
$params = $app->request()->get();
return JsonView::render($tableGateway->getItems($params));
return $this->app->response($tableGateway->getItems($params));
}
}
}
@@ -36,7 +36,7 @@ public function rows($table)
// GET all table entries
$response = $tableGateway->getEntries($params);
JsonView::render($response);
return $this->app->response($response);
}
public function rowsBulk($table)
@@ -72,7 +72,7 @@ public function rowsBulk($table)
}
$entries = $TableGateway->getEntries($params);
JsonView::render($entries);
return $this->app->response($entries);
}
public function typeAhead($table, $query = null)
@@ -113,7 +113,8 @@ public function typeAhead($table, $query = null)
$val = implode(' ', $tokens);
array_push($response, ['value' => $val, 'tokens' => $tokens, 'id' => $entry['id']]);
}
JsonView::render($response);
return $this->app->response($response);
}
public function row($table, $id)
@@ -150,7 +151,7 @@ public function row($table, $id)
// DELETE a given table entry
case 'DELETE':
$success = $TableGateway->delete([$TableGateway->primaryKeyFieldName => $id]);
return JsonView::render([
return $this->app->response([
'meta' => [
'table' => $table
],
@@ -168,6 +169,7 @@ public function row($table, $id)
'success' => false
];
}
JsonView::render($response);
return $this->app->response($response);
}
}
}
@@ -63,7 +63,7 @@ public function files($id = null)
];
}
JsonView::render($response);
return $this->app->response($response);
}
public function upload()
@@ -74,7 +74,7 @@ public function upload()
$result[] = $Files->upload($file);
}
JsonView::render($result);
return $this->app->response($result);
}
public function uploadLink()
@@ -120,6 +120,6 @@ public function uploadLink()
}
}
JsonView::render($result);
return $this->app->response($result);
}
}
}
@@ -42,7 +42,7 @@ public function groups()
'data' => $outputData
];
JsonView::render($outputData);
return $this->app->response($outputData);
}
public function group($id)
@@ -69,6 +69,6 @@ public function group($id)
$response = $schemaManager->parseRecordValuesByType($response, $columns);
// $response = SchemaManager::parseRecordValuesByType($response, $columns);
JsonView::render($response);
return $this->app->response($response);
}
}
}
Oops, something went wrong.

0 comments on commit f825c8c

Please sign in to comment.