如何在Laravel中使用多个数据库表(Laravel本机和其他应用程序'legacy')进行登录?

问题描述 投票:-1回答:1

我非常喜欢Laravel(这很重要)))

我的应用程序应该使用现有数据库中的数据(只读)。所以我将两个数据库连接到我的应用程序,一个用于Laravel,第二个用于读取数据。我需要为第二个数据库中存在的用户进行身份验证,并将其保存在“本机”Laravel'用户'表中。例如。:

  • 用户尝试登录
  • 应用程序在Laravel DB中的Laravel本机“用户”表中搜索用户凭据
  • 如果用户存在,请登录
  • 此外,应用程序在“Legacy”DB中搜索用户凭据(重要!'Legacy'应用程序使用md5进行密码散列)
  • 如果用户存在于“遗留”数据库中,请将用户凭据保存在Laravel DB中并登录
  • 否则会引发错误

在我的DEV中,DB以下列方式连接

.env

...
DB_CONNECTION=sqlite
DB_DATABASE=/path/to/app/db.sqlite

DB_CONNECTION_LEGACY=sqlite
DB_DATABASE_LEGACY=/path/to/app/legacy.sqlite
...

config/database.php

...
'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],

    'sqlite_legacy' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE_LEGACY', database_path('database.sqlite')),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],
...

请帮我。我被卡住了。

对不起英语不好,这不是我的母语。

laravel authentication laravel-5 laravel-5.7
1个回答
0
投票

完成。

为我的旧数据库创建模型:

php artisan make:model OldUsers

app\OldUsers.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class eboard_user extends Model
{
    /**
     * Connection for old 'users' table
     *
     * @var string
     */

    protected $connection = 'sqlite_legacy';

    /**
     * The table associated with the model.
     *
     * @var string
     */

    protected $table = 'users';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */

    public $timestamps = false;

    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */

    protected $dateFormat = 'U';
}

app/Http/Controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\OldUsers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        } else {

        $checkedUser = OldUsers::whereEmail($request->input('email'))->wherePass(md5($request->input('password')))->whereStatus('2')->first(); //Selecting user from old DB via needed criteria
        if ($checkedUser !== null) { //If old user exists:
            $newUser = new User; //Create Laravel native user
            $newUser->name = $checkedUser->name;
            $newUser->email = $checkedUser->email;
            $newUser->password = Hash::make($request->input('password'));
            $newUser->save();

            if ($this->attemptLogin($request)) {
                return $this->sendLoginResponse($request);
            }
        }
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.