Skip to content

Commit

Permalink
throw an exception when using not registered resource object [closes #8]
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkudera committed Aug 28, 2015
1 parent e74b40a commit 54283f1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Authorization/Authorizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carrooi\Security\InvalidStateException;
use Carrooi\Security\StrictModeException;
use Carrooi\Security\UnknownResourceObjectException;
use Carrooi\Security\User\User;
use Nette\Application\UI\Presenter;
use Nette\Object;
Expand Down Expand Up @@ -31,6 +32,9 @@ class Authorizator extends Object
/** @var bool */
private $default = false;

/** @var bool */
private $debugMode = false;

/** @var int */
private $actionsMode = self::MODE_ON;

Expand All @@ -50,6 +54,17 @@ public function __construct(ResourcesManager $resourcesManager)
}


/**
* @param bool $debugMode
* @return $this
*/
public function setDebugMode($debugMode = true)
{
$this->debugMode = (bool) $debugMode;
return $this;
}


/**
* @return \Carrooi\Security\Authorization\ResourcesManager
*/
Expand Down Expand Up @@ -181,6 +196,10 @@ public function isAllowed(User $user, $resource, $action)

$name = $this->resourcesManages->getTargetResource($resource);
if (!$name) {
if ($this->debugMode) {
throw new UnknownResourceObjectException('Object '. get_class($resource). ' is not registered security resource target.');
}

return $this->default;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Authorization/ResourcesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carrooi\Security\AuthorizatorClassNotExistsException;
use Carrooi\Security\AuthorizatorInvalidTypeException;
use Carrooi\Security\InvalidArgumentException;
use Nette\DI\Container;
use Nette\Object;
use Nette\Reflection\ClassType;
Expand Down Expand Up @@ -57,6 +58,10 @@ public function getTargetResource($resource)
return $resource;
}

if (!is_object($resource)) {
throw new InvalidArgumentException('Security resource target can be only string or an object, '. gettype($resource). ' given.');
}

$className = get_class($resource);

if (!isset($this->targetResources[$className])) {
Expand Down
1 change: 1 addition & 0 deletions src/DI/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function loadConfiguration()

$builder->addDefinition($this->prefix('authorizator'))
->setClass('Carrooi\Security\Authorization\Authorizator')
->addSetup('setDebugMode', [$builder->parameters['debugMode']])
->addSetup('setDefault', [$config['default']])
->addSetup('setComponentsMode', [$this->parseMode($config['components'])])
->addSetup('setSignalsMode', [$this->parseMode($config['signals'])])
Expand Down
4 changes: 4 additions & 0 deletions src/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class RuntimeException extends \RuntimeException {}

class LogicException extends \LogicException {}

class InvalidArgumentException extends \InvalidArgumentException {}

class InvalidStateException extends RuntimeException {}

class NotImplementedException extends LogicException {}
Expand All @@ -15,3 +17,5 @@ class StrictModeException extends LogicException {}
class AuthorizatorClassNotExistsException extends LogicException {}

class AuthorizatorInvalidTypeException extends LogicException {}

class UnknownResourceObjectException extends LogicException {}
12 changes: 12 additions & 0 deletions tests/CarrooiTests/Security/Authorization/Authorizator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ class AuthorizatorTest extends TestCase
}


public function testIsAllowed_targetResource_unknown_debugMode()
{
$this->manager->shouldReceive('getTargetResource')->once()->andReturnNull()->getMock();

$this->authorizator->setDebugMode(true);

Assert::exception(function() {
$this->authorizator->isAllowed($this->user, new \stdClass, 'view');
}, 'Carrooi\Security\UnknownResourceObjectException', 'Object stdClass is not registered security resource target.');
}


public function testIsAllowed_targetResource()
{
$booksAuthorizator = \Mockery::mock('Carrooi\Security\Authorizator\IResourceAuthorizator')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class ResourceManagerTest extends TestCase
}


public function testGetTargetResource_invalid()
{
Assert::exception(function() {
$this->manager->getTargetResource([]);
}, 'Carrooi\Security\InvalidArgumentException', 'Security resource target can be only string or an object, array given.');
}


public function testGetTargetResource_exactClass()
{
$this->manager->addTargetResource('stdClass', 'book');
Expand Down

0 comments on commit 54283f1

Please sign in to comment.