Skip to content

Commit

Permalink
Merge pull request #15 from youssman/master
Browse files Browse the repository at this point in the history
Added secure method
  • Loading branch information
Marco Janssen authored May 8, 2017
2 parents f451ed0 + b37fbaf commit f0a860c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ before_script:
- travis_retry composer install --no-interaction --prefer-source --dev

script:
- phpunit --coverage-text --coverage-clover=coverage.clover
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ Add an after-middleware: http://silex.sensiolabs.org/doc/middlewares.html#after-
$after = array('after' => function() {})
```

### secure (array)

Secures a controller for the given roles: http://silex.sensiolabs.org/doc/providers/security.html#traits

``` php
$secure = array('ROLE_ADMIN')
```

## Usage

### Adding a single route
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"silex/silex": "^2.0"
},
"require-dev": {
"symfony/security": "~v3.1.0",
"scrutinizer/ocular": "^1.3",
"phpunit/phpunit": "^5.5"
},
Expand Down
19 changes: 16 additions & 3 deletions src/MJanssen/Provider/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function addRoute(Container $app, array $route, $name = '')
join('|', array_map('strtoupper', $route['method']))
);

$supportedProperties = array('value', 'assert', 'convert', 'before', 'after');
$supportedProperties = array('value', 'assert', 'convert', 'before', 'after', 'secure');
foreach ($supportedProperties AS $property) {
if (isset($route[$property])) {
$this->addActions($controller, $route[$property], $property);
Expand Down Expand Up @@ -223,6 +223,9 @@ protected function addActions(Controller $controller, $actions, $type)
case 'before':
$this->addBeforeAfterMiddleware($controller, $type, $value);
break;
case 'secure':
$this->addSecure($controller, $type, $actions);
break;
default:
$this->addAction($controller, $name, $value, $type);
break;
Expand All @@ -241,9 +244,19 @@ protected function addAction(Controller $controller, $name, $value, $type)
call_user_func_array(array($controller, $type), array($name, $value));
}

/**
* @param Controller $controller
* @param $type
* @param array $values
*/
protected function addSecure(Controller $controller, $type, Array $values)
{
call_user_func_array(array($controller, $type), $values);
}

protected function isClosure($param)
{
return is_object($param) && ($param instanceof \Closure);
return is_object($param) && is_callable($param);
}

/**
Expand Down Expand Up @@ -304,4 +317,4 @@ protected function addMiddlewareFromConfig(Controller $controller, $type, $value
$controller->$type([new $class, $method]);
}
}
}
}
56 changes: 55 additions & 1 deletion tests/MJanssen/Provider/RoutingServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Silex\Application;
use Symfony\Component\Routing\RouteCollection;

class RoutingServiceProviderTest extends \PHPUnit_Framework_TestCase
class RoutingServiceProviderTest extends \PHPUnit\Framework\TestCase
{

private $validRoute = array(
Expand Down Expand Up @@ -345,6 +345,60 @@ public function testRouteAssert()
$this->assertEquals('regexp_name', $requirements['name']);
}

/**
* test if secure matches
*/
public function testRouteSecure()
{
$route = array(
'pattern' => '/bar',
'controller' => 'MJanssen\Controller\barController::barAction',
'method' => array('get'),
'secure' => array('ROLE_ADMIN')
);
$app = new Application();
$app['route_class'] = Security::class;
$routingServiceProvider = new RoutingServiceProvider();
$routingServiceProvider->addRoute($app, $route);
$routeCollection = $app['controllers']->flush();
$secure = $routeCollection->getIterator()->current();
$this->assertInstanceOf(Security::class, $secure);
}

/**
* @expectedException \BadMethodCallException
*/
public function testInvalidSecurityRoute()
{
$route = array(
'pattern' => '/bar',
'controller' => 'MJanssen\Controller\barController::barAction',
'method' => array('get'),
'secure' => array('ROLE_ADMIN')
);
$app = new Application();
$routingServiceProvider = new RoutingServiceProvider();
$routingServiceProvider->addRoute($app, $route);
$app['controllers']->flush();
}

/**
* @expectedException InvalidArgumentException
*/
public function testInvalidSecurityRouteParameter()
{
$route = array(
'pattern' => '/bar',
'controller' => 'MJanssen\Controller\barController::barAction',
'method' => array('get'),
'secure' => 'ROLE_ADMIN'
);
$app = new Application();
$routingServiceProvider = new RoutingServiceProvider();
$routingServiceProvider->addRoute($app, $route);
$app['controllers']->flush();
}

/**
* @return mixed
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/MJanssen/Provider/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace MJanssen\Provider;

use Silex\Route;
use Silex\Route\SecurityTrait;

class Security extends Route
{
use SecurityTrait;
}

0 comments on commit f0a860c

Please sign in to comment.