有2个不同的表认证 [英] Authentication with 2 different tables
问题描述
我需要创建另一个表和用户一个新的权威性的配置。我对管理员的用户一个表,另一台为普通用户。
I need to create a new "auth" config with another table and users. I have one table for the "admin" users and another table for the normal users.
但我怎么可以创建验证的另一个实例
用不同的配置?
But how can I create another instance of Auth
with a different configuration?
推荐答案
您可以模拟一个新的验证类。
You can "emulate" a new Auth class.
Laravel验证部件基本照亮\\验证\\卫队
类,这个类有一定的相关性。
Laravel Auth component is basically the Illuminate\Auth\Guard
class, and this class have some dependencies.
所以,基本上,你必须创建一个新的卫队类和一些门面...
So, basically you have to create a new Guard class and some facades...
<?php
use Illuminate\Auth\Guard as AuthGuard;
class CilentGuard extends AuthGuard
{
public function getName()
{
return 'login_' . md5('ClientAuth');
}
public function getRecallerName()
{
return 'remember_' . md5('ClientAuth');
}
}
...添加的ServiceProvider
来初始化这个类,传递它的依赖。
... add a ServiceProvider
to initialize this class, passing it's dependencies.
<?php
use Illuminate\Support\ServiceProvider;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Auth\Reminders\PasswordBroker;
use Illuminate\Auth\Reminders\DatabaseReminderRepository;
use ClientGuard;
use ClientAuth;
class ClientServiceProvider extends ServiceProvider
{
public function register()
{
$this->registerAuth();
$this->registerReminders();
}
protected function registerAuth()
{
$this->registerClientCrypt();
$this->registerClientProvider();
$this->registerClientGuard();
}
protected function registerClientCrypt()
{
$this->app['client.auth.crypt'] = $this->app->share(function($app)
{
return new BcryptHasher;
});
}
protected function registerClientProvider()
{
$this->app['client.auth.provider'] = $this->app->share(function($app)
{
return new EloquentUserProvider(
$app['client.auth.crypt'],
'Client'
);
});
}
protected function registerClientGuard()
{
$this->app['client.auth'] = $this->app->share(function($app)
{
$guard = new Guard(
$app['client.auth.provider'],
$app['session.store']
);
$guard->setCookieJar($app['cookie']);
return $guard;
});
}
protected function registerReminders()
{
# DatabaseReminderRepository
$this->registerReminderDatabaseRepository();
# PasswordBroker
$this->app['client.reminder'] = $this->app->share(function($app)
{
return new PasswordBroker(
$app['client.reminder.repository'],
$app['client.auth.provider'],
$app['redirect'],
$app['mailer'],
'emails.client.reminder' // email template for the reminder
);
});
}
protected function registerReminderDatabaseRepository()
{
$this->app['client.reminder.repository'] = $this->app->share(function($app)
{
$connection = $app['db']->connection();
$table = 'client_reminders';
$key = $app['config']['app.key'];
return new DatabaseReminderRepository($connection, $table, $key);
});
}
public function provides()
{
return array(
'client.auth',
'client.auth.provider',
'client.auth.crypt',
'client.reminder.repository',
'client.reminder',
);
}
}
在此服务提供商,我把如何创建一个新密码提示组件一些例子。
In this Service Provider, I put some example of how to create a 'new' password reminder component to.
现在你需要创建两个新的门面,一个用于认证,一个密码提示。
Now you need to create two new facades, one for authentication and one for password reminders.
<?php
use Illuminate\Support\Facades\Facade;
class ClientAuth extends Facade
{
protected static function getFacadeAccessor()
{
return 'client.auth';
}
}
和...
<?php
use Illuminate\Support\Facades\Facade;
class ClientPassword extends Facade
{
protected static function getFacadeAccessor()
{
return 'client.reminder';
}
}
当然,对于密码提示,您需要创建数据库表,为了工作。在这个例子中,表名应该是 client_reminders
,你可以在服务提供商的 registerReminderDatabaseRepository
办法看到的。表结构是相同的原始提醒表
Of course, for password reminders, you need to create the table in database, in order to work. In this example, the table name should be client_reminders
, as you can see in the registerReminderDatabaseRepository
method in the Service Provider. The table structure is the same as the original reminders table.
在这之后,你可以用你的 ClientAuth
使用验证
类的方法相同。和 ClientPassword
同样的事情与密码
类。
After that, you can use your ClientAuth
the same way you use the Auth
class. And the same thing for ClientPassword
with the Password
class.
ClientAuth::gust();
ClientAuth::attempt(array('email' => $email, 'password' => $password));
ClientPassword::remind($credentials);
不要忘了您的服务提供商加入到应用程序/配置/ app.php
文件中的服务提供商名单。
Don't forget to add your service provider to the service providers list in the app/config/app.php
file.
更新:
如果您使用的是Laravel 4.1中,PasswordBroker不需要重定向
类了。
If you are using Laravel 4.1, the PasswordBroker doesn't need the Redirect
class anymore.
return new PasswordBroker(
$app['client.reminder.repository'],
$app['client.auth.provider'],
$app['mailer'],
'emails.client.reminder' // email template for the reminder
);
更新2
Laravel 5.2刚刚推出多权威性,所以这里不再在这个版本需要
Laravel 5.2 just introduced multi auth, so this is no longer needed in this version.
这篇关于有2个不同的表认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!