Refactoring some auth code.

This commit is contained in:
Taylor Otwell 2012-05-01 13:12:32 -05:00
parent de7046cc66
commit f050f7731b
3 changed files with 38 additions and 46 deletions

View File

@ -18,6 +18,19 @@
'driver' => 'eloquent', 'driver' => 'eloquent',
/*
|--------------------------------------------------------------------------
| Authentication Username
|--------------------------------------------------------------------------
|
| Here you may specify the database column that should be considered the
| "username" for your users. Typically, this will either be "usenrame"
| or "email". Of course, you're free to change the value to anything.
|
*/
'username' => 'email',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Authentication Model | Authentication Model

View File

@ -1,27 +1,7 @@
<?php namespace Laravel\Auth\Drivers; use User, Laravel\Hash; <?php namespace Laravel\Auth\Drivers; use Laravel\Hash, Laravel\Config;
class Eloquent extends Driver { class Eloquent extends Driver {
/**
* The name of the "User" model used by the application.
*
* @var string
*/
public $model;
/**
* Create a new Eloquent authentication driver.
*
* @param string $model
* @return void
*/
public function __construct($model)
{
$this->model = $model;
parent::__construct();
}
/** /**
* Get the current user of the application. * Get the current user of the application.
* *
@ -46,7 +26,9 @@ public function retrieve($id)
*/ */
public function attempt($arguments = array()) public function attempt($arguments = array())
{ {
$user = $this->model()->where('email', '=', $arguments['email'])->first(); $username = Config::get('auth.username');
$user = $this->model()->where($username, '=', $arguments['username'])->first();
// This driver uses a basic username and password authentication scheme // This driver uses a basic username and password authentication scheme
// so if the credentials match what is in the database we will just // so if the credentials match what is in the database we will just
@ -68,7 +50,9 @@ public function attempt($arguments = array())
*/ */
protected function model() protected function model()
{ {
return new $this->model; $model = Config::get('auth.model');
return new $model;
} }
} }

View File

@ -1,27 +1,7 @@
<?php namespace Laravel\Auth\Drivers; use Laravel\Hash, Laravel\Database; <?php namespace Laravel\Auth\Drivers; use Laravel\Hash, Laravel\Database as DB;
class Fluent extends Driver { class Fluent extends Driver {
/**
* The "users" table used by the application.
*
* @var string
*/
public $table;
/**
* Create a new fluent authentication driver.
*
* @param string $table
* @return void
*/
public function __construct($table)
{
$this->table = $table;
parent::__construct();
}
/** /**
* Get the current user of the application. * Get the current user of the application.
* *
@ -34,7 +14,7 @@ public function retrieve($id)
{ {
if (filter_var($id, FILTER_VALIDATE_INT) !== false) if (filter_var($id, FILTER_VALIDATE_INT) !== false)
{ {
Database::table($this->table)->find($id); DB::table(Config::get('auth.table'))->find($id);
} }
} }
@ -46,7 +26,7 @@ public function retrieve($id)
*/ */
public function attempt($arguments = array()) public function attempt($arguments = array())
{ {
$user = Database::table($this->table)->where_email($arguments['email'])->first(); $user = $this->get_user($arguments['username']);
// This driver uses a basic username and password authentication scheme // This driver uses a basic username and password authentication scheme
// so if the credentials mmatch what is in the database we will just // so if the credentials mmatch what is in the database we will just
@ -61,4 +41,19 @@ public function attempt($arguments = array())
return false; return false;
} }
/**
* Get the user from the database table by username.
*
* @param mixed $value
* @return mixed
*/
protected function get_user($value)
{
$table = Config::get('auth.table');
$username = Config::get('auth.username');
return DB::table($table)->where($username, '=', $value)->first();
}
} }