2016-09-03 4 views
1

명백한 전자 메일과 암호를 사용하여 사용자를 인증하려고 시도하고 있으며 ban_status가 데이터베이스에서 0으로 설정된 경우에도 마찬가지입니다.로그인에 대한 추가 조건을 laravel 5.3에 추가하십시오.

나는 최신 laravel의 문서를 살펴 있었 나는 이것은 지금까지 내가 말할 수있는 사용자 로그인하므로 아무것도하지 않는

protected function validateLogin(Request $request) 
{ 
    $this->validate($request, [ 
     $this->username() => 'required', 'password' => 'required', 'ban_status' => '0', 
    ]); 
} 

AuthenticateUsers.php에이 방법을 시도 여부에 상관없이 금지 상태가 0인지 아닌지,이 여분의 조건을 수행해야합니까?

답변

1

간략한 이야기를하기 위해 게시 한 코드에서 실제로 수행하려는 작업은 $request, 즉 로그인 양식에서 전달되는 ban_status 값을 확인하는 것입니다.

귀하의 질문에 대한 이해는 이것이 실제로 원하는 것이 아니라는 것입니다.

대신

,이 시도 :

다음과 같은 작은 추가와 함께, LoginController 그것을 정의하여 AuthenticatesUserslogin 방법이 ban_status 확인하기 위해 재정의 :

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 ($lockedOut = $this->hasTooManyLoginAttempts($request)) { 
     $this->fireLockoutEvent($request); 

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

    $credentials = $this->credentials($request); 

    if ($this->guard()->attempt($credentials, $request->has('remember'))) { 
     if ($this->guard()->user()->ban_status === 0) { // ADDED THIS CHECK 
      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. 
    if (! $lockedOut) { 
     $this->incrementLoginAttempts($request); 
    } 

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

어떻게 재정의하겠습니까? LoginController @ login으로 이동하기 위해/login에 대한 게시물 요청을위한 경로를 만들 수 있습니까? – user6073700

+0

사실, 새로운 코드를 복사하여 LoginController에 붙여 넣기 만하면됩니다. 다른 모든 것은 동일하게 유지 될 수 있습니다. 우리가하는 일은 AuthenticatesUsers 특성의 로그인 메소드를 "오버라이드 (override)"하는 것입니다. 이 개념에 익숙하지 않은 경우 [this] (http://php.net/manual/en/language.oop5.inheritance.php)를 확인한 다음 [this] (http://php.net/manual/en /language.oop5.traits.php). – tam5

+0

아, 그래, 내 문제는 내가 잘못된 요청을 사용했다, 어디서 사용자가 로그인 실패하면 어디 갈 정의 했을까? 즉 ban_status가 0이 아닙니다. – user6073700

1

당신은 수동으로 사용자를 인증 할 수 있습니다 :

public function authenticate(Request $request) 
{ 
    $password=$request->get('password'); 
    $email=$request->get('email'); 
    if (Auth::attempt(['email' => $email, 'password' => $password,'ban_status'=>0])) 
    {  
     return redirect()->intended('/'); 
    } 
    else 
    { 
     return redirect('/login');  
    }  
} 
+0

이렇게하면 조절 능력을 잃게됩니다. – tam5

2

tam의 답변을 토대로, 나는 실패한 " 금지 된 "상태를 나타냅니다. 그렇지 않으면 조건이 거짓 일지라도 여전히 로그인됩니다. 다음은 LoginController.php에있는 나를 위해 작동 한 로그인 함수의 재정의입니다.

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

    $credentials = $this->credentials($request); 

    if ($this->guard()->attempt($credentials, $request->has('remember'))) 
    {   
     if ($this->guard()->user()->ban_status === 0) { // ADDED THIS CHECK 
      return $this->sendLoginResponse($request); 
     } else { // logout and redirect if failed 
      $this->guard()->logout(); 
      return redirect()->back() 
       ->withInput($request->only($this->username(), 'remember')) 
       ->withErrors([ 
        $this->username() => 'You have been banned', 
       ]);   
     } 
    } 

    // 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); 
} 
관련 문제