设置在授权5 Laravel自定义SQL [英] set custom sql for Authorization in Laravel 5

查看:271
本文介绍了设置在授权5 Laravel自定义SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel新的使用授权。我找了更改缺省SQL进行验证的方式。其实,Laravel做它用下面这个简单的SQL命令:

I am new on Laravel and use Authorization. I am looking for the way to change default sql for Auth. Actually, Laravel does it using this simple sql command at below:

SELECT * FROM users WHERE login="something" AND password = "something" LIMIT 1

我试图改变默认的SQL是这样的:

I am trying to change default sql like this:

SELECT
u.id, u.name, c.company
FROM
users u, companies c
WHERE 
u.login="something" AND 
u.password = "something" AND
u.companyId = c.id
LIMIT 1

我明白我应该创建自定义授权系统:箱子新用户提供与认证提供商

I understood that I should create custom Authorization system: crate new user Provider and Auth Provider.

首先,我创建了auth文件夹内的应用程序,并添加有 CustomUserProvider.php

Firstly, I created Auth folder inside App and added there CustomUserProvider.php

CustomUserProvider.php

<?php namespace App\Auth;

use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Auth\UserProvider as UserProviderInterface;
use App\Models\User;

class CustomUserProvider implements UserProviderInterface {

    protected $model;

    public function __construct(UserContract $model)
    {
        $this->model = $model;
    }

    public function retrieveById($identifier)
    {

    }

    public function retrieveByToken($identifier, $token)
    {

    }

    public function updateRememberToken(UserContract $user, $token)
    {

    }

    public function retrieveByCredentials(array $credentials)
    {

    }

    public function validateCredentials(UserContract $user, array $credentials)
    {

    }

}

我的 customAuthProvider.php 文件,在应用/供应商

<?php namespace App\Providers;

use App\Models\User;
use Illuminate\Support\Facades\Auth;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;

class CustomAuthProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app['auth']->extend('custom',function()
        {
            return new CustomUserProvider(new User);
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

}

最后我通过设置驱动程序自定义的配置/ Auth.php

'driver' => 'custom'

我要寻找我使用如何使用自定义的SQL命令授权(登录)这个自定义类的方式?
或者,也许这样是错的?

I am looking for the way using this custom classes how can I use custom sql command for Authorization (Login)? Or maybe this way is wrong?

推荐答案

我试过这个软件包,它帮助我:

I tried this package and it helped me:

https://github.com/ollieread/multiauth/

这篇关于设置在授权5 Laravel自定义SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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