A Doctrine ACL package for Laravel

A Doctrine ACL package for Laravel

README

A Doctrine ACL package for Laravel 5

  • Login with Doctrine User entity
  • User roles
  • User banning
  • User suspending
  • User permissions
  • User last login and last attempt event listeners
  • Role permissions

Installation

Include the service provider in config/app.php

'BrouwersLaravelDoctrineDoctrineServiceProvider',
'MaatwebsiteUsherUsherServiceProvider'

Config

To change the defaults of this package, publish the config:

php artisan vendor:publish --provider="MaatwebsiteUsherUsherServiceProvider"

Default usage

Out of the box, you can use the ACL system without defining your own entities. However this is not recommended!

Custom usage

For example if you want a Customer and Group entity, you just have to make sure it implements MaatwebsiteUsherContractsUsersUser. If you want a faster solution, you can optionally extend the MappedSuperclass MaatwebsiteUsherDomainUsersUser.
*Note that you will have to define the roles relation yourself.

Example with the MappedSuperclass:


use DoctrineORMMapping as ORM;
use MaatwebsiteUsherDomainUsersUser;
use MaatwebsiteUsherContractsUsersUser as UserInterface;

/**
 * @ORMEntity(repositoryClass="DoctrineCustomerRepository")
 * @ORMTable(name="customers")
 * @ORMHasLifecycleCallbacks()
 */
class Customer extends User implements UserInterface
{
    /**
     * @ORMManyToMany(targetEntity="Group", inversedBy="customers")
     * @ORMJoinTable(name="customer_groups")
     * @var ArrayCollection|AppDomainCustomersEntitiesRole
     */
    protected $groups;
    
    /**
     * Customer Constructor
     */
    public function __construct()
    {
        $this->groups = new ArrayCollection();
    }
    
    /**
     * @return ArrayCollection|MaatwebsiteUsherContractsRolesRole
     */
    public function getRoles()
    {
        return $this->groups;
    }
  }

Same as with the User MappedSuperclass, you’ll have to define the User relation yourself.

/**
 * @ORMEntity(repositoryClass="DoctrineRoleRepository")
 * @ORMTable(name="groups")
 * @ORMHasLifecycleCallbacks()
 */
class Group extends Role implements RoleInterface
{

    /**
     * @ORMManyToMany(targetEntity="Customer", mappedBy="groups")
     * @var ArrayCollection|Customer
     **/
    protected $customers;

    /**
     * Role Constructor
     */
    public function __construct()
    {
        $this->customers = new ArrayCollection();
    }

    /**
     * @return ArrayCollection|MaatwebsiteUsherContractsUsersUser
     */
    public function getUsers()
    {
        return $this->customers;
    }
}

Next you’ll have to update the class reference in config/usher.php for the user.entity en role.entity

return [
    'users'  => [
        'entity' => 'Customer'
    ],
    'roles'  => [
        'entity' => 'Group'
    ]
]

Events

Domain Events

Domain Event When
UserGotAssignedToRole $user->assignRole($role)
UserGotBanned $user->ban()
UserGotRemovedFromRole $user->removeRole($role)
UserGotSuspended $user->suspend($minutes)
UserRegistered $user->register()
UserUpdatedProfile $user->update()
RoleWasCreated $role->create()
RoleWasUpdated $role->update()

Event Listeners

Event listeners can be enabled and disabled inside the config. By default the listed listenes are all enabled.

Listener When
SaveLastAttemptDate auth.attempt
CheckIfUserIsBanned auth.attempt
CheckIfUserIsSuspended auth.attempt
SaveLastLoginDate auth.login

Source

Add a Comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *