From 16b3beec5ef4e0fcf47b5a87857d2bea01a9e301 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 2 Jan 2011 13:35:43 -0500 Subject: [PATCH] Starting ControllerAuthorize adding it and the test cases. --- .../components/auth/controller_authorize.php | 52 ++++++++++++++++ .../auth/controller_authorize.test.php | 62 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 cake/libs/controller/components/auth/controller_authorize.php create mode 100644 cake/tests/cases/libs/controller/components/auth/controller_authorize.test.php diff --git a/cake/libs/controller/components/auth/controller_authorize.php b/cake/libs/controller/components/auth/controller_authorize.php new file mode 100644 index 000000000..ed78ac5bd --- /dev/null +++ b/cake/libs/controller/components/auth/controller_authorize.php @@ -0,0 +1,52 @@ +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; + } +} \ No newline at end of file diff --git a/cake/tests/cases/libs/controller/components/auth/controller_authorize.test.php b/cake/tests/cases/libs/controller/components/auth/controller_authorize.test.php new file mode 100644 index 000000000..9863d23de --- /dev/null +++ b/cake/tests/cases/libs/controller/components/auth/controller_authorize.test.php @@ -0,0 +1,62 @@ +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)); + } +}