简体   繁体   English

如何在Laravel中使用SQL Server数据库登录凭据作为用户登录

[英]How to use SQL Server database login cridentials as user login in laravel

My client has a large SQL Server database which stores various forms of data for 20 companies. 我的客户有一个大型SQL Server数据库,该数据库存储了20家公司的各种形式的数据。 And each of these companies have access to respective sections of the database, by using their database login cridentials. 这些公司中的每一个都可以使用其数据库登录凭据来访问数据库的各个部分。

To make things clear, i do not have a user table. 为了清楚起见,我没有用户表。 But instead, each user is actually a database user . 但是,每个用户实际上是一个数据库用户

Right now, i am using a custom MVC of my own, and it is working very well. 现在,我正在使用自己的自定义MVC ,并且运行良好。 Like so : 像这样:

$user = $_POST['login']
$pass = Hash::create('sha256', $_POST['password'], HASH_PASSWORD_KEY);

        try {
            $conn = new PDO(DB_TYPE . ':server=' . DB_HOST . ',1433 ;Database=' . DB_NAME, $user,  $pass);
            $baglantiDurumu = "baglandi";
        }

As this project is growing bigger, i thought it is time to switch to Laravel , so that i can benefit from the functionalities of it. 随着该项目的规模不断扩大,我认为现在是时候改用Laravel了 ,这样我就可以从它的功能中受益。 But i have no idea how to achieve this in Laravel. 但是我不知道如何在Laravel中实现这一目标。

I will need this user/password to use in the connection string (database.php) eg: 我将需要此用户名/密码在连接字符串(database.php)中使用,例如:

    'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'xxx.xxx.xxx.xxx'),
        'port' => env('DB_PORT', '1433'),
        'database' => env('DB_DATABASE', 'TestDb-3'),
        'username' => env('DB_USERNAME', $user),
        'password' => env('DB_PASSWORD', $pass),
        'charset' => 'utf8',
        'prefix' => '',
    ],

I assume, this alone is not enough to solve my problem, because in order to use Laravel's other functionalities such as "Auth" , "Session" and all the other classes related with user login, i need a user database. 我认为,仅凭这还不足以解决我的问题,因为为了使用Laravel的其他功能(例如"Auth""Session"以及与用户登录相关的所有其他类),我需要一个用户数据库。 Which unfortunately i don't have. 不幸的是我没有。

By the way, forgot to mention that all the users including me have only read privillage. 顺便说一句,忘了提到包括我在内的所有用户都只阅读特权。 No INSERT , no UPDATE , just SELECT . 没有INSERT ,没有UPDATE ,只有SELECT

Do you have any suggestions, or should i go back to my old MVC? 您有什么建议吗,还是我应该回到以前的MVC?

Try this: 尝试这个:

config/database.php 配置/ database.php中

'sqlsrv' => [
    'driver' => 'sqlsrv',
    'host' => env('DB_HOST', 'xxx.xxx.xxx.xxx'),
    'port' => env('DB_PORT', '1433'),
    'database' => env('DB_DATABASE', 'TestDb-3'),
    'username' => '',
    'password' => '',
    'charset' => 'utf8',
    'prefix' => '',
],

AnyController.php AnyController.php

public function anyThing(Request $request)
{
    config('database.sqlsrv.username', $request->input('db_username'));
    config('database.sqlsrv.password', bcrypt($request->input('db_password'));

    // Query Builder
    $data = DB::connection('sqlsrv')->table('table_name')->get();

    // Or Eloquent
    $data = TableModel::connection('sqlsrv')->get();

    return view('path.to.view')->with(compact('data'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM