Ace*_*rya 4 php authentication laravel
使用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 ...
}
Run Code Online (Sandbox Code Playgroud)
我想实现一个条件值,例如:
->active > 0
Run Code Online (Sandbox Code Playgroud)
我使用活动(字段)作为登录用户的身份验证级别.任何高于0(零)的内容都应满足下一个条件.
怎么能在一个声明中完成?
您不能在传递给的数组中执行此操作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;
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到一个电话$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);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,您可以看到传递给的数组Auth::attempt()
在a中处理,foreach
并且每个键值对都作为WHERE
子句添加到查询中.因为它是通过$query->where($key, $value);
调用完成的,所以它仅限于相等比较.
解决方法是将此行更改为:
$query->where($key, $value['operator'], $value['value']);
Run Code Online (Sandbox Code Playgroud)
然后你可以重组给定的数组Auth::attempt()
.
$auth = Auth::attempt(array(
'email' => array(
'value' => Input::get('email'),
'operator' => '='
),
'password' => array(
'value' => Input::get('password'),
'operator' => '='
),
'active' => array(
'value' => 0,
'operator' => '>'
)
), $remember);
Run Code Online (Sandbox Code Playgroud)
这个问题是你必须覆盖使用这个数组的所有其他函数,所以你最终得到了一个自定义解决方案.通过这种努力,您可以编写自己的身份验证查询或在active
之后进行检查Auth::attempt()
.
归档时间: |
|
查看次数: |
2804 次 |
最近记录: |