Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting ControllerAuthorize adding it and the test cases.
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
cake/libs/controller/components/auth/controller_authorize.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
|
||
class ControllerAuthorize { | ||
/** | ||
* Controller for the request. | ||
* | ||
* @var Controller | ||
*/ | ||
protected $_controller = null; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Controller $controller The controller for this request. | ||
* @param string $settings An array of settings. This class does not use any settings. | ||
*/ | ||
public function __construct(Controller $controller, $settings = array()) { | ||
$this->controller($controller); | ||
} | ||
|
||
/** | ||
* Checks user authorization using a controller callback. | ||
* | ||
* @param array $user Active user data | ||
* @param CakeRequest $request | ||
* @return boolean | ||
*/ | ||
public function authorize($user, CakeRequest $request) { | ||
return (bool) $this->_controller->isAuthorized($user); | ||
} | ||
|
||
/** | ||
* Accessor to the controller object. | ||
* | ||
* @param mixed $controller null to get, a controller to set. | ||
* @return mixed. | ||
*/ | ||
public function controller($controller = null) { | ||
if ($controller) { | ||
if (!$controller instanceof Controller) { | ||
throw new CakeException(__('$controller needs to be an instance of Controller')); | ||
} | ||
if (!method_exists($controller, 'isAuthorized')) { | ||
throw new CakeException(__('$controller does not implement an isAuthorized() method.')); | ||
} | ||
$this->_controller = $controller; | ||
return true; | ||
} | ||
return $this->_controller; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
cake/tests/cases/libs/controller/components/auth/controller_authorize.test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
App::import('Component', 'auth/controller_authorize'); | ||
App::import('Core', 'CakeRequest'); | ||
App::import('Core', 'Controller'); | ||
|
||
class ControllerAuthorizeTest extends CakeTestCase { | ||
|
||
/** | ||
* setup | ||
* | ||
* @return void | ||
*/ | ||
function setUp() { | ||
parent::setUp(); | ||
$this->controller = $this->getMock('Controller', array('isAuthorized'), array(), '', false); | ||
$this->auth = new ControllerAuthorize($this->controller); | ||
} | ||
|
||
/** | ||
* | ||
* @expectedException CakeException | ||
*/ | ||
function testControllerTypeError() { | ||
$this->auth->controller(new StdClass()); | ||
} | ||
|
||
/** | ||
* @expectedException CakeException | ||
*/ | ||
function testControllerErrorOnMissingMethod() { | ||
$this->auth->controller(new Controller()); | ||
} | ||
|
||
/** | ||
* test failure | ||
* | ||
* @return void | ||
*/ | ||
function testAuthorizeFailure() { | ||
$user = array(); | ||
$request = new CakeRequest('/posts/index', false); | ||
$this->assertFalse($this->auth->authorize($user, $request)); | ||
} | ||
|
||
/** | ||
* test isAuthorized working. | ||
* | ||
* @return void | ||
*/ | ||
function testAuthorizeSuccess() { | ||
$user = array('User' => array('username' => 'mark')); | ||
$request = new CakeRequest('/posts/index', false); | ||
|
||
$this->controller->expects($this->once()) | ||
->method('isAuthorized') | ||
->with($user) | ||
->will($this->returnValue(true)); | ||
|
||
$this->assertTrue($this->auth->authorize($user, $request)); | ||
} | ||
} |