Files
spa-api/laravel/security/auth.php

183 lines
4.3 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
2011-06-08 23:45:08 -05:00
2011-10-05 18:32:48 -05:00
use Laravel\IoC;
2011-10-10 21:34:15 -05:00
use Laravel\Str;
use Laravel\Config;
use Laravel\Cookie;
2011-10-10 21:34:15 -05:00
use Laravel\Session\Manager as Session;
class Auth {
2011-06-08 23:45:08 -05:00
/**
* The current user of the application.
*
* @var object
*/
2011-10-05 18:32:48 -05:00
protected 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
/**
* Determine if the current user of the application is authenticated.
*
* @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-10 21:34:15 -05:00
* This method will call the "user" closure in the authentication configuration file.
2011-10-15 14:04:11 -05:00
* If the user is not authenticated, null will be returned by the methd.
2011-10-10 21:34:15 -05:00
*
* If no user exists in the session, the method will check for a "remember me"
* cookie and attempt to login the user based on the value of that cookie.
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>
*
2011-06-08 23:45:08 -05:00
* @return object
*/
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;
2011-10-12 23:15:10 -05:00
static::$user = call_user_func(Config::get('auth.user'), Session::get(Auth::user_key));
2011-10-09 12:49:41 -04:00
if (is_null(static::$user) and ! is_null($cookie = Cookie::get(Auth::remember_key)))
{
static::$user = static::recall($cookie);
}
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-10-15 14:04:11 -05:00
* We should be able to trust the cookie is valid, since all cookies
* set by Laravel include a fingerprint hash. So, the cookie should
* be safe to use within this method.
*
* @param string $cookie
* @return mixed
*/
protected static function recall($cookie)
{
2011-10-10 21:34:15 -05:00
$cookie = explode('|', Crypter::decrypt($cookie));
2011-10-15 14:04:11 -05:00
if ( ! is_null($user = call_user_func(Config::get('auth.user'), $cookie[0])))
{
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
*
2011-10-15 14:04:11 -05:00
* If the credentials are valid, the user will be logged into the application
* and their user ID will be stored in the session via the "login" method.
*
2011-10-15 14:04:11 -05:00
* The user may also be "remembered", which will keep the user logged into the
* application for one year or until they logout. The user is rememberd via
* an encrypted cookie.
*
* @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');
if ( ! is_null($user = call_user_func($config['attempt'], $username, $password, $config)))
2011-06-08 23:45:08 -05:00
{
2011-10-10 21:34:15 -05:00
static::login($user, $config, $remember);
return true;
2011-06-08 23:45:08 -05:00
}
return false;
}
/**
* Log a user into the application.
*
* @param object $user
* @param bool $remember
* @return void
*/
public static function login($user, $remember = false)
{
2011-10-05 18:32:48 -05:00
static::$user = $user;
2011-10-15 14:04:11 -05:00
if ($remember) static::remember($user->id);
2011-10-12 23:15:10 -05:00
Session::put(Auth::user_key, $user->id);
}
/**
* Set a cookie so that users are "remembered" and don't need to login.
*
* @param string $id
* @return void
*/
2011-10-15 14:04:11 -05:00
protected static function remember($id)
{
2011-10-15 14:04:11 -05:00
$cookie = 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
// to assume the settings are the same.
$config = Config::get('session');
Cookie::forever(Auth::remember_key, $cookie, $config['path'], $config['domain'], $config['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
*
2011-10-15 14:04:11 -05:00
* The "logout" closure in the authenciation configuration file will be
* called. All authentication cookies will be deleted and the user ID
* will be removed from the session.
2011-10-04 21:43:39 -05:00
*
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
{
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
Cookie::forget(Auth::user_key);
Cookie::forget(Auth::remember_key);
2011-10-12 23:15:10 -05:00
Session::forget(Auth::user_key);
2011-08-19 20:12:39 -05:00
}
2011-08-11 13:45:17 -05:00
2011-06-08 23:45:08 -05:00
}