bogardo/multiauth
Composer 安装命令:
composer require bogardo/multiauth
包简介
Laravel Auth driver with support for multiple Eloquent models
README 文档
README
Laravel authentication driver which enables you to use multiple Eloquent models for authentication.
This package supports Laravel's password reminders by default and needs no extra configuration.
Installation
Currently only compatible with Laravel 4.2
Support for Laravel 5 is on the roadmap
Install the package with composer
$ composer require bogardo\multiauth
Add the service provider
Add the following to your providers array in app/config/app.php
'Bogardo\Multiauth\MultiauthServiceProvider',
Configuration
Change the following in your app/config/auth.php file.
Set your driver to multiauth
'driver' => 'multiauth'
Add your multiauth settings
'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
- Add the
Bogardo\Multiauth\User\UserTrait(replace it if it is already present) - Add a public
$authtypeproperty. The value of the property should match thetypekey defined in themultiauthconfiguration.
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.
This package provides a convient validation rule to do exactly that.
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.
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.
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
typeof the user being updated - The
idof the user being updated
multiAuthUnique:user,2
Usage
Creating new record
Validator::make( ['email' => 'email@example.com'], // data array ['email' => 'multiAuthUnique'] // rules array );
Updating an existing Admin user with an id of 5
Validator::make( ['email' => 'email@example.com'], // data array ['email' => 'multiAuthUnique:administrator,5'] // rules array );
API
Get the Multiauth service
/** @var Bogardo\Multiauth\Service $service */ $service = App::make('multiauth.service');
Get a collection of all registered entities
The EntityCollection extends Laravel's Collection
$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
$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.
/** @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
Which you could use to restrict access to a route which should only be accessible for a specific user type.
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.
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
bogardo/multiauth 适用场景与选型建议
bogardo/multiauth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auth」 「laravel」 「multi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bogardo/multiauth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bogardo/multiauth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bogardo/multiauth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Theme and asset management for laravel
This package provides Laravel bindings and a Eloquent/Model trait for pragmarx/recovery package.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-15