php – 如何在Laravel Auth :: attempt中使用条件参数?
|
使用Laravel 4.1.30我得到以下代码,通过Auth测试登录尝试.
//... more codes here ...
$auth = Auth::attempt(array(
'email' => Input::get('email'),'password' => Input::get('password'),'active' => 1
),$remember);
if ($auth) {
//... more codes here ...
}
我想实现一个条件值,例如: ->active > 0 我使用活动(字段)作为登录用户的身份验证级别.任何高于0(零)的内容都应满足下一个条件. 怎么能在一个声明中完成? 解决方法
TL;博士
您不能在传递给Auth :: attempt()的数组中执行此操作,因为在框架中,硬编码在生成的查询中使用相等比较. 全面审查 框架实施 attempt()函数在Illuminate / Auth / 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;
}
在这里,您可以看到$this-> provider-> retrieveByCredentials($credentials);的电话. retrieveByCredentials()函数在Illuminate / Auth / 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);
}
}
在这里,您可以看到传递给Auth :: attempt()的数组在foreach中处理,并且每个键值对都作为WHERE子句添加到查询中.因为它完成了$query-> where($key,$value);打电话,它仅限于平等比较. 可能的解决方案 解决方法是将此行更改为: $query->where($key,$value['operator'],$value['value']); 然后你可以重构给Auth :: attempt()的数组. $auth = Auth::attempt(array(
'email' => array(
'value' => Input::get('email'),'operator' => '='
),'password' => array(
'value' => Input::get('password'),'active' => array(
'value' => 0,'operator' => '>'
)
),$remember);
这个问题是你必须覆盖使用这个数组的所有其他函数,所以你最终得到了一个自定义解决方案.通过这种努力,您可以编写自己的身份验证查询,或者在Auth :: attempt()之后检查是否处于活动状态. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
