set custom sql for Authorization in Laravel 5

2019-09-18 15:27发布

问题:

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?

回答1:

If all you need are additional constraints on the query that fetches user from the database during authentication, there is much simpler way to do that.

First of all, Laravel offers an AuthenticatesUsers trait that you can use in your controller to handle authentication requests. The default implementation fetches user from the database using username field and then, if matching user is found, it validates their password.

The list of attributes that is used to fetch user from the database can be customized by overriding getCredentials method in your controller. In your case the following should be enough to load user using their username and company id:

protected function getCredentials(Request $request)
{
    return $request->only($this->loginUsername(), 'password', 'companyId);
}

Once you add that, user should provide their username, companyId and password in the login form and they will be authenticated only if there exists a user with given username that belongs to given company and the password provided is valid.

UPDATE: If you decide not to use the trait, but want to authenticate users manually, you can do so in a really similar manner. When calling Auth::attempt() you just need to pass all the criteria that should be used to authenticate the user, e.g.:

Auth::attempt([
  'username' => Input::get('username'), 
  'companyId' => Input::get('companyId'), 
  'password' => Input::get('password')
]);


回答2:

I tried this package and it helped me:

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