4

これはかなり標準的なログイン機能であり、適切に機能する検証です。しかし、ユーザーがアクティブであることも確認したいと思います。「アクティブ」を 0 または 1 に設定して、users テーブルに列を設定しました。

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

ただし、これは「認証試行」if ステートメント内でのみ使用でき、その時点でユーザーの資格情報 (電子メールとパス) は既に検証されています。そのため、この時点でユーザー アカウントがアクティブでない場合でも、ユーザーは既にログインしています。

メールとパスがチェックされると同時に、アカウントをアクティブ化し、アカウントが設定されているかどうかを確認する必要があることを知らせるために、検証を返す方法が必要です。

4

5 に答える 5

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('/');
    }
}
});
于 2014-10-16T16:20:00.123 に答える
3

アクティブなフィールドを確認の 1 つにするだけです。あなたはこれを行うことができます:

$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');
    }
于 2014-10-12T15:59:28.377 に答える
2

より良い解決策は、すでに使用されている Eloquent Auth ドライバーを拡張する 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') を確認し、それに応じて処理します。

または、ログインは許可しますが、アクティベーション メールを再送信するためのリンクを提供するページ以外にはアクセスさせないようにします。

于 2013-05-25T17:46:44.373 に答える
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(),
    ]);
于 2016-02-04T11:47:48.810 に答える