使用2个不同的表进行身份验证

卡洛斯·戈斯(Carlos Goce)

我需要使用另一个表和用户创建一个新的“ auth”配置。我为“ admin”用户提供了一个表,为普通用户提供了另一个表。

但是,如何创建Auth具有不同配置的的另一个实例

路易斯·达莫林

您可以“模拟”新的Auth类。

Laravel Auth组件基本上是Illuminate\Auth\Guard该类,并且该类具有一些依赖关系。

因此,基本上,您必须创建一个新的Guard类和一些外墙...

<?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以初始化此类,并传递其依赖项。

<?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', 
        );
    }
}

在该服务提供商中,我举了一些示例,说明如何向其中创建“新”密码提醒组件。

现在,您需要创建两个新的外观,一个用于身份验证,另一个用于密码提醒。

<?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在服务提供者中的方法中所见该表的结构与原始提醒表相同。

之后,您可以使用ClientAuth与使用Auth相同的方式而对于同样的事情ClientPasswordPassword类。

ClientAuth::gust();
ClientAuth::attempt(array('email' => $email, 'password' => $password));

ClientPassword::remind($credentials);

不要忘记将服务提供商添加到app/config/app.php文件中的服务提供商列表中

更新:

如果您使用的是Laravel 4.1,则PasswordBroker不再需要Redirect该类。

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,因此在此版本中不再需要此功能。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用2个不同的表进行身份验证

来自分类Dev

在laravel 4使用两个不同的模型和表进行身份验证系统?

来自分类Dev

如何使用身份服务器 3 对 2 个不同的 mvc 应用程序进行身份验证

来自分类Dev

针对两个不同的数据库表进行身份验证

来自分类Dev

Symfony 2 Swiftmailer + Amazon SES:无法使用2个可能的身份验证器在用户名“ XXXXXXXXXXXXXXXX”的SMTP服务器上进行身份验证

来自分类Dev

无法使用2个可能的身份验证器使用用户名在SMTP服务器上进行身份验证

来自分类Dev

使用AFHTTPRequestOperation进行AFNetworking 2身份验证

来自分类Dev

在iOS上使用OAuth2进行身份验证

来自分类Dev

在iOS上使用OAuth2进行身份验证

来自分类Dev

使用OAuth2对Google App Engine Java中不同服务提供商的用户进行身份验证

来自分类Dev

使用AES进行DESFire身份验证

来自分类Dev

使用Glass进行身份验证

来自分类Dev

使用cURL进行设备身份验证

来自分类Dev

使用REST进行远程身份验证

来自分类Dev

使用JGit的PullCommand进行身份验证

来自分类Dev

使用Play进行身份验证!2.4

来自分类Dev

使用JWT进行身份验证

来自分类Dev

使用Wordpress进行.htaccess身份验证

来自分类Dev

使用gundb进行JWT身份验证

来自分类Dev

使用Firebase进行Shopify身份验证?

来自分类Dev

匿名使用Firebase进行身份验证

来自分类Dev

使用docker进行PyMongo身份验证

来自分类Dev

无法使用git进行身份验证

来自分类Dev

使用ldapbind进行身份验证

来自分类Dev

如何使用HttpURLconnection进行身份验证

来自分类Dev

使用硒进行身份验证(Python)

来自分类Dev

使用REST进行远程身份验证

来自分类Dev

使用部分密码进行身份验证

来自分类Dev

仅使用Pamusb进行身份验证

Related 相关文章

热门标签

归档