Files
spa-api/laravel/auth.php

228 lines
5.9 KiB
PHP
Raw Normal View History

<?php namespace Laravel;
class Auth {
2011-06-08 23:45:08 -05:00
/**
* The current user of the application.
*
* @var object
*/
2012-01-16 13:59:24 -06:00
public static $user;
2011-06-08 23:45:08 -05:00
2011-09-21 21:46:16 -05:00
/**
* The key used when storing the user ID in the session.
*
* @var string
*/
const user_key = 'laravel_user_id';
/**
* The key used when setting the "remember me" cookie.
*
* @var string
*/
const remember_key = 'laravel_remember';
2011-06-08 23:45:08 -05:00
/**
2011-11-02 21:27:43 -05:00
* Determine if the user of the application is not logged in.
*
* This method is the inverse of the "check" method.
*
* @return bool
*/
public static function guest()
{
return ! static::check();
}
/**
* Determine if the user of the application is logged in.
2011-06-08 23:45:08 -05:00
*
* @return bool
*/
2011-10-05 18:32:48 -05:00
public static function check()
2011-06-08 23:45:08 -05:00
{
2011-10-05 18:32:48 -05:00
return ! is_null(static::user());
2011-06-08 23:45:08 -05:00
}
/**
* Get the current user of the application.
*
2011-10-04 21:43:39 -05:00
* <code>
* // Get the current user of the application
* $user = Auth::user();
*
* // Access a property on the current user of the application
* $email = Auth::user()->email;
* </code>
*
2012-01-16 13:59:24 -06:00
* @return object|null
2011-06-08 23:45:08 -05:00
*/
2011-10-05 18:32:48 -05:00
public static function user()
2011-06-08 23:45:08 -05:00
{
2011-10-05 18:32:48 -05:00
if ( ! is_null(static::$user)) return static::$user;
2012-01-16 13:59:24 -06:00
$id = Session::get(Auth::user_key);
2012-01-16 13:59:24 -06:00
// To retrieve the user, we'll first attempt to use the "user" Closure
// defined in the auth configuration file, passing in the ID. The user
// Closure gives the developer a ton of freedom surrounding how the
// user is actually retrieved.
$config = Config::get('auth');
2011-10-09 12:49:41 -04:00
2012-01-16 13:59:24 -06:00
static::$user = call_user_func($config['user'], $id);
// If the user wasn't found in the database but a "remember me" cookie
// exists, we'll attempt to recall the user based on the cookie value.
// Since all cookies contain a fingerprint hash verifying that they
// haven't changed, we can trust it.
2011-11-22 18:00:17 -06:00
$recaller = Cookie::get(Auth::remember_key);
if (is_null(static::$user) and ! is_null($recaller))
{
2011-11-22 18:00:17 -06:00
static::$user = static::recall($recaller);
}
2011-10-09 12:49:41 -04:00
return static::$user;
2011-06-08 23:45:08 -05:00
}
/**
* Attempt to login a user based on a long-lived "remember me" cookie.
*
2011-11-22 18:00:17 -06:00
* @param string $recaller
* @return mixed
*/
2011-11-22 18:00:17 -06:00
protected static function recall($recaller)
{
2012-01-16 13:59:24 -06:00
// When the remember me cookie is stored, it is encrypted and contains
// the user's ID and a long, random string. The segments are separated
// by a pipe character so we'll explode on that.
2011-11-22 18:00:17 -06:00
$recaller = explode('|', Crypter::decrypt($recaller));
2012-01-16 13:59:24 -06:00
// We'll pass the ID that was stored in the cookie into the same user
// Closure that is used by the "user" method. If the method returns
// a user, we will log them into the application.
$user = call_user_func(Config::get('auth.user'), $recaller[0]);
if ( ! is_null($user))
{
static::login($user);
2011-10-10 21:34:15 -05:00
return $user;
}
}
2011-06-08 23:45:08 -05:00
/**
* Attempt to log a user into the application.
2011-07-07 23:01:44 -05:00
*
2012-01-16 13:59:24 -06:00
* <code>
* // Attempt to log a user into the application
* $success = Auth::attempt('username', 'password');
*
2012-01-16 13:59:24 -06:00
* // Attempt to login a user and set the "remember me" cookie
* Auth::attempt('username', 'password', true);
* </code>
*
* @param string $username
* @param string $password
2011-10-09 11:57:00 -04:00
* @param bool $remember
* @return bool
2011-06-08 23:45:08 -05:00
*/
public static function attempt($username, $password = null, $remember = false)
2011-06-08 23:45:08 -05:00
{
$config = Config::get('auth');
2012-01-16 13:59:24 -06:00
// When attempting to login the user, we will call the "attempt" closure
// from the configuration file. This gives the developer the freedom to
// authenticate based on the needs of their application.
//
// All of the password hashing and checking and left totally up to the
// developer, as this gives them the freedom to use any hashing scheme
// or authentication provider they wish.
$user = call_user_func($config['attempt'], $username, $password);
2011-10-20 21:44:18 -05:00
2012-01-16 13:59:24 -06:00
// If the user credentials were authenticated by the closure, we will
// log the user into the application, which will store their user ID
// in the session for subsequent requests.
if (is_null($user)) return false;
2012-01-16 13:59:24 -06:00
static::login($user, $remember);
2011-06-08 23:45:08 -05:00
2012-01-16 13:59:24 -06:00
return true;
2011-06-08 23:45:08 -05:00
}
/**
* Log a user into the application.
*
* <code>
* // Login the user with an ID of 15
* Auth::login(15);
*
2012-01-16 13:59:24 -06:00
* // Login a user by passing a user object
* Auth::login($user);
*
* // Login a user and set a "remember me" cookie
* Auth::login($user, true);
* </code>
*
* @param object|int $user
* @param bool $remember
* @return void
*/
public static function login($user, $remember = false)
{
$id = (is_object($user)) ? $user->id : (int) $user;
if ($remember) static::remember($id);
2012-01-16 13:59:24 -06:00
Session::put(Auth::user_key, $id);
}
/**
2012-01-16 13:59:24 -06:00
* Set a cookie so that the user is "remembered".
*
* @param string $id
* @return void
*/
2011-10-15 14:04:11 -05:00
protected static function remember($id)
{
2011-11-22 18:00:17 -06:00
$recaller = Crypter::encrypt($id.'|'.Str::random(40));
2011-10-15 14:04:11 -05:00
// This method assumes the "remember me" cookie should have the same
// configuration as the session cookie. Since this cookie, like the
// session cookie, should be kept very secure, it's probably safe
2012-01-16 13:59:24 -06:00
// to assume the settings are the same for this cookie.
$config = Config::get('session');
2011-10-29 21:21:36 -05:00
extract($config, EXTR_SKIP);
2011-11-22 18:00:17 -06:00
Cookie::forever(Auth::remember_key, $recaller, $path, $domain, $secure);
}
2011-06-08 23:45:08 -05:00
/**
* Log the current user out of the application.
2011-06-08 23:45:08 -05:00
*
* @return void
*/
2011-10-05 18:32:48 -05:00
public static function logout()
2011-06-08 23:45:08 -05:00
{
2012-01-16 13:59:24 -06:00
// We will call the "logout" closure first, which gives the developer
// the chance to do any clean-up or before the user is logged out of
// the application. No action is taken by default.
2011-10-05 18:32:48 -05:00
call_user_func(Config::get('auth.logout'), static::user());
2011-10-05 18:32:48 -05:00
static::$user = null;
2011-08-19 20:12:39 -05:00
2012-01-05 11:52:42 -06:00
$config = Config::get('session');
extract($config, EXTR_SKIP);
// When forgetting the cookie, we need to also pass in the path and
// domain that would have been used when the cookie was originally
// set by the framework, otherwise it will not be deleted.
Cookie::forget(Auth::remember_key, $path, $domain, $secure);
2012-01-16 13:59:24 -06:00
Session::forget(Auth::user_key);
2011-08-19 20:12:39 -05:00
}
2011-08-11 13:45:17 -05:00
2011-11-13 15:23:48 -06:00
}