GuardHelpers.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Illuminate\Auth;
  3. use Illuminate\Contracts\Auth\UserProvider;
  4. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  5. /**
  6. * These methods are typically the same across all guards.
  7. */
  8. trait GuardHelpers
  9. {
  10. /**
  11. * The currently authenticated user.
  12. *
  13. * @var \Illuminate\Contracts\Auth\Authenticatable
  14. */
  15. protected $user;
  16. /**
  17. * The user provider implementation.
  18. *
  19. * @var \Illuminate\Contracts\Auth\UserProvider
  20. */
  21. protected $provider;
  22. /**
  23. * Determine if the current user is authenticated.
  24. *
  25. * @return \Illuminate\Contracts\Auth\Authenticatable
  26. *
  27. * @throws \Illuminate\Auth\AuthenticationException
  28. */
  29. public function authenticate()
  30. {
  31. if (! is_null($user = $this->user())) {
  32. return $user;
  33. }
  34. throw new AuthenticationException;
  35. }
  36. /**
  37. * Determine if the guard has a user instance.
  38. *
  39. * @return bool
  40. */
  41. public function hasUser()
  42. {
  43. return ! is_null($this->user);
  44. }
  45. /**
  46. * Determine if the current user is authenticated.
  47. *
  48. * @return bool
  49. */
  50. public function check()
  51. {
  52. return ! is_null($this->user());
  53. }
  54. /**
  55. * Determine if the current user is a guest.
  56. *
  57. * @return bool
  58. */
  59. public function guest()
  60. {
  61. return ! $this->check();
  62. }
  63. /**
  64. * Get the ID for the currently authenticated user.
  65. *
  66. * @return int|null
  67. */
  68. public function id()
  69. {
  70. if ($this->user()) {
  71. return $this->user()->getAuthIdentifier();
  72. }
  73. }
  74. /**
  75. * Set the current user.
  76. *
  77. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  78. * @return $this
  79. */
  80. public function setUser(AuthenticatableContract $user)
  81. {
  82. $this->user = $user;
  83. return $this;
  84. }
  85. /**
  86. * Get the user provider used by the guard.
  87. *
  88. * @return \Illuminate\Contracts\Auth\UserProvider
  89. */
  90. public function getProvider()
  91. {
  92. return $this->provider;
  93. }
  94. /**
  95. * Set the user provider used by the guard.
  96. *
  97. * @param \Illuminate\Contracts\Auth\UserProvider $provider
  98. * @return void
  99. */
  100. public function setProvider(UserProvider $provider)
  101. {
  102. $this->provider = $provider;
  103. }
  104. }