Authentication 带条件的Laravel认证

Authentication 带条件的Laravel认证,authentication,laravel-5.1,Authentication,Laravel 5.1,我使用的是Laravel 5.1和Laravel的默认身份验证系统 在数据库(MySQL)中,我添加了一个名为“角色”的新列。管理员的值为1,成员的值为2 现在我只想为admin授予登录权限,这意味着值为1。我该怎么做呢?我觉得有更好的方法来实现你所追求的目标,比如,考虑到你所追求的目标,这将是实现目标的一种方法 登录用户后,我们将发送到“主页”,除非您在AuthController中另有指定 在routes.php中,如果您只需设置一个GET路由以指向HomeController(或任何您命名

我使用的是Laravel 5.1和Laravel的默认身份验证系统

在数据库(MySQL)中,我添加了一个名为“角色”的新列。管理员的值为1,成员的值为2


现在我只想为admin授予登录权限,这意味着值为1。我该怎么做呢?

我觉得有更好的方法来实现你所追求的目标,比如,考虑到你所追求的目标,这将是实现目标的一种方法

登录用户后,我们将发送到“主页”,除非您在
AuthController
中另有指定

routes.php
中,如果您只需设置一个
GET
路由以指向
HomeController
(或任何您命名的路由),那么您可以使用一个函数来运行所需的测试

routes.php

Route::get('home','HomeController@index');

HomeController

public function index()
    {
        //If they are yet to log in then return your normal homepage
        if (Auth::guest())
        {
            return View::make('home');
        }
        else
        {
           //Run your tests here to check their role and direct appropriately
           //Given you have added the role column to the users table, you can access it like so:
           //Auth::user()->role
        }
    }

事实上我解决了。我只是在
AthenticatesUsers.php
方法的
postLogin()
方法中添加了这些代码

    // If role is equal to 1, user allowed to login
    // You can change $admin value anytime according to database Design
    // Example: In role column the value for admin is 2 or A. You just need to change the value of $admin.
    $userData = User::select('role')->where('email',$request['email'])->first();
    $admin = 1;
    $role = $userData->role;
    if($role == $admin){
        $request['role'] = $role;
    }

所以,我想澄清一下,您希望管理员能够做什么?授予登录权限听起来像是你只希望管理员能够登录到你的站点是的,我只希望管理员能够登录。好的,但是如果成员无法登录,允许他们注册有什么意义呢?或者您只是想将站点的一部分限制为仅限管理员登录?不允许成员始终登录。但如果您只允许管理员登录,则成员永远无法登录。我想帮你,但我不清楚你到底想要什么。您正在构建一个具有成员和管理员的应用程序,但是管理员是唯一可以根据您的请求登录的应用程序。这真的没有意义,因为有会员在第一位有什么意义,因为他们永远不能登录。