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

116 lines
2.2 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
2011-06-08 23:45:08 -05:00
use Laravel\Session\Driver;
class Authenticator {
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
/**
* 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 configuration manager instance.
2011-06-08 23:45:08 -05:00
*
* @var Config
2011-06-08 23:45:08 -05:00
*/
protected $engine;
2011-06-08 23:45:08 -05:00
2011-08-19 20:12:39 -05:00
/**
* Create a new authenticator instance.
2011-08-19 20:12:39 -05:00
*
* @param Config $config
* @param Session\Driver $session
2011-08-19 20:12:39 -05:00
* @return void
*/
public function __construct(Config $config, Driver $session)
2011-08-19 20:12:39 -05:00
{
$this->config = $config;
$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
return $this->user = call_user_func($this->config->get('auth.user'), $this->session->get('laravel_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
*
* 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($this->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;
$this->session->put('laravel_user_id', $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($this->config->get('auth.logout'), $this->user()->id);
2011-08-19 20:12:39 -05:00
$this->user = null;
$this->session->forget('laravel_user_id');
2011-08-19 20:12:39 -05:00
}
2011-08-11 13:45:17 -05:00
2011-06-08 23:45:08 -05:00
}