2017 © Pedro Peláez
 

library multiauth

Laravel Auth driver with support for multiple Eloquent models

image

bogardo/multiauth

Laravel Auth driver with support for multiple Eloquent models

  • Monday, February 16, 2015
  • by Bogardo
  • Repository
  • 1 Watchers
  • 6 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Bogardo\Multiauth

Latest Stable Version MIT License Build Status Coverage Status Packagist Downloads Total Packagist Downloads Month, (*1)

Laravel authentication driver which enables you to use multiple Eloquent models for authentication., (*2)

This package supports Laravel's password reminders by default and needs no extra configuration., (*3)

Installation

Currently only compatible with Laravel 4.2
Support for Laravel 5 is on the roadmap, (*4)

Install the package with composer

$ composer require bogardo\multiauth, (*5)

Add the service provider

Add the following to your providers array in app/config/app.php, (*6)

'Bogardo\Multiauth\MultiauthServiceProvider',, (*7)

Configuration

Change the following in your app/config/auth.php file., (*8)

Set your driver to multiauth, (*9)

'driver'    => 'multiauth'

Add your multiauth settings, (*10)

'multiauth' => [

    'identifier_key' => 'email',

    'entities' => [
        [
            'type'       => 'user',
            'table'      => 'users',
            'model'      => 'User',
            'identifier' => 'username'
        ],
        [
            'type'       => 'administrator',
            'table'      => 'admins',
            'model'      => 'Admin',
            'identifier' => 'email'
        ]
    ]
]

Usage

Prepare your models

There are 2 things that must be changed in your models
1. Add the Bogardo\Multiauth\User\UserTrait (replace it if it is already present)
2. Add a public $authtype property. The value of the property should match the type key defined in the multiauth configuration., (*11)

Examples

User model
use Bogardo\Multiauth\User\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface
{

    use UserTrait, RemindableTrait;

    public $authtype = 'user';

}
Admin model
use Bogardo\Multiauth\User\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Admin extends Eloquent implements UserInterface, RemindableInterface
{

    use UserTrait, RemindableTrait;

    public $authtype = 'administrator';

}

Validation

When working with just one table for users you'd normally only check for unique emails/usernames in your users table. But when you're working with multiple tables you have to make sure that your emails/usernames are unique on all relevant tables., (*12)

This package provides a convient validation rule to do exactly that., (*13)

It will check if the supplied value (email/username) exists on any of the tables defined in the multiauth configuration. It will take note of the identifier (email/username/custom) defined in the configuration., (*14)

When using the rule without extra parameters it will make sure the supplied value does not exist in any of the tables. You'd probably use this for creating new users., (*15)

multiAuthUnique

When updating an existing user you'd want to check if the supplied value is unique except for the record you are updating. You can do this by supplying two parameters: - The type of the user being updated - The id of the user being updated, (*16)

multiAuthUnique:user,2

Usage

Creating new record, (*17)

Validator::make(
    ['email' => 'email@example.com'], // data array
    ['email' => 'multiAuthUnique']    // rules array
);

Updating an existing Admin user with an id of 5, (*18)

Validator::make(
    ['email' => 'email@example.com'],              // data array
    ['email' => 'multiAuthUnique:administrator,5'] // rules array
);

API

Get the Multiauth service, (*19)

/** @var Bogardo\Multiauth\Service $service */
$service = App::make('multiauth.service');

Get a collection of all registered entities The EntityCollection extends Laravel's Collection, (*20)

$entities = $service->getEntities();

// $entities Output
object(Bogardo\Multiauth\Entity\EntityCollection)
  protected 'items' => 
    array (size=2)
       0 => 
        object(Bogardo\Multiauth\Entity\Entity)
          public 'type' => string 'user'
          public 'table' => string 'users'
          public 'model' => string 'User'
          public 'identifier' => string 'username'
      1 => 
        object(Bogardo\Multiauth\Entity\Entity)
          public 'type' => string 'administator'
          public 'table' => string 'admins'
          public 'model' => string 'Admin'
          public 'identifier' => string 'email'


Get an entity by type, (*21)

$entity = $service->getEntityByType('user');

// $entity Output
object(Bogardo\Multiauth\Entity\Entity)
  public 'type' => string 'user'
  public 'table' => string 'users'
  public 'model' => string 'User'
  public 'identifier' => string 'username'


Filters example implementation

You could use this, for example, to create custom route filters for each user type., (*22)


/** @var Bogardo\Multiauth\Service $service */ $service = App::make('multiauth.service'); $types = $service->getEntities()->lists('type'); foreach ($types as $type) { Route::filter('multiauth.' . $type, function($route, $request) use ($type) { if (Auth::guest()) { // The user is not logged in. return Redirect::to('/'); } if (Auth::user()->authtype !== $type) { // The user is logged in, but is not of correct type return Redirect::to('/'); } }); }

This will create a route filter for every registered user type: - user: multiauth.user - administrator: multiauth.administrator, (*23)

Which you could use to restrict access to a route which should only be accessible for a specific user type., (*24)

Route::get('admin', ['before' => 'multiauth.administrator', function() {
    echo 'Admins Only!!';
}]);

FAQ

- I'm unable to login using the loginUsingId and onceUsingId methods

The loginUsingId() and onceUsingId() methods should be passed the type and the id of the user, separated by a dot. This deviates from the default usage, where you'd only have to pass the ID of the user., (*25)

Auth::loginUsingId('administrator.2');

Auth::onceUsingId('user.54')

Changelog

v0.2.0

  • Added Unit Tests
  • Moved Validator to it's own class
  • Added Badges
  • Added Coverall

v0.1.0

  • Multiauth implementation
  • Updated docs (formatting)

v0.0.1

  • Initial setup with just documentation

Todo

  • Add support for Laravel 5

The Versions

16/02 2015

dev-master

9999999-dev

Laravel Auth driver with support for multiple Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel auth multi

16/02 2015

v0.2.0

0.2.0.0

Laravel Auth driver with support for multiple Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel auth multi

15/02 2015

v0.1.0

0.1.0.0

Laravel Auth driver with support for multiple Eloquent models

  Sources   Download

MIT

The Requires

 

laravel auth multi