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

115 lines
2.1 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
2011-06-08 23:45:08 -05:00
use Laravel\Config;
use Laravel\Session\Driver;
class Auth {
2011-06-08 23:45:08 -05:00
/**
* The current user of the application.
*
* @var object
*/
protected $user;
2011-08-19 20:12:39 -05:00
2011-08-23 21:04:40 -05:00
/**
* The session driver instance.
2011-06-08 23:45:08 -05:00
*
* @var Session\Driver
2011-06-08 23:45:08 -05:00
*/
protected $session;
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-08-19 20:12:39 -05:00
/**
* Create a new authenticator instance.
2011-08-19 20:12:39 -05:00
*
* @param Session\Driver $session
2011-08-19 20:12:39 -05:00
* @return void
*/
public function __construct(Driver $session)
2011-08-19 20:12:39 -05:00
{
$this->session = $session;
2011-08-19 20:12:39 -05:00
}
2011-06-08 23:45:08 -05:00
/**
* Determine if the current user of the application is authenticated.
*
* @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.
*
* If the current user is not authenticated, NULL will be returned.
*
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
{
if ( ! is_null($this->user)) return $this->user;
2011-06-08 23:45:08 -05:00
2011-09-21 21:46:16 -05:00
return $this->user = call_user_func(Config::get('auth.user'), $this->session->get(Auth::user_key));
2011-06-08 23:45:08 -05:00
}
/**
* Attempt to log a user into the application.
2011-07-07 23:01:44 -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
*/
public 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
{
$this->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-08-19 20:12:39 -05:00
public function remember($user)
{
2011-08-19 20:12:39 -05:00
$this->user = $user;
2011-09-21 21:46:16 -05:00
$this->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
*
* @return void
*/
2011-08-19 20:12:39 -05:00
public function logout()
2011-06-08 23:45:08 -05:00
{
call_user_func(Config::get('auth.logout'), $this->user()->id);
2011-08-19 20:12:39 -05:00
$this->user = null;
2011-09-21 21:46:16 -05:00
$this->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
}