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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路