download1323 2015-11-09 19:39
浏览 159
已采纳

在Laravel 5中为授权设置自定义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

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.

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)
    {

    }

}

My customAuthProvider.php file, in App/Providers:

<?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()
    {
        //
    }

}

At the end I set driver to custom in config/Auth.php

'driver' => 'custom'

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?

  • 写回答

2条回答 默认 最新

  • dpub33855 2015-11-09 22:30
    关注

    I tried this package and it helped me:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站