kokspflanze / bjy-authorize
Laminas\Acl based firewall system for Laminas dispatch protection
Installs: 132 708
Dependents: 7
Suggesters: 0
Security: 0
Stars: 19
Watchers: 5
Forks: 161
Open Issues: 2
Requires
- php: ^7.3 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- laminas/laminas-authentication: ^2.8.0
- laminas/laminas-cache: ^2.13.2 || ^3.1.0
- laminas/laminas-eventmanager: ^3.4.0
- laminas/laminas-http: ^2.15.0
- laminas/laminas-mvc: ^3.2.0
- laminas/laminas-permissions-acl: ^2.8.0
- laminas/laminas-servicemanager: ^3.7.0
- laminas/laminas-view: ^2.14.2
Requires (Dev)
- doctrine/persistence: ^1.3.8 || ^2.2.2
- laminas/laminas-cache-storage-adapter-memory: @stable
- laminas/laminas-coding-standard: ^2.3.0
- laminas/laminas-db: ^2.13.4
- laminas/laminas-developer-tools: ^2.1.1
- lm-commons/lmc-user: ^3.5.0
- phpunit/phpunit: ^9.5.9
README
This module is designed to provide a facade for Laminas\Permissions\Acl
that will
ease its usage with modules and applications. By default, it provides simple
setup via config files or by using Laminas\Db
or Doctrine ORM/ODM.
Information
This is a fork of the original bjyoungblood/BjyAuthorize module. I added Laminas support, so starting with release 2.0.0 the module works with Laminas. Releases from the 1.x series are still targeted at Zend Framework 2 and 3. If you found a bug, please report it, just pm me in gitter or open a PullRequest.
What does BjyAuthorize do?
BjyAuthorize adds event listeners to your application so that you have a "security" or "firewall" that disallows unauthorized access to your controllers or routes.
This is what a normal Laminas\Mvc
application workflow would look like:
And here's how it would look like with BjyAuthorize enabled:
Requirements
Installation
Composer
The suggested installation method is via composer:
composer require kokspflanze/bjy-authorize
Configuration
Following steps apply if you want to use LmcUser
with Laminas\Db
. If you want to use Doctrine ORM/ODM, you should
also check the doctrine documentation.
- Ensure that following modules are enabled in your
application.config.php
file in the this order:LmcUser
BjyAuthorize
- Import the SQL schema located in
./vendor/BjyAuthorize/data/schema.sql
. - Create a
./config/autoload/bjyauthorize.global.php
file and fill it with configuration variable values as described in the following annotated example.
Here is an annotated sample configuration file:
<?php return [ 'bjyauthorize' => [ // set the 'guest' role as default (must be defined in a role provider) 'default_role' => 'guest', /* this module uses a meta-role that inherits from any roles that should * be applied to the active user. the identity provider tells us which * roles the "identity role" should inherit from. * for LmcUser, this will be your default identity provider */ 'identity_provider' => \BjyAuthorize\Provider\Identity\LmcUserLaminasDb::class, /* If you only have a default role and an authenticated role, you can * use the 'AuthenticationIdentityProvider' to allow/restrict access * with the guards based on the state 'logged in' and 'not logged in'. * * 'default_role' => 'guest', // not authenticated * 'authenticated_role' => 'user', // authenticated * 'identity_provider' => \BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider::class, */ /* role providers simply provide a list of roles that should be inserted * into the Laminas\Acl instance. the module comes with two providers, one * to specify roles in a config file and one to load roles using a * Laminas\Db adapter. */ 'role_providers' => [ /* here, 'guest' and 'user are defined as top-level roles, with * 'admin' inheriting from user */ \BjyAuthorize\Provider\Role\Config::class => [ 'guest' => [], 'user' => ['children' => [ 'admin' => [], ]], ], // this will load roles from the user_role table in a database // format: user_role(role_id(varchar], parent(varchar)) \BjyAuthorize\Provider\Role\LaminasDb::class => [ 'table' => 'user_role', 'identifier_field_name' => 'id', 'role_id_field' => 'role_id', 'parent_role_field' => 'parent_id', ], // this will load roles from // the 'BjyAuthorize\Provider\Role\ObjectRepositoryProvider' service \BjyAuthorize\Provider\Role\ObjectRepositoryProvider::class => [ // class name of the entity representing the role 'role_entity_class' => 'My\Role\Entity', // service name of the object manager 'object_manager' => 'My\Doctrine\Common\Persistence\ObjectManager', ], ], // resource providers provide a list of resources that will be tracked // in the ACL. like roles, they can be hierarchical 'resource_providers' => [ \BjyAuthorize\Provider\Resource\Config::class => [ 'pants' => [], ], ], /* rules can be specified here with the format: * [roles (array), resource, privilege (array|string), assertion] * assertions will be loaded using the service manager and must implement * Laminas\Acl\Assertion\AssertionInterface. * *if you use assertions, define them using the service manager!* */ 'rule_providers' => [ \BjyAuthorize\Provider\Rule\Config::class => [ 'allow' => [ // allow guests and users (and admins, through inheritance) // the "wear" privilege on the resource "pants" [['guest', 'user'], 'pants', 'wear'], ], // Don't mix allow/deny rules if you are using role inheritance. // There are some weird bugs. 'deny' => [ // ... ], ], ], /* Currently, only controller and route guards exist * * Consider enabling either the controller or the route guard depending on your needs. */ 'guards' => [ /* If this guard is specified here (i.e. it is enabled], it will block * access to all controllers and actions unless they are specified here. * You may omit the 'action' index to allow access to the entire controller */ \BjyAuthorize\Guard\Controller::class => [ ['controller' => 'index', 'action' => 'index', 'roles' => ['guest','user']], ['controller' => 'index', 'action' => 'stuff', 'roles' => ['user']], // You can also specify an array of actions or an array of controllers (or both) // allow "guest" and "admin" to access actions "list" and "manage" on these "index", // "static" and "console" controllers [ 'controller' => ['index', 'static', 'console'], 'action' => ['list', 'manage'], 'roles' => ['guest', 'admin'], ], [ 'controller' => ['search', 'administration'], 'roles' => ['staffer', 'admin'], ], ['controller' => 'lmcuser', 'roles' => []], // Below is the default index action used by the LaminasSkeletonApplication // ['controller' => 'Application\Controller\Index', 'roles' => ['guest', 'user']], ], /* If this guard is specified here (i.e. it is enabled], it will block * access to all routes unless they are specified here. */ \BjyAuthorize\Guard\Route::class => [ ['route' => 'lmcuser', 'roles' => ['user']], ['route' => 'lmcuser/logout', 'roles' => ['user']], ['route' => 'lmcuser/login', 'roles' => ['guest']], ['route' => 'lmcuser/register', 'roles' => ['guest']], // Below is the default index action used by the LaminasSkeletonApplication ['route' => 'home', 'roles' => ['guest', 'user']], ], ], ], ];
Helpers and Plugins
There are view helpers and controller plugins registered for this module.
In either a controller or a view script, you can call
$this->isAllowed($resource[, $privilege])
, which will query the ACL
using the currently authenticated (or default) user's roles.
Whenever you need to stop processing your action you can throw an UnAuthorizedException and users will see you message on a 403 page.
function cafeAction() { if (!$this->isAllowed('alcohol', 'consume')) { throw new \BjyAuthorize\Exception\UnAuthorizedException('Grow a beard first!'); } // party on ... }
License
Released under the MIT License. See file LICENSE included with the source code for this project for a copy of the licensing terms.