Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cakephp fashioned redirect if possible #7

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -34,7 +34,8 @@
},
"autoload": {
"psr-4": {
"UserPermissions\\": "src"
"UserPermissions\\": "src",
"Tests\\":"tests"
}
}
}
38 changes: 25 additions & 13 deletions src/Controller/Component/UserPermissionsComponent.php
Expand Up @@ -52,6 +52,11 @@ class UserPermissionsComponent extends Component {
*/
private $throwEx;

/**
* Boolean value true if an redirect is already invoked.
*/
private $isRedirecting;

/**
* Initialization to get controller variable
*
Expand All @@ -68,14 +73,15 @@ public function initialize(array $config)
$this->controller = $this->_registry->getController();
$this->session = $this->controller->request->session();

$this->actions = array();
$this->allow = true;
$this->redirect = '';
$this->params = '';
$this->message = '';
$this->userType = '';
$this->action = null;
$this->throwEx = isset($config["throwEx"]) && $config["throwEx"];
$this->actions = array();
$this->allow = true;
$this->redirect = null;
$this->params = '';
$this->message = '';
$this->userType = '';
$this->action = null;
$this->throwEx = isset($config["throwEx"]) && $config["throwEx"];
$this->isRedirecting = false;
}

/**
Expand Down Expand Up @@ -205,16 +211,22 @@ private function checkForHandler($controller, $handler)

return true;
}

private function redirectIfIsSet()
{
if($this->redirect != ''){
if($this->redirect && !$this->isRedirecting){
$this->isRedirecting = true;
if($this->message != ''){
$this->Flash->set($this->message);
}

header("Location: " . $this->redirect);
exit;
if(method_exists($this->controller, "redirect")) {
$this->controller->redirect($this->redirect);
}
else {
header("Location: " . $this->redirect);
exit;
}
}
}
}
}
@@ -1,6 +1,7 @@
<?php
namespace App\Test\TestCase\Controller\Component;

use App\Test\TestCase\Resources\MockRedirect;
use Cake\Controller\Controller;
use Cake\Controller\ComponentRegistry;
use Cake\Network\Request;
Expand Down Expand Up @@ -226,4 +227,26 @@ public function testMissingHandlerThrowsException() {

$result = $this->userPermissions->allow($rules);
}

public function testCakePhpFashionedRedirect() {
$userType = "user";
$action = "action";
$redirectData = "testRedirectData";
$redirector = new MockRedirect();

$rules = array(
"user_type" => $userType,
"redirect" => $redirectData,
"message" => "You don't have permission to access this page",
"action" => $action,
"controller" => $redirector,
"groups" => array(
$userType => array()
)
);

$result = $this->userPermissions->allow($rules);

$this->assertEquals($redirectData, $redirector->passedData);
}
}
11 changes: 11 additions & 0 deletions tests/TestCase/Resources/MockRedirect.php
@@ -0,0 +1,11 @@
<?php
namespace App\Test\TestCase\Resources;

class MockRedirect
{
public $passedData = null;

public function redirect($data) {
$this->passedData = $data;
}
}