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

170 lines
3.8 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
2011-06-08 23:45:08 -05:00
use Laravel\IoC;
use Laravel\Config;
use Laravel\Session\Driver;
class Authenticator {
2011-06-08 23:45:08 -05:00
/**
* 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
*/
2011-08-19 20:12:39 -05:00
public $user;
/**
* The session driver being used by the Auth instance.
*
* @var Session\Driver
*/
protected $session;
2011-06-08 23:45:08 -05:00
2011-08-23 21:04:40 -05:00
/**
* The hashing engine that should be used to perform hashing.
*
* @var Hashing\Engine
2011-08-23 21:04:40 -05:00
*/
protected $hasher;
2011-06-08 23:45:08 -05:00
/**
* 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
2011-08-19 20:12:39 -05:00
/**
* Create a new Auth class instance.
*
2011-08-23 21:04:40 -05:00
* @param Session\Driver $driver
* @param Hashing\Engine $hasher
2011-08-19 20:12:39 -05:00
* @return void
*/
public function __construct(Driver $driver, Hashing\Engine $hasher)
2011-08-19 20:12:39 -05:00
{
2011-08-23 21:04:40 -05:00
$this->hasher = $hasher;
2011-08-19 20:12:39 -05:00
$this->session = $driver;
}
/**
* Create a new Auth class instance.
*
* If no session driver or hasher is provided, the default implementations will be used.
*
* @return Auth
*/
public static function make()
{
return IoC::container()->resolve('laravel.security.auth');
}
2011-06-08 23:45:08 -05:00
/**
* Determine if the current user of the application is authenticated.
*
2011-08-18 19:56:29 -05:00
* @see login()
2011-06-08 23:45:08 -05:00
* @return bool
*/
2011-08-19 20:12:39 -05:00
public function check()
2011-06-08 23:45:08 -05:00
{
2011-08-19 20:12:39 -05:00
return ! is_null($this->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-08-18 19:56:29 -05:00
* <code>
* $email = Auth::user()->email;
* </code>
*
2011-06-08 23:45:08 -05:00
* @return object
*/
2011-08-19 20:12:39 -05:00
public function user()
2011-06-08 23:45:08 -05:00
{
2011-08-19 20:12:39 -05:00
if (is_null($this->user) and $this->session->has(static::$key))
2011-06-08 23:45:08 -05:00
{
2011-08-19 20:12:39 -05:00
$this->user = call_user_func(Config::get('auth.by_id'), $this->session->get(static::$key));
2011-06-08 23:45:08 -05:00
}
2011-08-19 20:12:39 -05:00
return $this->user;
2011-06-08 23:45:08 -05:00
}
/**
* 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-08-18 19:56:29 -05:00
* <code>
* if (Auth::login('email@example.com', 'password'))
* {
* // The credentials are valid and the user is now logged in.
* }
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $username
* @param string $password
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-08-19 20:12:39 -05:00
public 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
{
2011-08-23 21:04:40 -05:00
if ($this->hasher->check($password, $user->password))
2011-06-08 23:45:08 -05:00
{
2011-08-19 20:12:39 -05:00
$this->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
2011-08-18 19:56:29 -05:00
* "logged in" on subsequent requests to your application. This method is called
* by the login method after determining a user's credentials are valid.
*
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
*/
2011-08-19 20:12:39 -05:00
public function remember($user)
{
2011-08-19 20:12:39 -05:00
$this->user = $user;
2011-08-19 20:12:39 -05:00
$this->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
2011-08-18 19:56:29 -05:00
* be considered logged in on subsequent requests to your application.
2011-06-08 23:45:08 -05:00
*
* @return void
*/
2011-08-19 20:12:39 -05:00
public function logout()
2011-06-08 23:45:08 -05:00
{
2011-08-19 20:12:39 -05:00
$this->user = null;
$this->session->forget(static::$key);
}
2011-08-11 13:45:17 -05:00
2011-06-08 23:45:08 -05:00
}