Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
security namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Jan 19, 2016
1 parent 165dc57 commit ede6cf6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 63 deletions.
70 changes: 42 additions & 28 deletions source/Albus/Albus.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Albus extends Component implements CoreInterface, SingletonInterface
use BenchmarkTrait, GuardedTrait, TranslatorTrait;

/**
* Declaring to IoC to treat Albus as sinleton.
* Declaring to IoC to treat Albus as singleton.
*/
const SINGLETON = self::class;

Expand Down Expand Up @@ -118,39 +118,16 @@ public function callAction($controller, $action = '', array $parameters = [])
);
}

if (!$this->guard()->allows("albus.{$controller}", compact('action'))) {
$permission = "{$this->config->securityNamespace()}.{$controller}";

if (!$this->guard()->allows($permission, compact('action'))) {
throw new ControllerException(
"Unreachable albus controller '{$controller}'",
ControllerException::FORBIDDEN
);
}

$benchmark = $this->benchmark('callAction', $controller . '::' . ($action ?: '~default~'));
$scope = $this->container->replace(CoreInterface::class, $this);

//To let navigation know current controller
$this->controller = $controller;

try {
//Initiating controller with all required dependencies
$object = $this->container->make(
$this->config->controllers()[$controller]
);

if (!$object instanceof ControllerInterface) {
throw new ControllerException(
"Invalid '{$controller}', ControllerInterface not implemented.",
ControllerException::NOT_FOUND
);
}

return $object->callAction($action, $parameters);
} finally {
$this->benchmark($benchmark);
$this->container->restore($scope);

$this->controller = '';
}
return $this->executeController($controller, $action, $parameters);
}

/**
Expand Down Expand Up @@ -210,4 +187,41 @@ protected function createRoute()
{
return $this->config->createRoute('albus')->setAlbus($this);
}

/**
* @param string $controller
* @param string $action
* @param array $parameters
* @return mixed
* @throws ControllerException
*/
protected function executeController($controller, $action, array $parameters)
{
$benchmark = $this->benchmark('callAction', $controller . '::' . ($action ?: '~default~'));
$scope = $this->container->replace(CoreInterface::class, $this);

//To let navigation know current controller
$this->controller = $controller;

try {
//Initiating controller with all required dependencies
$object = $this->container->make(
$this->config->controllers()[$controller]
);

if (!$object instanceof ControllerInterface) {
throw new ControllerException(
"Invalid '{$controller}', ControllerInterface not implemented.",
ControllerException::NOT_FOUND
);
}

return $object->callAction($action, $parameters);
} finally {
$this->benchmark($benchmark);
$this->container->restore($scope);

$this->controller = '';
}
}
}
22 changes: 0 additions & 22 deletions source/Albus/AlbusController.php

This file was deleted.

16 changes: 13 additions & 3 deletions source/Albus/Bootloaders/InsecureAlbusBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace Spiral\Albus\Bootloaders;

use Spiral\Albus\Configs\AlbusConfig;
use Spiral\Albus\Security\Rules\InsecureRule;
use Spiral\Core\Bootloaders\Bootloader;
use Spiral\Security\Entities\Actors\Guest;
Expand All @@ -23,17 +24,26 @@ class InsecureAlbusBootloader extends Bootloader

/**
* @param PermissionsInterface $permissions
* @param AlbusConfig $config
*/
public function boot(PermissionsInterface $permissions)
public function boot(PermissionsInterface $permissions, AlbusConfig $config)
{
if (!$permissions->hasRole(static::ROLE)) {
$permissions->addRole(static::ROLE);
}

//Following rule will raise log message to notify that insecure setting were used
$permissions->associate(static::ROLE, 'albus.*', InsecureRule::class);
$permissions->associate(
static::ROLE,
$config->securityNamespace() . '.*',
InsecureRule::class
);

//Controller specific permissions
$permissions->associate(static::ROLE, 'albus.*.*', InsecureRule::class);
$permissions->associate(
static::ROLE,
$config->securityNamespace() . '.*',
InsecureRule::class
);
}
}
22 changes: 19 additions & 3 deletions source/Albus/Configs/AlbusConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,39 @@ class AlbusConfig extends InjectableConfig
*/
const CONFIG = 'modules/albus';

/**
* Default permissions namespace.
*/
const GUARD_NAMESPACE = 'albus';

/**
* @var array
*/
protected $config = [
'guardNamespace' => 'albus',

//Default albus controller
'controllers' => [],
'navigation' => [],
'controllers' => [],
'navigation' => [],

//Example: albus/users/addresses/1/remove/123
'route' => [
'route' => [
'middlewares' => [],
'pattern' => 'albus[/<controller>[/<action>[/<id>[/<operation>[/<childID>]]]]]',
'defaults' => [],
'matchHost' => false,
]
];

public function securityNamespace()
{
if (empty($this->config['guardNamespace'])) {
return self::GUARD_NAMESPACE;
}

return $this->config['guardNamespace'];
}

/**
* List of allowed albus controllers in a form alias => class.
*
Expand Down
4 changes: 2 additions & 2 deletions source/Albus/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/
namespace Spiral\Albus\Controllers;

use Spiral\Albus\AlbusController;
use Spiral\Core\Controller;

/**
* No guard check in this sample controller.
*/
class DashboardController extends AlbusController
class DashboardController extends Controller
{
/**
* @return string
Expand Down
5 changes: 0 additions & 5 deletions source/AlbusModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public function register(RegistratorInterface $registrator)
" /*{{domain.albus}}*/",
"]"
]);

//Shared albus permissions and rules
$registrator->configure('modules/security', 'libraries', 'spiral/albus', [
'\Spiral\Albus\Security\AlbusLibrary::class'
]);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions source/config/albus.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
*/

return [
/*
* Every controller access will be checked under following permissions namespace. For example
* access to TestController mounted under name "test" will be checked as "albus.test".
*/
'guardNamespace' => 'albus',

/*
* List of controller classes associated with their alias to be available for albus. No other
* controllers can be called.
Expand Down

0 comments on commit ede6cf6

Please sign in to comment.