Laravel身份验证条件

时间:2015-10-26 19:16:04

标签: authentication laravel-5.1

我正在使用Laravel 5.1和Laravel的默认身份验证系统。

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

现在我只想为admin提供登录权限,意味着值为1.我该怎么做?

2 个答案:

答案 0 :(得分:1)

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

    // 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;
    }

答案 1 :(得分:0)

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

登录用户后,我们发送到“主页”,除非您在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
        }
    }