Php Laravel 5只允许根据用户类型登录到特定用户

Php Laravel 5只允许根据用户类型登录到特定用户,php,laravel,laravel-5,eloquent,laravel-5.3,Php,Laravel,Laravel 5,Eloquent,Laravel 5.3,我有一个由多个用户组成的用户表,并且已经成功地使用中间件创建了身份验证系统。现在,我想知道是否有可能只允许特定用户登录,并根据他们的类型阻止其他用户登录(即,我已经在我的用户表中定义了user_type列)您可以在Auth::true中使用自定义字段: class LoginController extends Controller { /** * Handle an authentication attempt. * * @return Response

我有一个由多个用户组成的用户表,并且已经成功地使用中间件创建了身份验证系统。现在,我想知道是否有可能只允许特定用户登录,并根据他们的类型阻止其他用户登录(即,我已经在我的用户表中定义了user_type列)

您可以在
Auth::true
中使用自定义字段:

class LoginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password, 'user_type'=>10])) { //<---
            // Authentication passed...
        }
    }
}

类登录控制器扩展控制器
{
/**
*处理身份验证尝试。
*
*@返回响应
*/
公共函数身份验证()
{

如果(Auth::trunt(['email'=>$email,'password'=>$password,'user_type'=>10]){//您可以在
Auth::trunt
中使用自定义字段:

class LoginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password, 'user_type'=>10])) { //<---
            // Authentication passed...
        }
    }
}

类登录控制器扩展控制器
{
/**
*处理身份验证尝试。
*
*@返回响应
*/
公共函数身份验证()
{
如果(Auth::trunt(['email'=>$email,'password'=>$password,'user_type'=>10]){//试试这个

$allowed = User::join('user_types', 'user.id', '=', 'user_types.user_id')
             ->where('user.email', $email)
             ->where('user.password', $password)
             ->where('user_types.type', 'admin') // the type of user
             ->exists(); 

if($allowed) {
   if (Auth::attempt(['email' => $email, 'password' => $password])) {
      // passed
   }
   return 'not authorized';
}
试试这个

$allowed = User::join('user_types', 'user.id', '=', 'user_types.user_id')
             ->where('user.email', $email)
             ->where('user.password', $password)
             ->where('user_types.type', 'admin') // the type of user
             ->exists(); 

if($allowed) {
   if (Auth::attempt(['email' => $email, 'password' => $password])) {
      // passed
   }
   return 'not authorized';
}

在5.3中,您可以覆盖
LoginController.php
中的
authenticate()
方法:

public function authenticate()
{
    if (auth()->user()->user_type !== 1) {
        auth()->logout();
    }
}

此代码将检查用户类型,如果不是
1
,则将使用方法注销已验证的用户。

在5.3中,您可以覆盖
LoginController.php
中的
authenticate()
方法:

public function authenticate()
{
    if (auth()->user()->user_type !== 1) {
        auth()->logout();
    }
}

此代码将检查用户类型,如果不是
1
,它将使用方法注销经过身份验证的用户。

是的,可以这样做。如果您想要更具体的答案,请提供您想要的和您已经拥有的详细信息。如果我没记错,如果没有记错,您可以自己轻松完成。laravel 5附带一个postLogin和afterLogin函数在Auth/LoginController中,在这两个函数中,您可以执行自己的自定义检查以验证用户是否允许登录。是的,可以这样做。如果您想要更具体的答案,请提供您想要的和您已经拥有的详细信息。如果我没记错,您可以ily make yourself laravel 5在Auth/LoginController中提供了postLogin和afterLogin函数,在这两个函数中,您可以执行自己的自定义检查以验证用户是否允许登录。没有authenticate()函数在我的LoginController中。它有一个中间件在_构造函数中加载'guest'中间件,或者我的LoginController中没有authenticate()函数。它有一个中间件在_构造函数中加载'guest'中间件,或者没有authenticate()函数在我的LoginController中。它只有一个中间件在uu构造函数中加载“来宾”中间件,所以每个人都是来宾,并且没有登录?
类LoginController扩展控制器{use AuthenticatesUsers;protected$redirectTo='/viewspace';公共函数u构造函数(){$this->middleware('guest',['except'=>'logout'];}}
我的LoginController中没有authenticate()函数。它只有一个中间件在_构造函数中加载'guest'中间件,所以每个人都是guest,没有登录?
类LoginController扩展控制器{use AuthenticatesUsers;protected$redirectTo='/viewspace';public function uu construct(){$this->middleware('guest',['except'=>'logout']);}