auth.php 5.28 KB
Newer Older
1
<?php namespace Laravel;
2

3
class Auth {
4 5 6 7 8 9

	/**
	 * The current user of the application.
	 *
	 * @var object
	 */
10
	public static $user;
11 12

	/**
Taylor Otwell committed
13 14 15 16 17 18 19
	 * The key used when storing the user ID in the session.
	 *
	 * @var string
	 */
	const user_key = 'laravel_user_id';

	/**
Taylor Otwell committed
20 21 22 23 24 25 26 27 28 29 30 31 32
	 * Determine if the user of the application is not logged in.
	 *
	 * This method is the inverse of the "check" method.
	 *
	 * @return bool
	 */
	public static function guest()
	{
		return ! static::check();
	}

	/**
	 * Determine if the user of the application is logged in.
33 34 35
	 *
	 * @return bool
	 */
36
	public static function check()
37
	{
38
		return ! is_null(static::user());
39 40 41 42 43
	}

	/**
	 * Get the current user of the application.
	 *
44 45 46 47 48 49 50
	 * <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>
51
	 *
52
	 * @return object|null
53
	 */
54
	public static function user()
55
	{
56 57
		if ( ! is_null(static::$user)) return static::$user;

58
		$id = Session::get(Auth::user_key);
59

60 61 62 63 64
		// To retrieve the user, we'll first attempt to use the "user" Closure
		// defined in the auth configuration file, passing in the ID. The user
		// Closure gives the developer a ton of freedom surrounding how the
		// user is actually retrieved.
		$config = Config::get('auth');
65

66 67 68 69 70 71
		static::$user = call_user_func($config['user'], $id);

		// If the user wasn't found in the database but a "remember me" cookie
		// exists, we'll attempt to recall the user based on the cookie value.
		// Since all cookies contain a fingerprint hash verifying that they
		// haven't changed, we can trust it.
72
		$recaller = Cookie::get($config['cookie']);
73 74

		if (is_null(static::$user) and ! is_null($recaller))
75
		{
76
			static::$user = static::recall($recaller);
77 78
		}

79
		return static::$user;
80 81 82
	}

	/**
83 84
	 * Attempt to login a user based on a long-lived "remember me" cookie.
	 *
85
	 * @param  string  $recaller
86 87
	 * @return mixed
	 */
88
	protected static function recall($recaller)
89
	{
90
		$recaller = explode('|', Crypter::decrypt($recaller));
91

92 93 94 95 96 97
		// We'll pass the ID that was stored in the cookie into the same user
		// Closure that is used by the "user" method. If the method returns
		// a user, we will log them into the application.
		$user = call_user_func(Config::get('auth.user'), $recaller[0]);

		if ( ! is_null($user))
98 99 100
		{
			static::login($user);

Taylor Otwell committed
101 102
			return $user;
		}
103 104 105
	}

	/**
106
	 * Attempt to log a user into the application.
107
	 *
108 109 110
	 * <code>
	 *		// Attempt to log a user into the application
	 *		$success = Auth::attempt('username', 'password');
111
	 *
112 113 114
	 *		// Attempt to login a user and set the "remember me" cookie
	 *		Auth::attempt('username', 'password', true);
	 * </code>
115
	 *
116 117
	 * @param  string  $username
	 * @param  string  $password
118
	 * @param  bool    $remember
119
	 * @return bool
120
	 */
121
	public static function attempt($username, $password = null, $remember = false)
122
	{
123 124
		$config = Config::get('auth');

125 126
		// When attempting to login the user, we will call the "attempt" closure
		// from the configuration file. This gives the developer the freedom to
127 128
		// authenticate based on the needs of their application, even allowing
		// the user of third-party providers.
129
		$user = call_user_func($config['attempt'], $username, $password);
130

131
		if (is_null($user)) return false;
132

133
		static::login($user, $remember);
134

135
		return true;
136 137 138
	}

	/**
139
	 * Log a user into the application.
140
	 *
141 142 143 144
	 * <code>
	 *		// Login the user with an ID of 15
	 *		Auth::login(15);
	 *
145 146 147
	 *		// Login a user by passing a user object
	 *		Auth::login($user);
	 *
148 149 150 151 152 153
	 *		// Login a user and set a "remember me" cookie
	 *		Auth::login($user, true);
	 * </code>
	 *
	 * @param  object|int  $user
	 * @param  bool        $remember
154 155
	 * @return void
	 */
156
	public static function login($user, $remember = false)
157
	{
158
		$id = (is_object($user)) ? $user->id : (int) $user;
159

160
		if ($remember) static::remember($id);
161

162
		Session::put(Auth::user_key, $id);
163 164 165
	}

	/**
166
	 * Set a cookie so that the user is "remembered".
167 168 169 170
	 *
	 * @param  string  $id
	 * @return void
	 */
171
	protected static function remember($id)
172
	{
173
		$recaller = Crypter::encrypt($id.'|'.Str::random(40));
174

175 176
		// This method assumes the "remember me" cookie should have the same
		// configuration as the session cookie. Since this cookie, like the
177
		// session cookie, should be kept very secure, it's probably safe.
178
		// to assume the cookie settings are the same.
179 180
		$config = Config::get('session');

181 182
		extract($config, EXTR_SKIP);

183 184 185
		$cookie = Config::get('auth.cookie');

		Cookie::forever($cookie, $recaller, $path, $domain, $secure);
186 187 188
	}

	/**
189
	 * Log the current user out of the application.
190 191 192
	 *
	 * @return void
	 */
193
	public static function logout()
194
	{
195 196 197
		// We will call the "logout" closure first, which gives the developer
		// the chance to do any clean-up or before the user is logged out of
		// the application. No action is taken by default.
198
		call_user_func(Config::get('auth.logout'), static::user());
199

200
		static::$user = null;
Taylor Otwell committed
201

202 203 204 205 206 207 208
		$config = Config::get('session');

		extract($config, EXTR_SKIP);

		// When forgetting the cookie, we need to also pass in the path and
		// domain that would have been used when the cookie was originally
		// set by the framework, otherwise it will not be deleted.
209 210 211
		$cookie = Config::get('auth.cookie');

		Cookie::forget($cookie, $path, $domain, $secure);
212

213
		Session::forget(Auth::user_key);
Taylor Otwell committed
214
	}
215

216
}