From 8edd8fcbec4b41159de840317b632e6588cdea0d Mon Sep 17 00:00:00 2001 From: Phill Sparks Date: Wed, 23 May 2012 11:21:07 +0100 Subject: [PATCH 1/7] Fixed line endings. Signed-off-by: Phill Sparks --- laravel/auth/drivers/driver.php | 446 ++++++++++++++++---------------- 1 file changed, 223 insertions(+), 223 deletions(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index d2927cd9..4a272ba4 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -1,224 +1,224 @@ -token = Session::get($this->token()); - } - - // If a token did not exist in the session for the user, we will attempt - // to load the value of a "remember me" cookie for the driver, which - // serves as a long-lived client side authenticator for the user. - if (is_null($this->token)) - { - $this->token = $this->recall(); - } - } - - /** - * 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. - * - * @return bool - */ - public function check() - { - return ! is_null($this->user()); - } - - /** - * Get the current user of the application. - * - * If the user is a guest, null should be returned. - * - * @return mixed|null - */ - 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 array $arguments - * @return void - */ - abstract public function attempt($arguments = array()); - - /** - * Login the user assigned to the given token. - * - * The token is typically a numeric ID for the user. - * - * @param string $token - * @param bool $remember - * @return bool - */ - public function login($token, $remember = false) - { - $this->token = $token; - - $this->store($token); - - if ($remember) $this->remember($token); - - return true; - } - - /** - * Log the user out of the driver's auth context. - * - * @return void - */ - public function logout() - { - $this->user = null; - - $this->cookie($this->recaller(), null, -2000); - - Session::forget($this->token()); - } - - /** - * Store a user's token in the session. - * - * @param string $token - * @return void - */ - protected function store($token) - { - Session::put($this->token(), $token); - } - - /** - * Store a user's token in a long-lived cookie. - * - * @param string $token - * @return void - */ - protected function remember($token) - { - $token = Crypter::encrypt($token.'|'.Str::random(40)); - - $this->cookie($this->recaller(), $token, Cookie::forever); - } - - /** - * Attempt to find a "remember me" cookie for the user. - * - * @return string|null - */ - protected function recall() - { - $cookie = Cookie::get($this->recaller()); - - // By default, "remember me" cookies are encrypted and contain the user - // token as well as a random string. If it exists, we'll decrypt it - // and return the first segment, which is the user's ID token. - if ( ! is_null($cookie)) - { - return head(explode('|', Crypter::decrypt($cookie))); - } - } - - /** - * Store an authentication cookie. - * - * @param string $name - * @param string $value - * @param int $minutes - * @return void - */ - protected function cookie($name, $value, $minutes) - { - // When setting the default implementation of an authentication - // cookie we'll use the same settings as the session cookie. - // This typically makes sense as they both are sensitive. - $config = Config::get('session'); - - extract($config); - - Cookie::put($name, $value, $minutes, $path, $domain, $secure); - } - - /** - * Get session key name used to store the token. - * - * @return string - */ - protected function token() - { - return $this->name().'_login'; - } - - /** - * Get the name used for the "remember me" cookie. - * - * @return string - */ - protected function recaller() - { - return $this->name().'_remember'; - } - - /** - * Get the name of the driver in a storage friendly format. - * - * @return string - */ - protected function name() - { - return strtolower(str_replace('\\', '_', get_class($this))); - } - +token = Session::get($this->token()); + } + + // If a token did not exist in the session for the user, we will attempt + // to load the value of a "remember me" cookie for the driver, which + // serves as a long-lived client side authenticator for the user. + if (is_null($this->token)) + { + $this->token = $this->recall(); + } + } + + /** + * 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. + * + * @return bool + */ + public function check() + { + return ! is_null($this->user()); + } + + /** + * Get the current user of the application. + * + * If the user is a guest, null should be returned. + * + * @return mixed|null + */ + 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 array $arguments + * @return void + */ + abstract public function attempt($arguments = array()); + + /** + * Login the user assigned to the given token. + * + * The token is typically a numeric ID for the user. + * + * @param string $token + * @param bool $remember + * @return bool + */ + public function login($token, $remember = false) + { + $this->token = $token; + + $this->store($token); + + if ($remember) $this->remember($token); + + return true; + } + + /** + * Log the user out of the driver's auth context. + * + * @return void + */ + public function logout() + { + $this->user = null; + + $this->cookie($this->recaller(), null, -2000); + + Session::forget($this->token()); + } + + /** + * Store a user's token in the session. + * + * @param string $token + * @return void + */ + protected function store($token) + { + Session::put($this->token(), $token); + } + + /** + * Store a user's token in a long-lived cookie. + * + * @param string $token + * @return void + */ + protected function remember($token) + { + $token = Crypter::encrypt($token.'|'.Str::random(40)); + + $this->cookie($this->recaller(), $token, Cookie::forever); + } + + /** + * Attempt to find a "remember me" cookie for the user. + * + * @return string|null + */ + protected function recall() + { + $cookie = Cookie::get($this->recaller()); + + // By default, "remember me" cookies are encrypted and contain the user + // token as well as a random string. If it exists, we'll decrypt it + // and return the first segment, which is the user's ID token. + if ( ! is_null($cookie)) + { + return head(explode('|', Crypter::decrypt($cookie))); + } + } + + /** + * Store an authentication cookie. + * + * @param string $name + * @param string $value + * @param int $minutes + * @return void + */ + protected function cookie($name, $value, $minutes) + { + // When setting the default implementation of an authentication + // cookie we'll use the same settings as the session cookie. + // This typically makes sense as they both are sensitive. + $config = Config::get('session'); + + extract($config); + + Cookie::put($name, $value, $minutes, $path, $domain, $secure); + } + + /** + * Get session key name used to store the token. + * + * @return string + */ + protected function token() + { + return $this->name().'_login'; + } + + /** + * Get the name used for the "remember me" cookie. + * + * @return string + */ + protected function recaller() + { + return $this->name().'_remember'; + } + + /** + * Get the name of the driver in a storage friendly format. + * + * @return string + */ + protected function name() + { + return strtolower(str_replace('\\', '_', get_class($this))); + } + } \ No newline at end of file From 4abd56e695286c5901801c51a2a74f52f6d0449b Mon Sep 17 00:00:00 2001 From: Dayle Rees Date: Thu, 24 May 2012 15:34:34 +0200 Subject: [PATCH 2/7] added alias Authenticator for Laravel\Auth\Drivers\Driver --- application/config/application.php | 81 +++++++++++++++--------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/application/config/application.php b/application/config/application.php index 57b08295..9f597954 100755 --- a/application/config/application.php +++ b/application/config/application.php @@ -139,46 +139,47 @@ */ 'aliases' => array( - 'Auth' => 'Laravel\\Auth', - 'Asset' => 'Laravel\\Asset', - 'Autoloader' => 'Laravel\\Autoloader', - 'Blade' => 'Laravel\\Blade', - 'Bundle' => 'Laravel\\Bundle', - 'Cache' => 'Laravel\\Cache', - 'Config' => 'Laravel\\Config', - 'Controller' => 'Laravel\\Routing\\Controller', - 'Cookie' => 'Laravel\\Cookie', - 'Crypter' => 'Laravel\\Crypter', - 'DB' => 'Laravel\\Database', - 'Eloquent' => 'Laravel\\Database\\Eloquent\\Model', - 'Event' => 'Laravel\\Event', - 'File' => 'Laravel\\File', - 'Filter' => 'Laravel\\Routing\\Filter', - 'Form' => 'Laravel\\Form', - 'Hash' => 'Laravel\\Hash', - 'HTML' => 'Laravel\\HTML', - 'Input' => 'Laravel\\Input', - 'IoC' => 'Laravel\\IoC', - 'Lang' => 'Laravel\\Lang', - 'Log' => 'Laravel\\Log', - 'Memcached' => 'Laravel\\Memcached', - 'Paginator' => 'Laravel\\Paginator', - 'Profiler' => 'Laravel\\Profiling\\Profiler', - 'URL' => 'Laravel\\URL', - 'Redirect' => 'Laravel\\Redirect', - 'Redis' => 'Laravel\\Redis', - 'Request' => 'Laravel\\Request', - 'Response' => 'Laravel\\Response', - 'Route' => 'Laravel\\Routing\\Route', - 'Router' => 'Laravel\\Routing\\Router', - 'Schema' => 'Laravel\\Database\\Schema', - 'Section' => 'Laravel\\Section', - 'Session' => 'Laravel\\Session', - 'Str' => 'Laravel\\Str', - 'Task' => 'Laravel\\CLI\\Tasks\\Task', - 'URI' => 'Laravel\\URI', - 'Validator' => 'Laravel\\Validator', - 'View' => 'Laravel\\View', + 'Auth' => 'Laravel\\Auth', + 'Authenticator' => 'Laravel\\Auth\\Drivers\\Driver', + 'Asset' => 'Laravel\\Asset', + 'Autoloader' => 'Laravel\\Autoloader', + 'Blade' => 'Laravel\\Blade', + 'Bundle' => 'Laravel\\Bundle', + 'Cache' => 'Laravel\\Cache', + 'Config' => 'Laravel\\Config', + 'Controller' => 'Laravel\\Routing\\Controller', + 'Cookie' => 'Laravel\\Cookie', + 'Crypter' => 'Laravel\\Crypter', + 'DB' => 'Laravel\\Database', + 'Eloquent' => 'Laravel\\Database\\Eloquent\\Model', + 'Event' => 'Laravel\\Event', + 'File' => 'Laravel\\File', + 'Filter' => 'Laravel\\Routing\\Filter', + 'Form' => 'Laravel\\Form', + 'Hash' => 'Laravel\\Hash', + 'HTML' => 'Laravel\\HTML', + 'Input' => 'Laravel\\Input', + 'IoC' => 'Laravel\\IoC', + 'Lang' => 'Laravel\\Lang', + 'Log' => 'Laravel\\Log', + 'Memcached' => 'Laravel\\Memcached', + 'Paginator' => 'Laravel\\Paginator', + 'Profiler' => 'Laravel\\Profiling\\Profiler', + 'URL' => 'Laravel\\URL', + 'Redirect' => 'Laravel\\Redirect', + 'Redis' => 'Laravel\\Redis', + 'Request' => 'Laravel\\Request', + 'Response' => 'Laravel\\Response', + 'Route' => 'Laravel\\Routing\\Route', + 'Router' => 'Laravel\\Routing\\Router', + 'Schema' => 'Laravel\\Database\\Schema', + 'Section' => 'Laravel\\Section', + 'Session' => 'Laravel\\Session', + 'Str' => 'Laravel\\Str', + 'Task' => 'Laravel\\CLI\\Tasks\\Task', + 'URI' => 'Laravel\\URI', + 'Validator' => 'Laravel\\Validator', + 'View' => 'Laravel\\View', ), ); From 1005051738e67809db6d9d224c94de49a56e9d8d Mon Sep 17 00:00:00 2001 From: Shawn McCool Date: Sat, 26 May 2012 13:18:00 +0200 Subject: [PATCH 3/7] commit new-line --- laravel/auth/drivers/driver.php | 446 ++++++++++++++++---------------- 1 file changed, 223 insertions(+), 223 deletions(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index d2927cd9..4a272ba4 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -1,224 +1,224 @@ -token = Session::get($this->token()); - } - - // If a token did not exist in the session for the user, we will attempt - // to load the value of a "remember me" cookie for the driver, which - // serves as a long-lived client side authenticator for the user. - if (is_null($this->token)) - { - $this->token = $this->recall(); - } - } - - /** - * 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. - * - * @return bool - */ - public function check() - { - return ! is_null($this->user()); - } - - /** - * Get the current user of the application. - * - * If the user is a guest, null should be returned. - * - * @return mixed|null - */ - 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 array $arguments - * @return void - */ - abstract public function attempt($arguments = array()); - - /** - * Login the user assigned to the given token. - * - * The token is typically a numeric ID for the user. - * - * @param string $token - * @param bool $remember - * @return bool - */ - public function login($token, $remember = false) - { - $this->token = $token; - - $this->store($token); - - if ($remember) $this->remember($token); - - return true; - } - - /** - * Log the user out of the driver's auth context. - * - * @return void - */ - public function logout() - { - $this->user = null; - - $this->cookie($this->recaller(), null, -2000); - - Session::forget($this->token()); - } - - /** - * Store a user's token in the session. - * - * @param string $token - * @return void - */ - protected function store($token) - { - Session::put($this->token(), $token); - } - - /** - * Store a user's token in a long-lived cookie. - * - * @param string $token - * @return void - */ - protected function remember($token) - { - $token = Crypter::encrypt($token.'|'.Str::random(40)); - - $this->cookie($this->recaller(), $token, Cookie::forever); - } - - /** - * Attempt to find a "remember me" cookie for the user. - * - * @return string|null - */ - protected function recall() - { - $cookie = Cookie::get($this->recaller()); - - // By default, "remember me" cookies are encrypted and contain the user - // token as well as a random string. If it exists, we'll decrypt it - // and return the first segment, which is the user's ID token. - if ( ! is_null($cookie)) - { - return head(explode('|', Crypter::decrypt($cookie))); - } - } - - /** - * Store an authentication cookie. - * - * @param string $name - * @param string $value - * @param int $minutes - * @return void - */ - protected function cookie($name, $value, $minutes) - { - // When setting the default implementation of an authentication - // cookie we'll use the same settings as the session cookie. - // This typically makes sense as they both are sensitive. - $config = Config::get('session'); - - extract($config); - - Cookie::put($name, $value, $minutes, $path, $domain, $secure); - } - - /** - * Get session key name used to store the token. - * - * @return string - */ - protected function token() - { - return $this->name().'_login'; - } - - /** - * Get the name used for the "remember me" cookie. - * - * @return string - */ - protected function recaller() - { - return $this->name().'_remember'; - } - - /** - * Get the name of the driver in a storage friendly format. - * - * @return string - */ - protected function name() - { - return strtolower(str_replace('\\', '_', get_class($this))); - } - +token = Session::get($this->token()); + } + + // If a token did not exist in the session for the user, we will attempt + // to load the value of a "remember me" cookie for the driver, which + // serves as a long-lived client side authenticator for the user. + if (is_null($this->token)) + { + $this->token = $this->recall(); + } + } + + /** + * 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. + * + * @return bool + */ + public function check() + { + return ! is_null($this->user()); + } + + /** + * Get the current user of the application. + * + * If the user is a guest, null should be returned. + * + * @return mixed|null + */ + 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 array $arguments + * @return void + */ + abstract public function attempt($arguments = array()); + + /** + * Login the user assigned to the given token. + * + * The token is typically a numeric ID for the user. + * + * @param string $token + * @param bool $remember + * @return bool + */ + public function login($token, $remember = false) + { + $this->token = $token; + + $this->store($token); + + if ($remember) $this->remember($token); + + return true; + } + + /** + * Log the user out of the driver's auth context. + * + * @return void + */ + public function logout() + { + $this->user = null; + + $this->cookie($this->recaller(), null, -2000); + + Session::forget($this->token()); + } + + /** + * Store a user's token in the session. + * + * @param string $token + * @return void + */ + protected function store($token) + { + Session::put($this->token(), $token); + } + + /** + * Store a user's token in a long-lived cookie. + * + * @param string $token + * @return void + */ + protected function remember($token) + { + $token = Crypter::encrypt($token.'|'.Str::random(40)); + + $this->cookie($this->recaller(), $token, Cookie::forever); + } + + /** + * Attempt to find a "remember me" cookie for the user. + * + * @return string|null + */ + protected function recall() + { + $cookie = Cookie::get($this->recaller()); + + // By default, "remember me" cookies are encrypted and contain the user + // token as well as a random string. If it exists, we'll decrypt it + // and return the first segment, which is the user's ID token. + if ( ! is_null($cookie)) + { + return head(explode('|', Crypter::decrypt($cookie))); + } + } + + /** + * Store an authentication cookie. + * + * @param string $name + * @param string $value + * @param int $minutes + * @return void + */ + protected function cookie($name, $value, $minutes) + { + // When setting the default implementation of an authentication + // cookie we'll use the same settings as the session cookie. + // This typically makes sense as they both are sensitive. + $config = Config::get('session'); + + extract($config); + + Cookie::put($name, $value, $minutes, $path, $domain, $secure); + } + + /** + * Get session key name used to store the token. + * + * @return string + */ + protected function token() + { + return $this->name().'_login'; + } + + /** + * Get the name used for the "remember me" cookie. + * + * @return string + */ + protected function recaller() + { + return $this->name().'_remember'; + } + + /** + * Get the name of the driver in a storage friendly format. + * + * @return string + */ + protected function name() + { + return strtolower(str_replace('\\', '_', get_class($this))); + } + } \ No newline at end of file From ffa9c7997a1898a1ab26461efdcc820b17aa882f Mon Sep 17 00:00:00 2001 From: Dayle Rees Date: Fri, 25 May 2012 22:02:42 +0100 Subject: [PATCH 4/7] added ability to pass eloquent model to Auth::login() Signed-off-by: Dayle Rees --- laravel/auth/drivers/driver.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index 4a272ba4..8f4f391b 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -5,6 +5,7 @@ use Laravel\Config; use Laravel\Session; use Laravel\Crypter; +use Laravel\Database\Eloquent\Model as Eloquent; abstract class Driver { @@ -100,12 +101,16 @@ abstract public function attempt($arguments = array()); * * The token is typically a numeric ID for the user. * - * @param string $token + * @param mixed $token * @param bool $remember * @return bool */ public function login($token, $remember = false) { + // if the token is an Eloquent model + // set the token from the id field + if ($token instanceof Eloquent) $token = $token->id; + $this->token = $token; $this->store($token); From 3c43f5fac46b199cafd09b248e9b792d751f0e0a Mon Sep 17 00:00:00 2001 From: Shawn McCool Date: Sun, 27 May 2012 20:29:00 +0200 Subject: [PATCH 5/7] updated auth::login to use get_key() --- laravel/auth/drivers/driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index 8f4f391b..21a5f5ad 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -109,7 +109,7 @@ public function login($token, $remember = false) { // if the token is an Eloquent model // set the token from the id field - if ($token instanceof Eloquent) $token = $token->id; + if ($token instanceof Eloquent) $token = $token->get_key(); $this->token = $token; From 11c67a56ccc0d51dd16d8fa5f73faabcf96deedc Mon Sep 17 00:00:00 2001 From: Shawn McCool Date: Mon, 28 May 2012 11:31:21 +0200 Subject: [PATCH 6/7] added eloquent model detection to eloquent auth driver --- laravel/auth/drivers/driver.php | 4 ---- laravel/auth/drivers/eloquent.php | 25 ++++++++++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index 21a5f5ad..5785291a 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -107,10 +107,6 @@ abstract public function attempt($arguments = array()); */ public function login($token, $remember = false) { - // if the token is an Eloquent model - // set the token from the id field - if ($token instanceof Eloquent) $token = $token->get_key(); - $this->token = $token; $this->store($token); diff --git a/laravel/auth/drivers/eloquent.php b/laravel/auth/drivers/eloquent.php index 5c0437d6..410de34e 100644 --- a/laravel/auth/drivers/eloquent.php +++ b/laravel/auth/drivers/eloquent.php @@ -15,7 +15,30 @@ public function retrieve($id) if (filter_var($id, FILTER_VALIDATE_INT) !== false) { return $this->model()->find($id); - } + } + } + + /** + * Login the user assigned to the given token. + * + * The token is typically a numeric ID for the user. + * + * @param mixed $token + * @param bool $remember + * @return bool + */ + public function login($token, $remember = false) + { + // if the token is an Eloquent model get the primary key + if ($token instanceof \Eloquent) $token = $token->get_key(); + + $this->token = $token; + + $this->store($token); + + if ($remember) $this->remember($token); + + return true; } /** From 69d9257a50566d9fc34a62e8f0730685b1be9a30 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 29 May 2012 17:00:23 -0500 Subject: [PATCH 7/7] Return value of cookie. --- laravel/auth/drivers/driver.php | 446 ++++++++++++++++---------------- laravel/cookie.php | 2 +- 2 files changed, 224 insertions(+), 224 deletions(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index d2927cd9..4a272ba4 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -1,224 +1,224 @@ -token = Session::get($this->token()); - } - - // If a token did not exist in the session for the user, we will attempt - // to load the value of a "remember me" cookie for the driver, which - // serves as a long-lived client side authenticator for the user. - if (is_null($this->token)) - { - $this->token = $this->recall(); - } - } - - /** - * 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. - * - * @return bool - */ - public function check() - { - return ! is_null($this->user()); - } - - /** - * Get the current user of the application. - * - * If the user is a guest, null should be returned. - * - * @return mixed|null - */ - 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 array $arguments - * @return void - */ - abstract public function attempt($arguments = array()); - - /** - * Login the user assigned to the given token. - * - * The token is typically a numeric ID for the user. - * - * @param string $token - * @param bool $remember - * @return bool - */ - public function login($token, $remember = false) - { - $this->token = $token; - - $this->store($token); - - if ($remember) $this->remember($token); - - return true; - } - - /** - * Log the user out of the driver's auth context. - * - * @return void - */ - public function logout() - { - $this->user = null; - - $this->cookie($this->recaller(), null, -2000); - - Session::forget($this->token()); - } - - /** - * Store a user's token in the session. - * - * @param string $token - * @return void - */ - protected function store($token) - { - Session::put($this->token(), $token); - } - - /** - * Store a user's token in a long-lived cookie. - * - * @param string $token - * @return void - */ - protected function remember($token) - { - $token = Crypter::encrypt($token.'|'.Str::random(40)); - - $this->cookie($this->recaller(), $token, Cookie::forever); - } - - /** - * Attempt to find a "remember me" cookie for the user. - * - * @return string|null - */ - protected function recall() - { - $cookie = Cookie::get($this->recaller()); - - // By default, "remember me" cookies are encrypted and contain the user - // token as well as a random string. If it exists, we'll decrypt it - // and return the first segment, which is the user's ID token. - if ( ! is_null($cookie)) - { - return head(explode('|', Crypter::decrypt($cookie))); - } - } - - /** - * Store an authentication cookie. - * - * @param string $name - * @param string $value - * @param int $minutes - * @return void - */ - protected function cookie($name, $value, $minutes) - { - // When setting the default implementation of an authentication - // cookie we'll use the same settings as the session cookie. - // This typically makes sense as they both are sensitive. - $config = Config::get('session'); - - extract($config); - - Cookie::put($name, $value, $minutes, $path, $domain, $secure); - } - - /** - * Get session key name used to store the token. - * - * @return string - */ - protected function token() - { - return $this->name().'_login'; - } - - /** - * Get the name used for the "remember me" cookie. - * - * @return string - */ - protected function recaller() - { - return $this->name().'_remember'; - } - - /** - * Get the name of the driver in a storage friendly format. - * - * @return string - */ - protected function name() - { - return strtolower(str_replace('\\', '_', get_class($this))); - } - +token = Session::get($this->token()); + } + + // If a token did not exist in the session for the user, we will attempt + // to load the value of a "remember me" cookie for the driver, which + // serves as a long-lived client side authenticator for the user. + if (is_null($this->token)) + { + $this->token = $this->recall(); + } + } + + /** + * 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. + * + * @return bool + */ + public function check() + { + return ! is_null($this->user()); + } + + /** + * Get the current user of the application. + * + * If the user is a guest, null should be returned. + * + * @return mixed|null + */ + 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 array $arguments + * @return void + */ + abstract public function attempt($arguments = array()); + + /** + * Login the user assigned to the given token. + * + * The token is typically a numeric ID for the user. + * + * @param string $token + * @param bool $remember + * @return bool + */ + public function login($token, $remember = false) + { + $this->token = $token; + + $this->store($token); + + if ($remember) $this->remember($token); + + return true; + } + + /** + * Log the user out of the driver's auth context. + * + * @return void + */ + public function logout() + { + $this->user = null; + + $this->cookie($this->recaller(), null, -2000); + + Session::forget($this->token()); + } + + /** + * Store a user's token in the session. + * + * @param string $token + * @return void + */ + protected function store($token) + { + Session::put($this->token(), $token); + } + + /** + * Store a user's token in a long-lived cookie. + * + * @param string $token + * @return void + */ + protected function remember($token) + { + $token = Crypter::encrypt($token.'|'.Str::random(40)); + + $this->cookie($this->recaller(), $token, Cookie::forever); + } + + /** + * Attempt to find a "remember me" cookie for the user. + * + * @return string|null + */ + protected function recall() + { + $cookie = Cookie::get($this->recaller()); + + // By default, "remember me" cookies are encrypted and contain the user + // token as well as a random string. If it exists, we'll decrypt it + // and return the first segment, which is the user's ID token. + if ( ! is_null($cookie)) + { + return head(explode('|', Crypter::decrypt($cookie))); + } + } + + /** + * Store an authentication cookie. + * + * @param string $name + * @param string $value + * @param int $minutes + * @return void + */ + protected function cookie($name, $value, $minutes) + { + // When setting the default implementation of an authentication + // cookie we'll use the same settings as the session cookie. + // This typically makes sense as they both are sensitive. + $config = Config::get('session'); + + extract($config); + + Cookie::put($name, $value, $minutes, $path, $domain, $secure); + } + + /** + * Get session key name used to store the token. + * + * @return string + */ + protected function token() + { + return $this->name().'_login'; + } + + /** + * Get the name used for the "remember me" cookie. + * + * @return string + */ + protected function recaller() + { + return $this->name().'_remember'; + } + + /** + * Get the name of the driver in a storage friendly format. + * + * @return string + */ + protected function name() + { + return strtolower(str_replace('\\', '_', get_class($this))); + } + } \ No newline at end of file diff --git a/laravel/cookie.php b/laravel/cookie.php index 88967407..1f79a69b 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -44,7 +44,7 @@ public static function has($name) */ public static function get($name, $default = null) { - if (isset(static::$jar[$name])) return static::$jar[$name]; + if (isset(static::$jar[$name])) return static::$jar[$name]['value']; return array_get(Request::foundation()->cookies->all(), $name, $default); }