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

111 lines
2.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;
use Laravel\Config;
2011-09-29 21:22:48 -05:00
use Laravel\Session\Payload;
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';
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-04 21:43:39 -05:00
* If the current user is not authenticated, null will be returned. This method
* will call the "user" closure in the authentication configuration file.
*
* <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;
$id = IoC::container()->core('session')->get(Auth::user_key);
2011-06-08 23:45:08 -05:00
2011-10-05 18:32:48 -05:00
return static::$user = call_user_func(Config::get('auth.user'), $id);
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-04 21:43:39 -05:00
* If the given credentials are valid, the user will be considered logged into
* the application and their user ID will be stored in the session data.
*
* @param string $username
* @param string $password
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-10-05 18:32:48 -05:00
public static function attempt($username, $password = null)
2011-06-08 23:45:08 -05:00
{
if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password)))
2011-06-08 23:45:08 -05:00
{
2011-10-05 18:32:48 -05:00
static::remember($user);
2011-06-08 23:45:08 -05:00
return true;
2011-06-08 23:45:08 -05:00
}
return false;
}
/**
* Log a user into the application.
*
* The user ID will be stored in the session so it is available on subsequent requests.
*
* @param object $user
* @return void
*/
2011-10-05 18:32:48 -05:00
public static function remember($user)
{
2011-10-05 18:32:48 -05:00
static::$user = $user;
2011-10-05 18:32:48 -05:00
IoC::container()->core('session')->put(Auth::user_key, $user->id);
}
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-04 21:43:39 -05:00
* The "logout" closure in the authenciation configuration file will be called.
*
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
2011-10-05 18:32:48 -05:00
IoC::container()->core('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
}