Files
spa-api/system/auth.php

120 lines
2.7 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
if (Config::get('session.driver') == '')
{
throw new \Exception("You must specify a session driver before using the Auth class.");
}
2011-06-08 23:45:08 -05:00
class Auth {
/**
* The current user of the application.
*
2011-08-13 22:19:04 -05:00
* If no user is logged in, this will be NULL. Otherwise, it will contain the result
* of the "by_id" closure in the authentication configuration file.
*
2011-08-13 22:19:04 -05:00
* Typically, the user should be accessed via the "user" method.
*
2011-06-08 23:45:08 -05:00
* @var object
*/
public static $user;
/**
* The key used to store the user ID in the session.
*
* @var string
*/
protected static $key = 'laravel_user_id';
2011-06-08 23:45:08 -05:00
/**
* Determine if the current user of the application is authenticated.
*
* @return bool
*/
public static function check()
{
return ! is_null(static::user());
2011-06-08 23:45:08 -05:00
}
/**
* Get the current user of the application.
*
* To retrieve the user, the user ID stored in the session will be passed to
* the "by_id" closure in the authentication configuration file. The result
* of the closure will be cached and returned.
*
2011-06-08 23:45:08 -05:00
* @return object
* @see $user
2011-06-08 23:45:08 -05:00
*/
public static function user()
{
if (is_null(static::$user) and Session::has(static::$key))
{
static::$user = call_user_func(Config::get('auth.by_id'), Session::get(static::$key));
2011-06-08 23:45:08 -05:00
}
return static::$user;
}
/**
* Attempt to log a user into your application.
2011-06-08 23:45:08 -05:00
*
2011-08-11 15:44:29 -05:00
* If the user credentials are valid. The user's ID will be stored in the session and the
* user will be considered "logged in" on subsequent requests to the application.
2011-07-07 23:01:44 -05:00
*
* The password passed to the method should be plain text, as it will be hashed
* by the Hash class when authenticating.
*
2011-06-08 23:45:08 -05:00
* @param string $username
* @param string $password
* @return bool
2011-06-08 23:45:08 -05:00
*/
public static function login($username, $password)
2011-06-08 23:45:08 -05:00
{
if ( ! is_null($user = call_user_func(Config::get('auth.by_username'), $username)))
2011-06-08 23:45:08 -05:00
{
if (Hash::check($password, $user->password))
2011-06-08 23:45:08 -05:00
{
static::remember($user);
2011-06-08 23:45:08 -05:00
return true;
}
}
return false;
}
/**
* Log a user into your application.
*
2011-08-11 15:44:29 -05:00
* The user's ID will be stored in the session and the user will be considered
* "logged in" on subsequent requests to your application.
*
2011-08-14 13:57:52 -05:00
* Note: The user given to this method should be an object having an "id" property.
*
* @param object $user
* @return void
*/
public static function remember($user)
{
static::$user = $user;
Session::put(static::$key, $user->id);
}
2011-06-08 23:45:08 -05:00
/**
* Log the user out of your application.
*
* The user ID will be removed from the session and the user will no longer
* be considered logged in on subsequent requests.
2011-06-08 23:45:08 -05:00
*
* @return void
*/
public static function logout()
{
static::$user = null;
2011-08-11 13:45:17 -05:00
Session::forget(static::$key);
2011-06-08 23:45:08 -05:00
}
}