auth.php 2.73 KB
Newer Older
1 2
<?php namespace System;

3 4 5 6 7
if (Config::get('session.driver') == '')
{
	throw new \Exception("You must specify a session driver before using the Auth class.");
}

8 9 10 11 12
class Auth {

	/**
	 * The current user of the application.
	 *
13 14
	 * 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.
15
	 *
16
	 * Typically, the user should be accessed via the "user" method.
17
	 *
18 19 20 21 22 23 24 25 26
	 * @var object
	 */
	public static $user;

	/**
	 * The key used to store the user ID in the session.
	 *
	 * @var string
	 */
27
	protected static $key = 'laravel_user_id';
28 29 30 31 32 33 34 35

	/**
	 * Determine if the current user of the application is authenticated.
	 *
	 * @return bool
	 */
	public static function check()
	{
36
		return ! is_null(static::user());
37 38 39 40 41
	}

	/**
	 * Get the current user of the application.
	 *
42 43 44 45
	 * 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.
	 *
46
	 * @return object
47
	 * @see    $user
48 49 50 51 52
	 */
	public static function user()
	{
		if (is_null(static::$user) and Session::has(static::$key))
		{
53
			static::$user = call_user_func(Config::get('auth.by_id'), Session::get(static::$key));
54 55 56 57 58 59
		}

		return static::$user;
	}

	/**
60
	 * Attempt to log a user into your application.
61
	 *
62 63
	 * 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.
64
	 *
65 66 67
	 * The password passed to the method should be plain text, as it will be hashed
	 * by the Hash class when authenticating.
	 *
68 69
	 * @param  string  $username
	 * @param  string  $password
70
	 * @return bool
71
	 */
72
	public static function login($username, $password)
73
	{
74
		if ( ! is_null($user = call_user_func(Config::get('auth.by_username'), $username)))
75
		{
76
			if (Hash::check($password, $user->password))
77
			{
78
				static::remember($user);
79 80 81 82 83 84 85 86 87

				return true;
			}
		}

		return false;
	}

	/**
88
	 * Log a user into your application.
89
	 *
90
	 * The user's ID will be stored in the session and the user will be considered
91
	 * "logged in" on subsequent requests to your application.
92
	 *
93
	 * Note: The user given to this method should be an object having an "id" property.
94 95 96 97
	 *
	 * @param  object  $user
	 * @return void
	 */
98
	public static function remember($user)
99 100 101 102 103 104 105
	{
		static::$user = $user;

		Session::put(static::$key, $user->id);
	}

	/**
106
	 * Log the user out of your application.
107 108 109
	 *
	 * The user ID will be removed from the session and the user will no longer
	 * be considered logged in on subsequent requests.
110 111 112 113 114 115
	 *
	 * @return void
	 */
	public static function logout()
	{
		static::$user = null;
116 117

		Session::forget(static::$key);
118 119 120
	}

}