2013-05-25 33 views
4

这是非常标准的登录功能和验证,可以很好地工作。但我也想检查用户是否活跃。我已成立了一个列在我的用户表设置为0或1使用laravel检查活动用户状态

public function post_login() 
{ 
    $input = Input::all(); 

    $rules = array(
     'email' => 'required|email', 
     'password' => 'required', 
    ); 

    $validation = Validator::make($input, $rules); 

    if ($validation->fails()) 
    { 
     return Redirect::to_route('login_user') 
      ->with_errors($validation->errors)->with_input(); 
    } 

    $credentials = array(
     'username' => $input['email'], 
     'password' => $input['password'], 
    ); 

    if (Auth::attempt($credentials)) 
    { 
     // Set remember me cookie if the user checks the box 
     $remember = Input::get('remember'); 
     if (!empty($remember)) 
     { 
      Auth::login(Auth::user()->id, true); 
     } 

     return Redirect::home(); 

    } else { 
     return Redirect::to_route('login_user') 
      ->with('login_errors', true); 
    } 
} 

我已经试过这样的事情已经“主动”:

$is_active = Auth::user()->active; 

if (!$is_active == 1) 
{ 
    echo "Account not activated"; 
} 

但这只能用在'auth attempt'if语句中,在那一点上,用户证书(email和pass)已经被验证。所以,即使用户帐户,如果不活跃在这一点上,他们已经登录。

我需要一种方式来返回验证,让他们知道他们仍然需要激活他们的帐户,并检查他们的帐户是否设置为相同他们的电子邮件和通行证正在被检查。

回答

2

一个更好的解决方案可能是创建已扩展雄辩验证驾驶员在使用中验证驱动程序,然后重写尝试的方法。

然后更改您的身份验证配置以使用您的驱动程序。

喜欢的东西:

<?php 

class Myauth extends Laravel\Auth\Drivers\Eloquent { 

    /** 
    * Attempt to log a user into the application. 
    * 
    * @param array $arguments 
    * @return void 
    */ 
    public function attempt($arguments = array()) 
    { 
     $user = $this->model()->where(function($query) use($arguments) 
     { 
      $username = Config::get('auth.username'); 

      $query->where($username, '=', $arguments['username']); 

      foreach(array_except($arguments, array('username', 'password', 'remember')) as $column => $val) 
      { 
       $query->where($column, '=', $val); 
      } 
     })->first(); 

     // If the credentials match what is in the database we will just 
     // log the user into the application and remember them if asked. 
     $password = $arguments['password']; 

     $password_field = Config::get('auth.password', 'password'); 

     if (! is_null($user) and Hash::check($password, $user->{$password_field})) 
     { 
      if ($user->active){ 
       return $this->login($user->get_key(), array_get($arguments, 'remember')); 
      } else { 
       Session::flash('authentication', array('message' => 'You must activate your account before you can log in')); 
      } 
     } 

     return false; 
    } 
} 
?> 

在登录屏幕,查看会议::获得( '验证'),并相应地处理。

或者,允许他们登录,但不要让他们访问除提供链接以重新发送激活电子邮件之外的任何页面。

4

你不能使用这样的事情:

if (Auth::once($credentials)) 
{ 
    if(!Auth::user()->active) { 
     Auth::logout(); 

     echo "Account not activated"; 
    } 
} 
+0

善于思考,我喜欢这个,我只是希望比不必要有进出,虽然登录用户更完美的解决方案。 –

+0

这就是我一直在寻找的东西。谢谢 –

3

只需使活动字段成为确认之一即可。你可以这样做:

$credentials = array(
     'username' => $input['email'], 
     'password' => $input['password'], 
     'active' => 1 
    ); 

    if (Auth::attempt($credentials)) 
    { 
     // User is active and password was correct 
    } 

如果你想明确告诉他们不是活跃用户 - 您可以跟进与此:

if (Auth::validate(['username' => $input['email'], 'password' => $input['password'], 'active' => 0])) 
    { 
     return echo ('you are not active'); 
    } 
8

过滤器是要走的路。解决这个问题很简单,很简单,参见下面的示例。

Route::filter('auth', function() 
{ 
    if (Auth::guest()) 
{ 
    if (Request::ajax()) 
    { 
     return Response::make('Unauthorized', 401); 
    } 
    else 
    { 
     return Redirect::guest('login'); 
    } 
} 
else 
{ 
    // If the user is not active any more, immidiately log out. 
    if(Auth::check() && !Auth::user()->active) 
    { 
     Auth::logout(); 

     return Redirect::to('/'); 
    } 
} 
}); 
0

这是我做的:

if (\Auth::attempt(['EmailWork' => $credentials['EmailWork'], 'password' => $credentials['Password']], $request->has('remember'))) { 
    if (\Auth::once(['EmailWork' => $credentials['EmailWork'], 'password' => $credentials['Password']])) { 
     if (!\Auth::user()->FlagActive == 'Active') { 
      \Auth::logout(); 
      return redirect($this->loginPath()) 
       ->withInput($request->only('EmailWork', 'RememberToken')) 
       ->withErrors([ 
        'Active' => 'You are not activated!', 
       ]); 
     } 
    } 

    return redirect('/'); 
} 

return redirect($this->loginPath()) 
    ->withInput($request->only('EmailWork', 'RememberToken')) 
    ->withErrors([ 
     'EmailWork' => $this->getFailedLoginMessage(), 
    ]); 
相关问题