如何在Laravel验证使用条件参数::尝试? [英] How do i use conditional parameters in Laravel Auth::attempt?

查看:431
本文介绍了如何在Laravel验证使用条件参数::尝试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel 4.1.30我这下code这通过测试验证一个登录尝试。

Using Laravel 4.1.30 I got this following code which test a sign-in attempt via Auth.

//... more codes here ...

$auth = Auth::attempt(array(
    'email'     => Input::get('email'),
    'password'  => Input::get('password'),
    'active'    => 1
), $remember);

if ($auth) {
    //... more codes here ...
}

我想实现一个条件值,如:

I like to implement a conditional value such as:

->active > 0

我使用的是有源(场)作为认证的用户。任何超过0(零)签订水平应满足下一个条件。

I am using the active (field) as a level of authentication for users signing in. Anything above 0 (zero) should satisfy the next condition.

如何可以把它在一个语句来完成?

How can it be done in one statement?

推荐答案

您不能传递给阵列内做到这一点验证::尝试(),因为在该框架是很难codeD使用相等比较中生成的查询。

tl;dr

You can't do this within the array passed to Auth::attempt(), because in the framework it is hard coded to use equality comparison in the generated query.

尝试()功能在实施照亮/认证/ Guard.php

public function attempt(array $credentials = array(), $remember = false, $login = true)
{
    $this->fireAttemptEvent($credentials, $remember, $login);

    $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);

    // If an implementation of UserInterface was returned, we'll ask the provider
    // to validate the user against the given credentials, and if they are in
    // fact valid we'll log the users into the application and return true.
    if ($this->hasValidCredentials($user, $credentials))
    {
        if ($login) $this->login($user, $remember);

        return true;
    }

    return false;
}

在这里,您可以看到 $这个 - &GT呼叫; provider-> retrieveByCredentials($凭证); 。在 retrieveByCredentials()功能在实施照亮/认证/ DatabaseUserProvider.php

public function retrieveByCredentials(array $credentials)
{
    // First we will add each credential element to the query as a where clause.
    // Then we can execute the query and, if we found a user, return it in a
    // generic "user" object that will be utilized by the Guard instances.
    $query = $this->conn->table($this->table);

    foreach ($credentials as $key => $value)
    {
        if ( ! str_contains($key, 'password'))
        {
            $query->where($key, $value);
        }
    }

    // Now we are ready to execute the query to see if we have an user matching
    // the given credentials. If not, we will just return nulls and indicate
    // that there are no matching users for these given credential arrays.
    $user = $query->first();

    if ( ! is_null($user))
    {
        return new GenericUser((array) $user);
    }
}

在这里你可以看到,数组传递给验证::尝试()中处理的的foreach 和每一个键 - 值对将作为一个 WHERE 子句查询。因为它做了 $查询 - 化合物其中($键,$值); 通话,它是有限的相等比较

Here you can see that the array you pass to Auth::attempt() is processed in a foreach and every key-value pairs are added as a WHERE clause to the query. Because it done with a $query->where($key, $value); call, it is limited to equality comparison.

一个解决方法是这一行更改为类似:

A workaround would be to change this line to something like:

$query->where($key, $value['operator'], $value['value']);

然后,你可以调整给数组验证::尝试()

$auth = Auth::attempt(array(
    'email' => array(
        'value'    => Input::get('email'),
        'operator' => '='
    ),
    'password' => array(
        'value'    => Input::get('password'),
        'operator' => '='
    ),
    'active' => array(
        'value'    => 0,
        'operator' => '>'
    )
), $remember);

这里的问题是,你必须覆盖使用此阵所有其他功能,让你结束了一个定制的解决方案。随着这项工作,您可以编写自己的认证查询或后做支票主动验证::尝试()

The problem with this is that you have to override every other function that uses this array, so you end up with a custom solution. With this effort you could write your own authentication query or do a check on active after Auth::attempt().

这篇关于如何在Laravel验证使用条件参数::尝试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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