123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php namespace Laravel;
- class Auth {
-
- public static $user;
-
- const user_key = 'laravel_user_id';
-
- const remember_key = 'laravel_remember';
-
- public static function guest()
- {
- return ! static::check();
- }
-
- public static function check()
- {
- return ! is_null(static::user());
- }
-
- public static function user()
- {
- if ( ! is_null(static::$user)) return static::$user;
- $id = IoC::core('session')->get(Auth::user_key);
-
-
-
-
- $config = Config::get('auth');
- static::$user = call_user_func($config['user'], $id);
-
-
-
-
- $recaller = Cookie::get(Auth::remember_key);
- if (is_null(static::$user) and ! is_null($recaller))
- {
- static::$user = static::recall($recaller);
- }
- return static::$user;
- }
-
- protected static function recall($recaller)
- {
-
-
-
- $recaller = explode('|', Crypter::decrypt($recaller));
-
-
-
- $user = call_user_func(Config::get('auth.user'), $recaller[0]);
- if ( ! is_null($user))
- {
- static::login($user);
- return $user;
- }
- }
-
- public static function attempt($username, $password = null, $remember = false)
- {
- $config = Config::get('auth');
-
-
-
-
-
-
-
- $user = call_user_func($config['attempt'], $username, $password);
-
-
-
- if (is_null($user)) return false;
- static::login($user, $remember);
- return true;
- }
-
- public static function login($user, $remember = false)
- {
- $id = (is_object($user)) ? $user->id : (int) $user;
- if ($remember) static::remember($id);
- Session::put(Auth::user_key, $id);
- }
-
- protected static function remember($id)
- {
- $recaller = Crypter::encrypt($id.'|'.Str::random(40));
-
-
-
-
- $config = Config::get('session');
- extract($config, EXTR_SKIP);
- Cookie::forever(Auth::remember_key, $recaller, $path, $domain, $secure);
- }
-
- public static function logout()
- {
-
-
-
- call_user_func(Config::get('auth.logout'), static::user());
- static::$user = null;
- $config = Config::get('session');
- extract($config, EXTR_SKIP);
-
-
-
- Cookie::forget(Auth::remember_key, $path, $domain, $secure);
- Session::forget(Auth::user_key);
- }
- }
|