使用laravel检查活动用户状态


Check for active user state with laravel

这是非常标准的登录函数和验证,工作得很好。我还想检查用户是否处于活动状态。我已经在我的用户表中设置了一个列,'active'设置为0或1。

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)已经验证。因此,即使用户帐户此时没有活动,他们也已经登录了。

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

过滤器是可行的方法。解决这个问题很简单明了,请看下面我的例子。

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('/');
    }
}
});

你能不能这样写:

if (Auth::once($credentials))
{
    if(!Auth::user()->active) {
        Auth::logout();
        echo "Account not activated";
    }
}

让活动字段成为确认字段之一。你可以这样做:

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

一个更好的解决方案可能是创建一个验证驱动程序来扩展已经在使用的Eloquent验证驱动程序,然后覆盖尝试方法。

然后更改您的auth配置以使用您的驱动程序。

类似:

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

在你的登录界面,检查Session::get('authentication')并相应地处理。

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

我是这样做的:

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(),
    ]);