独自の認証ドライバーを利用する
extension
悩み事
Laravelで用意されている認証ドライバーが、現在の仕様に合っていない等、
独自のドライバーを作成して利用したい
解決方法
Laravelを拡張して独自のドライバーを作成します
Step 1 - UserProviderInterfaceを実装する
まず、認証を処理するクラスを作成しなければなりません
ここではサンプルとしてランダムに資格情報を検証して、
50%をダミーユーザーとして返却するものを紹介します
<?php
namespace MyApp\Extensions;
use Illuminate\Auth\GenericUser;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserProviderInterface;
class DummyAuthProvider implements UserProviderInterface
{
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $id
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveById($id)
{
// 50% of the time return our dummy user
if (mt_rand(1, 100) <= 50) {
return $this->dummyUser();
}
// 50% of the time, fail
return null;
}
/**
* Retrieve a user by the given credentials.
* DO NOT TEST PASSWORD HERE!
*
* @param array $credentials
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveByCredentials(array $credentials)
{
// 50% of the time return our dummy user
if (mt_rand(1, 100) <= 50)
{
return $this->dummyUser();
}
// 50% of the time, fail
return null;
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Auth\UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
{
// we'll assume if a user was retrieved, it's good
return true;
}
/**
* Return a generic fake user
*/
protected function dummyUser()
{
$attributes = [
'id' = 123,
'username' => 'chuckles',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
];
return new GenericUser($attributes);
}
/**
* Needed by Laravel 4.1.26 and above
*/
public function retrieveByToken($identifier, $token)
{
return new \Exception('not implemented');
}
/**
* Needed by Laravel 4.1.26 and above
*/
public function updateRememberToken(UserInterface $user, $token)
{
return new \Exception('not implemented');
}
}
Step 2 - Authコンポーネントを拡張する
サービスプロバイダーか、app/start/global.php
に次の様に追加します
\Auth::extend('dummy', function($app) {
return new \MyApp\Extensions\DummyAuthProvider;
});
Step 3 - authドライバーを変更する
app/config/auth.php
のdriverを変更しましょう
'driver' => 'dummy',
アドバイス
この例はあり得ない例ですが、独自認証ドライバーの基本的な追加方法を踏まえています
このレシピサイトも独自のドライバーを使っています
Laravel.JpRecipe
またデータベースなども拡張して、
標準に含まれていないものを使って、拡張して作成する事もできますので参考にしてみましょう
Author:Chuck Heintzelman
Editor and Translator:Yuuki Takezawa
Category
- App 29
- Artisan 28
- Auth 36
- Basic Development 4
- Blade 23
- Cache 25
- Config 5
- Configuration 12
- Controller 3
- Cookie 2
- Core Extension 7
- Crypt 6
- DB 4
- Database Configuration 3
- Eloquent 0
- File 26
- Form 30
- Hash 1
- Help 2
- Html 17
- Installation 13
- Lang 6
- Middleware 2
- Paginator 1
- Route 1
- Session 0
- Solution 2
- Service Provider 1
- Testing 2
- Packages by 3rd Parties 0