auth.php 3.24 KB
Newer Older
1
<?php namespace Laravel\Security;
2

3
use Laravel\IoC;
4
use Laravel\Config;
5
use Laravel\Session\Payload;
6

7
class Auth {
8 9 10 11 12 13

	/**
	 * The current user of the application.
	 *
	 * @var object
	 */
14
	protected static $user;
15 16

	/**
Taylor Otwell committed
17 18 19 20 21 22 23
	 * The key used when storing the user ID in the session.
	 *
	 * @var string
	 */
	const user_key = 'laravel_user_id';

	/**
24 25 26 27
	 * Determine if the current user of the application is authenticated.
	 *
	 * @return bool
	 */
28
	public static function check()
29
	{
30
		return ! is_null(static::user());
31 32 33 34 35
	}

	/**
	 * Get the current user of the application.
	 *
36 37 38 39 40 41 42 43 44 45
	 * 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>
46
	 *
47 48
	 * @return object
	 */
49
	public static function user()
50
	{
51 52 53
		if ( ! is_null(static::$user)) return static::$user;

		$id = IoC::container()->core('session')->get(Auth::user_key);
54

55
		if (is_null($id) AND ! is_null($cookie = Crypter::decrypt(\Cookie::get('remember'))))
56 57 58 59 60 61
		{
			$cookie = explode('|', $cookie);
			if ($cookie[2] == md5(\Request::server('HTTP_USER_AGENT')))
			{
				$id = $cookie[0];
			}
62 63 64 65 66 67

			if ( ! is_null(static::$user = call_user_func(Config::get('auth.user'), $id)))
			{
				static::login($user);
				return static::$user;
			}
68 69
		}

70
		return static::$user = call_user_func(Config::get('auth.user'), $id);
71 72 73
	}

	/**
74
	 * Attempt to log a user into the application.
75
	 *
76 77
	 * 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.
78
	 *
79 80
	 * @param  string  $username
	 * @param  string  $password
81 82
	 * @param  bool    $remember
	 * @param  int     $ttl - Default is one week.
83
	 * @return bool
84
	 */
85
	public static function attempt($username, $password = null, $remember = false, $ttl = 10080)
86
	{
87
		if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password)))
88
		{
89
			static::login($user);
90

91 92
			if ($remember) static::remember($user);

93
			return true;
94 95 96 97 98 99
		}

		return false;
	}

	/**
100
	 * Log a user into the application.
101
	 *
102
	 * The user ID will be stored in the session so it is available on subsequent requests.
103 104 105 106
	 *
	 * @param  object  $user
	 * @return void
	 */
107
	public static function login($user)
108
	{
109
		static::$user = $user;
110

111
		IoC::container()->core('session')->put(Auth::user_key, $user->id);
112 113 114
	}

	/**
115
	 * Log the current user out of the application.
116
	 *
117 118
	 * The "logout" closure in the authenciation configuration file will be called.
	 *
119 120
	 * @return void
	 */
121
	public static function logout()
122
	{
123
		call_user_func(Config::get('auth.logout'), static::user());
124

125
		static::$user = null;
Taylor Otwell committed
126

127
		IoC::container()->core('session')->forget(Auth::user_key);
Taylor Otwell committed
128
	}
129

130 131 132
	/**
	 * Set a cookie so that users are remembered.
	 *
133 134
	 * @param  object  $user
	 * @param  int     $ttl - Default is one week.
135 136
	 * @return bool
	 */
137
	public static function remember($user, $ttl = 10080)
138 139
	{
		static::$user = $user;
140 141
		$cookie = Crypter::encrypt($user->id.'|'.\Request::ip().'|'.md5(\Request::server('HTTP_USER_AGENT')).'|'.time());
		\Cookie::put('remember', $cookie, $ttl);
142
	}
143
}