Skip to content

Commit

Permalink
Various fixes, including for API access and rendering foreign keys in…
Browse files Browse the repository at this point in the history
… index views
  • Loading branch information
Benn Oshrin committed Oct 29, 2018
1 parent 8f79280 commit 2af824e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
33 changes: 26 additions & 7 deletions app/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* Application setup class.
Expand All @@ -45,13 +47,30 @@ public function middleware($middlewareQueue) {
->add(AssetMiddleware::class)

// Add routing middleware.
->add(new RoutingMiddleware($this));

// Enable CSRF protection using Cake v3.5+ approach.
// Initially, we use the default options, except a different CSRF cookie
// name to avoid conflicts with Registry.
$middlewareQueue->add(new CsrfProtectionMiddleware(['cookieName' => 'matchCsrfToken']));

->add(new RoutingMiddleware($this))

// Enable CSRF protection using Cake v3.5+ approach.
// Initially, we use the default options, except a different CSRF cookie
// name to avoid conflicts with Registry. Additionally, we don't want CSRF
// checking enabled on API requests (which are stateless and should not be
// called from web browsers). See eg
// https://stackoverflow.com/questions/47714940/cakephp-3-5-6-disable-csrf-middleware-for-controller
// https://stackoverflow.com/questions/51931406/post-requests-for-cakephp-3-api-are-not-working

->add(function(ServerRequestInterface $request,
ResponseInterface $response,
callable $next) {
$params = $request->getAttribute('params');

if($params['controller'] == 'TierApi') {
// Do not enable CsrfProtectionMiddleware
return $next($request, $response);
} else {
$csrf = new CsrfProtectionMiddleware(['cookieName' => 'matchCsrfToken']);
return $csrf($request, $response, $next);
}
});

return $middlewareQueue;
}
}
18 changes: 15 additions & 3 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function edit($id) {
$this->getPrimaryLink();

// AutoViewVarsTrait
$this->populateAutoViewVars();
$this->populateAutoViewVars($obj);

// Default view title is edit object display field
$field = $this->$modelsName->getDisplayField();
Expand Down Expand Up @@ -276,6 +276,9 @@ public function index() {
// PrimaryLinkTrait
$link = $this->getPrimaryLink();

// AutoViewVarsTrait
$this->populateAutoViewVars();

if(!empty($link['linkattr'])) {
// If a link attribute is defined but no value is provided, then query
// where the link attribute is NULL
Expand All @@ -297,9 +300,10 @@ public function index() {
* Populate any auto view variables, as requested via AutoViewVarsTrait.
*
* @since COmanage Match v1.0.0
* @param object $obj Current object (eg: from edit), if set
*/

protected function populateAutoViewVars() {
protected function populateAutoViewVars(object $obj=null) {
// $this->name = Models
$modelsName = $this->name;

Expand Down Expand Up @@ -338,8 +342,16 @@ protected function populateAutoViewVars() {
$linkFilter = $this->$modelsName->getPrimaryLink();

if($linkFilter) {
// Try to find the $linkFilter value
$v = null;

// We might have been passed an object with the current value
if($obj && !empty($obj->$linkFilter)) {
$v = $obj->$linkFilter;
} elseif(!empty($this->request->getQuery($linkFilter))) {
$v = $this->request->getQuery($linkFilter);
}
// XXX also need to check getData()?
$v = $this->request->getQuery($linkFilter);
if($v) {
$query = $query->find($avv['find'], [$linkFilter => $v]);
Expand Down
20 changes: 20 additions & 0 deletions app/src/Template/Standard/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ function _column_key($modelsName, $c) {
case 'enum':
print __('match.en.'.$cfg['class'].'.'.$entity->$col);
break;
case 'fk':
// Assuming $col is of the form foo_id, look to see if the corresponding
// AutoViewVar $foos is set, and if so render the lookup value instead
$f = null;
if(preg_match('/^(.*?)_id$/', $col, $f)) {
$avv = \Cake\Utility\Inflector::pluralize($f[1]);

if(!empty(${$avv}[$entity->$col])) {
// We found the viewar (eg: $foos), and it has a corresponding value
// (eg: $foos[3]), so render it
print ${$avv}[$entity->$col]; // XXX filter_var?
} else {
// No match, just render the value
print $entity->$col;
}
} else {
// Just print the value
print $entity->$col;
}
break;
case 'link':
print $this->Html->link($entity->$col, ['action' => $primaryAction, $entity->id]);
break;
Expand Down

0 comments on commit 2af824e

Please sign in to comment.