Skip to content

Commit

Permalink
Merge pull request #12 from visto9259/master
Browse files Browse the repository at this point in the history
Updated documentation
  • Loading branch information
visto9259 authored Jan 14, 2021
2 parents 28cc304 + 4f6e914 commit c2b1a33
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 55 deletions.
4 changes: 1 addition & 3 deletions docs/01. Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ perform specific actions.
## What is the Rbac model?

Rbac stands for **role-based access control**. We use a very simple (albeit powerful) implementation of this model
through the use of [this PHP library](https://github.com/zf-fr/rbac).
through the use of the [zf-fr/rbac](https://github.com/zf-fr/rbac) library.

> We are not using the official ZF2 Rbac component since ZfcRbac 2.0 as it has some design flaws. The library we are
using here is actually a prototype for ZF3 Rbac component I've made specifically for ZfcRbac.

The basic idea of Rbac is to use roles and permissions:

Expand Down
2 changes: 1 addition & 1 deletion docs/02. Quick Start.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ return [
]
];
```
The identity given by `Laminas\Authentication\AuthenticationService` must implement `LmcRbacMvc\Identity\IdentityInterface`. Note that the default identity provided with Laminas does not implement this interface, neither does the ZfcUser suite.
The identity given by `Laminas\Authentication\AuthenticationService` must implement `LmcRbacMvc\Identity\IdentityInterface`. Note that the default identity provided with Laminas does not implement this interface, neither does the LmcUser suite.

LmcRbacMvc is flexible enough to use something other than the built-in `AuthenticationService`, by specifying custom
identity providers. For more information, refer [to this section](03.%20Role%20providers.md#identity-providers).
Expand Down
6 changes: 3 additions & 3 deletions docs/03. Role providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ If you are more confident with flat RBAC, the previous config can be re-written

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'role_provider' => [
'LmcRbacMvc\Role\InMemoryRoleProvider' => [
'admin' => [
Expand Down Expand Up @@ -169,7 +169,7 @@ Then, you need to add it to the role provider manager:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'role_provider_manager' => [
'factories' => [
'Application\Role\CustomRoleProvider' => 'Application\Factory\CustomRoleProviderFactory'
Expand All @@ -183,7 +183,7 @@ You can now use it like any other role provider:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'role_provider' => [
'Application\Role\CustomRoleProvider' => [
// Options
Expand Down
35 changes: 12 additions & 23 deletions docs/04. Guards.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ In this section, you will learn:

## What are guards and when should you use them?

Guards (called firewalls in older versions of ZfcRbac) are listeners that are registered on a specific event of
Guards are listeners that are registered on a specific event of
the MVC workflow. They allow your application to quickly mark a request as unauthorized.

Here is a simple workflow without guards:
Expand Down Expand Up @@ -72,7 +72,7 @@ All guards must be added in the `guards` subkey:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'guards' => [
// Guards config here!
]
Expand Down Expand Up @@ -351,7 +351,7 @@ The configuration rules are the same as for ControllerGuard.

RouteGuard and ControllerGuard listen to the `MvcEvent::EVENT_ROUTE` event. Therefore, if you use the
`forward` method in your controller, those guards will not intercept and check requests (because internally
ZF2 does not trigger again a new MVC loop).
Laminas MVC does not trigger again a new MVC loop).

Most of the time, this is not an issue, but you must be aware of it, and this is an additional reason why you
should always protect your services too.
Expand Down Expand Up @@ -437,37 +437,25 @@ Now, let's create the factory:
namespace Application\Factory;

use Application\Guard\IpGuard;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\MutableCreationOptionsInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;

class IpGuardFactory implements FactoryInterface, MutableCreationOptionsInterface
class IpGuardFactory implements FactoryInterface
{
/**
* @var array
*/
protected $options;

/**
* {@inheritDoc}
*/
public function setCreationOptions(array $options)
{
$this->options = $options;
}

/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new IpGuard($this->options);
if (null === $options) {
$options = [];
}
return new IpGuard($options);
}
}
```

The `MutableCreationOptionsInterface` was introduced in Zend Framework 2.2, and allows your factories to accept
options. In fact, in a real use case, you would likely fetched the blacklist from a database.
In a real use case, you would likely fetched the blacklist from a database.

Now we just need to add the guard to the `guards` option, so that LmcRbacMvc can execute the logic behind this guard. In
your config, add the following code:
Expand All @@ -484,6 +472,7 @@ return [
]
];
```
The array of IP addresses will be passed to `IpGuardFactory::__invoke` in the `$options` parameter.

### Navigation

Expand Down
6 changes: 3 additions & 3 deletions docs/05. Strategies.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ You can configure the strategy using the `redirect_strategy` subkey:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'redirect_strategy' => [
'redirect_when_connected' => true,
'redirect_to_route_connected' => 'home',
Expand Down Expand Up @@ -95,7 +95,7 @@ You can configure the strategy using the `unauthorized_strategy` subkey:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'unauthorized_strategy' => [
'template' => 'error/custom-403'
],
Expand All @@ -108,7 +108,7 @@ return [
## Creating custom strategies

Creating a custom strategy is rather easy. Let's say we want to create a strategy that integrates with
the [ApiProblem](https://github.com/laminas-api-tools/api-tools-api-problem) Zend Framework 2 module:
the [ApiProblem](https://github.com/laminas-api-tools/api-tools-api-problem) Laminas Api Tools module:

```php
namespace Application\View\Strategy;
Expand Down
13 changes: 6 additions & 7 deletions docs/06. Using the Authorization Service.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class Module
### Using delegator factory

LmcRbacMvc is shipped with a `LmcRbacMvc\Factory\AuthorizationServiceDelegatorFactory` [delegator factory]
(http://framework.zend.com/manual/2.3/en/modules/zend.service-manager.delegator-factories.html)
LmcRbacMvc is shipped with a `LmcRbacMvc\Factory\AuthorizationServiceDelegatorFactory` [delegator factory](https://docs.laminas.dev/laminas-servicemanager/delegators/)
to automatically inject the authorization service into your classes.

As for the initializer, the class must implement the `AuthorizationServiceAwareInterface`.
Expand Down Expand Up @@ -86,7 +85,7 @@ class Module
### Using Factories

You can inject the AuthorizationService into your factories by using Zend's ServiceManager. The AuthorizationService
You can inject the AuthorizationService into your factories by using Laminas' ServiceManager. The AuthorizationService
is known to the ServiceManager as `'LmcRbacMvc\Service\AuthorizationService'`. Here is a classic example for injecting
the AuthorizationService:

Expand All @@ -111,10 +110,10 @@ class Module
}
```

### Using Zend\DI
### Using Laminas\DI

DI is a great way for prototyping, getting results *fast* and maintaining a flexible structure. However it adds overhead and can get very slow. Unless you are using a compiler it is **not** recommended for production.
Here's how you enable Zend\DI to inject the AuthorizationService in MyClass:
Here's how you enable Laminas\DI to inject the AuthorizationService in MyClass:

*YourModule/Module.php*

Expand Down Expand Up @@ -159,7 +158,7 @@ by adding this to your `module.config.php` file:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'assertion_manager' => [
'factories' => [
'MyAssertion' => 'MyAssertionFactory'
Expand All @@ -177,7 +176,7 @@ adding this to your `module.config.php` file:

```php
return [
'zfc_rbac' => [
'lmc_rbac' => [
'assertion_map' => [
'myPermission' => 'myAssertion'
]
Expand Down
17 changes: 8 additions & 9 deletions docs/07. Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ This section will help you further understand how LmcRbacMvc works by providing
any other recipe you'd like to add, please open an issue!

- [A Real World Application](07.%20Cookbook.md#a-real-world-application)
- [Best Practices](https://github.com/manuakasam/zfc-rbac/blob/master07.%20Cookbook.md#best-practices)
- [When to use Guards](07.%20Cookbook.md#when-using-guards-then)
- [A Real World Application Part 2 - Only delete your own Posts](07.%20Cookbook.md#a-real-world-application-part-2---only-delete-your-own-posts)
- [A Real World Application Part 3 - But Admins can delete everything](07.%20Cookbook.md#a-real-world-application-part-3---admins-can-delete-everything)
- [A Real World Application Part 4 - Only admins have the menu item for managing the posts](07.%20Cookbook.md#user-content-a-real-world-application-part-4---checking-permissions-in-the-view)
- [Using LmcRbacMvc with Doctrine ORM](07.%20Cookbook.md#using-lmcrbac-with-doctrine-orm)
- [How to deal with roles with lot of permissions?](07.%20Cookbook.md#how-to-deal-with-roles-with-lot-of-permissions)
- [Using LmcRbacMvc and ZF2 Assetic](07.%20Cookbook.md#using-lmcrbac-and-zf2-assetic)
- [Using LmcRbacMvc and ZF2 Assetic](07.%20Cookbook.md#using-lmcrbacmvc-and-zf2-assetic)
- [Using LmcRbacMvc and LmcUser](07.%20Cookbook.md#using-lmcrbacmvc-and-lmcuser)

## A Real World Application
Expand Down Expand Up @@ -370,7 +369,7 @@ called `assertion_map`. In this map we glue `assertions` and `permissions` toget
```php
// module.config.php or wherever you configure your RBAC stuff
return [
'zfc_rbac' => [
'lmc_rbac' => [
'assertion_map' => [
'deletePost' => 'Your\Namespace\MustBeAuthorAssertion'
]
Expand All @@ -386,7 +385,7 @@ shown below:
```php
// module.config.php or wherever you configure your RBAC stuff
return [
'zfc_rbac' => [
'lmc_rbac' => [
// ... other rbac stuff
'assertion_manager' => [
'factories' => [
Expand Down Expand Up @@ -518,7 +517,7 @@ In this last example, the menu item will be hidden for users who don't have the
First your User entity class must implement `LmcRbacMvc\Identity\IdentityInterface` :

```php
use ZfcUser\Entity\User as ZfcUserEntity;
use LmccUser\Entity\User as LmcUserEntity;
use LmcRbacMvc\Identity\IdentityInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
Expand All @@ -528,7 +527,7 @@ use Doctrine\Common\Collections\Collection;
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User extends ZfcUserEntity implements IdentityInterface
class User extends LmcUserEntity implements IdentityInterface
{
/**
* @var Collection
Expand Down Expand Up @@ -664,7 +663,7 @@ And the last step is to create a Permission entity class which is a very simple

You can find all entity examples in this folder : [Example](/data)

You need one more configuration step. Indeed, how can the RoleProvider retrieve your role and permissions? For this you need to configure `LmcRbacMvc\Role\ObjectRepositoryRoleProvider` in your `zfc_rbac.global.php` file :
You need one more configuration step. Indeed, how can the RoleProvider retrieve your role and permissions? For this you need to configure `LmcRbacMvc\Role\ObjectRepositoryRoleProvider` in your `lmc_rbac.global.php` file :
```php
/**
* Configuration for role provider
Expand Down Expand Up @@ -746,7 +745,7 @@ return [

## Using LmcRbacMvc and LmcUser

To use the authentication service from ZfcUser, just add the following alias in your application.config.php:
To use the authentication service from LmcUser, just add the following alias in your application.config.php:

```php
return [
Expand All @@ -758,7 +757,7 @@ return [
];
```

Finally add the ZfcUser routes to your `guards`:
Finally add the LmcUser routes to your `guards`:

```php
return [
Expand Down
10 changes: 4 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
_Up-to-date with version 3.* of LmcRbacMvc

Welcome to the official LmcRbacMvc documentation. This documentation will help you quickly understand how to use
and extend LmcRbacMvc.

Expand All @@ -8,7 +6,7 @@ If you are looking for some information that is not listed in the documentation,
1. [Introduction](01.%20Introduction.md)
1. [Why should I use an authorization module?](01.%20Introduction.md#why-should-i-use-an-authorization-module)
2. [What is the Rbac model?](01.%20Introduction.md#what-is-the-rbac-model)
3. [How can I integrate LmcRbacMvc into my application?](01.%20Introduction.md#how-can-i-integrate-zfcrbac-into-my-application)
3. [How can I integrate LmcRbacMvc into my application?](01.%20Introduction.md#how-can-i-integrate-lmcrbacmvc-into-my-application)

2. [Quick Start](02.%20Quick%20Start.md)
1. [Specifying an identity provider](02.%20Quick%20Start.md#specifying-an-identity-provider)
Expand All @@ -24,7 +22,7 @@ If you are looking for some information that is not listed in the documentation,
4. [Creating custom role providers](03.%20Role%20providers.md#creating-custom-role-providers)

4. [Guards](04.%20Guards.md)
1. [What are guards and when to use them?](04.%20Guards.md#what-are-guards-and-when-to-use-them)
1. [What are guards and when to use them?](04.%20Guards.md#what-are-guards-and-when-should-you-use-them)
2. [Built-in guards](04.%20Guards.md#built-in-guards)
3. [Creating custom guards](04.%20Guards.md#creating-custom-guards)

Expand All @@ -44,7 +42,7 @@ If you are looking for some information that is not listed in the documentation,
7. [Cookbook](07.%20Cookbook.md)
1. [A real world example](07.%20Cookbook.md#a-real-world-application)
2. [Best practices](07.%20Cookbook.md#best-practices)
3. [Using LmcRbacMvc with Doctrine ORM](07.%20Cookbook.md#using-zfcrbac-with-doctrine-orm)
3. [Using LmcRbacMvc with Doctrine ORM](07.%20Cookbook.md#using-lmcrbacmvc-with-doctrine-orm)
4. [How to deal with roles with lot of permissions?](07.%20Cookbook.md#how-to-deal-with-roles-with-lot-of-permissions)
5. [Using LmcRbacMvc and ZF2 Assetic](07.%20Cookbook.md#using-zfcrbac-and-zf2-assetic)
5. [Using LmcRbacMvc and ZF2 Assetic](07.%20Cookbook.md#using-lmcrbacmvc-and-zf2-assetic)
6. [Using LmcRbacMvc and LmcUser](07.%20Cookbook.md#using-lmcrbacmvc-and-lmcuser)

0 comments on commit c2b1a33

Please sign in to comment.