amonger/firewall

v0.4.1 2015-09-02 10:41 UTC

This package is auto-updated.

Last update: 2024-05-12 21:58:20 UTC


README

This is a simple library which will have some action on a route being matched.

It is useful for legacy projects where some authorisation code my be copy pasted into a header multiple times and there is no under-laying structure.

Example

###Individual routes### You can define routes individually by doing the following:

use \amonger\Firewall\Firewall;

$firewall = new Firewall($_SERVER['REQUEST_URI']);

$firewall
    ->route('/managers\/.*/')
    ->unless(function ($uri) use ($container) {
       return $container['auth']->hasRole('manager');
    })
    ->handle(function () {
        throw new _401Exception();
    })
    ->execute();

###Multiple routes### A scenario which is more likely is that you will have a single request uri and multiple routes you'd like to handle. In this case you can use the builder to setup the firewall.

use \amonger\Firewall\Firewall;

$firewall = Firewall::getBuilder();
$firewall->setRequestUri($_SERVER['REQUEST_URI']);

$firewall
    ->route('/managers\/.*/')
    ->unless(function ($uri) use ($container) {
       return $container['auth']->hasRole('manager');
    })
    ->handle(function () {
        throw new _401Exception();
    });

$firewall
    ->route('/clients\/.*/')
    ->unless(function ($uri) use ($container) {
       return $container['auth']->hasRole('clients');
    })
    ->handle(function () {
        throw new _401Exception();
    });

Firewall::run($firewall);