refactored authentication system.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<?php namespace Laravel\Auth\Login\Drivers;
|
||||
<?php namespace Laravel\Auth\Drivers;
|
||||
|
||||
use Laravel\Str;
|
||||
use Laravel\Cookie;
|
||||
@@ -41,6 +41,18 @@ abstract class Driver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user of the application is not logged in.
|
||||
*
|
||||
* This method is the inverse of the "check" method.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function guest()
|
||||
{
|
||||
return ! $this->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is logged in.
|
||||
*
|
||||
@@ -58,15 +70,28 @@ abstract class Driver {
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
abstract public function user();
|
||||
public function user()
|
||||
{
|
||||
if ( ! is_null($this->user)) return $this->user;
|
||||
|
||||
return $this->user = $this->retrieve($this->token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the a given application user by ID.
|
||||
*
|
||||
* @param int $id
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function retrieve($id);
|
||||
|
||||
/**
|
||||
* Attempt to log a user into the application.
|
||||
*
|
||||
* @param dynamic $arguments
|
||||
* @param array $arguments
|
||||
* @return void
|
||||
*/
|
||||
abstract public function attempt();
|
||||
abstract public function attempt($arguments = array());
|
||||
|
||||
/**
|
||||
* Login the user assigned to the given token.
|
||||
@@ -74,10 +99,16 @@ abstract class Driver {
|
||||
* The token is typically a numeric ID for the user.
|
||||
*
|
||||
* @param string $token
|
||||
* @param bool $remember
|
||||
* @return bool
|
||||
*/
|
||||
public function login($token)
|
||||
public function login($token, $remember = false)
|
||||
{
|
||||
$this->store($token);
|
||||
|
||||
if ($remember) $this->remember($token);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
74
laravel/auth/drivers/eloquent.php
Normal file
74
laravel/auth/drivers/eloquent.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php namespace Laravel\Auth\Drivers; use User, Laravel\Hash;
|
||||
|
||||
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.
|
||||
*
|
||||
* If the user is a guest, null should be returned.
|
||||
*
|
||||
* @param int $id
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function retrieve($id)
|
||||
{
|
||||
if (filter_var($id, FILTER_VALIDATE_INT) !== false)
|
||||
{
|
||||
return $this->model()->find($id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to log a user into the application.
|
||||
*
|
||||
* @param array $arguments
|
||||
* @return void
|
||||
*/
|
||||
public function attempt($arguments = array())
|
||||
{
|
||||
$user = $this->model()->where('email', '=', $arguments['email'])->first();
|
||||
|
||||
// This driver uses a basic username and password authentication scheme
|
||||
// so 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'];
|
||||
|
||||
if ( ! is_null($user) and Hash::check($password, $user->password))
|
||||
{
|
||||
return $this->login($user->id, array_get($arguments, 'remember'));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a fresh model instance.
|
||||
*
|
||||
* @return Eloquent
|
||||
*/
|
||||
protected function model()
|
||||
{
|
||||
return new $this->model;
|
||||
}
|
||||
|
||||
}
|
||||
64
laravel/auth/drivers/fluent.php
Normal file
64
laravel/auth/drivers/fluent.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php namespace Laravel\Auth\Drivers; use Laravel\Hash, Laravel\Database;
|
||||
|
||||
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.
|
||||
*
|
||||
* If the user is a guest, null should be returned.
|
||||
*
|
||||
* @param int $id
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function retrieve($id)
|
||||
{
|
||||
if (filter_var($id, FILTER_VALIDATE_INT) !== false)
|
||||
{
|
||||
Database::table($this->table)->find($id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to log a user into the application.
|
||||
*
|
||||
* @param array $arguments
|
||||
* @return void
|
||||
*/
|
||||
public function attempt($arguments = array())
|
||||
{
|
||||
$user = Database::table($this->table)->where_email($arguments['email'])->first();
|
||||
|
||||
// This driver uses a basic username and password authentication scheme
|
||||
// so if the credentials mmatch what is in the database we will just
|
||||
// log the user into the application and remember them if asked.
|
||||
$password = $arguments['password'];
|
||||
|
||||
if ( ! is_null($user) and Hash::check($password, $user->password))
|
||||
{
|
||||
return $this->login($user->id, array_get($arguments, 'remember'));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user