auth.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php namespace Laravel\Security;
  2. use Laravel\IoC;
  3. use Laravel\Config;
  4. use Laravel\Session\Payload;
  5. class Auth {
  6. /**
  7. * The current user of the application.
  8. *
  9. * @var object
  10. */
  11. protected static $user;
  12. /**
  13. * The key used when storing the user ID in the session.
  14. *
  15. * @var string
  16. */
  17. const user_key = 'laravel_user_id';
  18. /**
  19. * Determine if the current user of the application is authenticated.
  20. *
  21. * @return bool
  22. */
  23. public static function check()
  24. {
  25. return ! is_null(static::user());
  26. }
  27. /**
  28. * Get the current user of the application.
  29. *
  30. * If the current user is not authenticated, null will be returned. This method
  31. * will call the "user" closure in the authentication configuration file.
  32. *
  33. * <code>
  34. * // Get the current user of the application
  35. * $user = Auth::user();
  36. *
  37. * // Access a property on the current user of the application
  38. * $email = Auth::user()->email;
  39. * </code>
  40. *
  41. * @return object
  42. */
  43. public static function user()
  44. {
  45. if ( ! is_null(static::$user)) return static::$user;
  46. $id = IoC::container()->core('session')->get(Auth::user_key);
  47. return static::$user = call_user_func(Config::get('auth.user'), $id);
  48. }
  49. /**
  50. * Attempt to log a user into the application.
  51. *
  52. * If the given credentials are valid, the user will be considered logged into
  53. * the application and their user ID will be stored in the session data.
  54. *
  55. * @param string $username
  56. * @param string $password
  57. * @return bool
  58. */
  59. public static function attempt($username, $password = null)
  60. {
  61. if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password)))
  62. {
  63. static::remember($user);
  64. return true;
  65. }
  66. return false;
  67. }
  68. /**
  69. * Log a user into the application.
  70. *
  71. * The user ID will be stored in the session so it is available on subsequent requests.
  72. *
  73. * @param object $user
  74. * @return void
  75. */
  76. public static function remember($user)
  77. {
  78. static::$user = $user;
  79. IoC::container()->core('session')->put(Auth::user_key, $user->id);
  80. }
  81. /**
  82. * Log the current user out of the application.
  83. *
  84. * The "logout" closure in the authenciation configuration file will be called.
  85. *
  86. * @return void
  87. */
  88. public static function logout()
  89. {
  90. call_user_func(Config::get('auth.logout'), static::user());
  91. static::$user = null;
  92. IoC::container()->core('session')->forget(Auth::user_key);
  93. }
  94. }