检查与laravel活跃用户状态 [英] Check for active user state with laravel

查看:131
本文介绍了检查与laravel活跃用户状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是pretty标准登录功能和确认工作得很好。但我也想检查用户有效。我已经建立了一个列我的用户表与主动设置为0或1。

This is pretty standard login function and validation that works nicely. But I also want to check that the user is active. I have set up a column in my users table with 'active' set to either 0 or 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);
    }
}

我已经试过这样的事情已经:

I've tried something like this already:

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

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

但是,这只能在AUTH尝试中的if语句,并在该点的用户凭据使用(电子邮件和通过)已经验证。因此,即使用户帐户如果在这一点上并不活跃,他们已经登录

But this can only be used within the 'auth attempt' if statement and at that point the users credentials(email and pass) are already validated. So even if the users account if not active at this point they are already logged in.

我需要一种方法来返回校验,让他们知道,他们仍然需要激活他们的帐户,并检查他们的帐户设置为自己的电子邮件和传球都被检查同时进行。

I need a way to return validation to let them know they still need to activate their account and check if their account is set at the same time their email and pass are being checked.

推荐答案

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

A better solution might be to create an Auth driver that extends the Eloquent Auth driver already in use and then override the attempt method.

然后改变你的AUTH配置使用驱动程序。

Then change your auth config to use your driver.

是这样的:

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

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

In your login screen, check for Session::get('authentication') and handle accordingly.

另外,允许他们登录,但不要让他们访问其他任何网页超过一个,提供了一个链接重新发送激活邮件。

Alternatively, allow them to log in but don't let them access any pages other than one that offers a link to resend the activation email.

这篇关于检查与laravel活跃用户状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆