使用 2 个不同的表进行身份验证 [英] Authentication with 2 different tables

查看:28
本文介绍了使用 2 个不同的表进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用另一个表和用户创建一个新的身份验证"配置.我有一张表供管理员"用户使用,另一张表供普通用户使用.

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.

但是如何使用不同的配置创建另一个 Auth 实例?

But how can I create another instance of Auth with a different configuration?

推荐答案

您可以模拟"一个新的 Auth 类.

You can "emulate" a new Auth class.

Laravel Auth 组件基本上就是IlluminateAuthGuard 类,这个类有一些依赖.

Laravel Auth component is basically the IlluminateAuthGuard class, and this class have some dependencies.

所以,基本上你必须创建一个新的 Guard 类和一些外观......

So, basically you have to create a new Guard class and some facades...

<?php 
use IlluminateAuthGuard 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 IlluminateSupportServiceProvider;
use IlluminateAuthEloquentUserProvider;
use IlluminateHashingBcryptHasher;
use IlluminateAuthRemindersPasswordBroker;
use IlluminateAuthRemindersDatabaseReminderRepository;
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 IlluminateSupportFacadesFacade;

class ClientAuth extends Facade
{

    protected static function getFacadeAccessor() 
    {
        return 'client.auth';
    }
}

还有……

<?php 
use IlluminateSupportFacadesFacade;

class ClientPassword extends Facade
{

    protected static function getFacadeAccessor() 
    {
        return 'client.reminder';
    }
}

当然,对于密码提醒,你需要在数据库中创建表,才能工作.在这个例子中,表名应该是 client_reminders,你可以在 Service Provider 的 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.

之后,您可以像使用 Auth 类一样使用 ClientAuth.对于 ClientPasswordPassword 类也一样.

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/config/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 不再需要 Redirect 类.

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 刚刚引入了multi auth,所以这个版本不再需要了.

Laravel 5.2 just introduced multi auth, so this is no longer needed in this version.

这篇关于使用 2 个不同的表进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆