AuthServiceProvider.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Illuminate\Auth;
  3. use Illuminate\Auth\Access\Gate;
  4. use Illuminate\Support\ServiceProvider;
  5. use Illuminate\Contracts\Auth\Access\Gate as GateContract;
  6. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  7. class AuthServiceProvider extends ServiceProvider
  8. {
  9. /**
  10. * Register the service provider.
  11. *
  12. * @return void
  13. */
  14. public function register()
  15. {
  16. $this->registerAuthenticator();
  17. $this->registerUserResolver();
  18. $this->registerAccessGate();
  19. $this->registerRequestRebindHandler();
  20. }
  21. /**
  22. * Register the authenticator services.
  23. *
  24. * @return void
  25. */
  26. protected function registerAuthenticator()
  27. {
  28. $this->app->singleton('auth', function ($app) {
  29. // Once the authentication service has actually been requested by the developer
  30. // we will set a variable in the application indicating such. This helps us
  31. // know that we need to set any queued cookies in the after event later.
  32. $app['auth.loaded'] = true;
  33. return new AuthManager($app);
  34. });
  35. $this->app->singleton('auth.driver', function ($app) {
  36. return $app['auth']->guard();
  37. });
  38. }
  39. /**
  40. * Register a resolver for the authenticated user.
  41. *
  42. * @return void
  43. */
  44. protected function registerUserResolver()
  45. {
  46. $this->app->bind(
  47. AuthenticatableContract::class, function ($app) {
  48. return call_user_func($app['auth']->userResolver());
  49. }
  50. );
  51. }
  52. /**
  53. * Register the access gate service.
  54. *
  55. * @return void
  56. */
  57. protected function registerAccessGate()
  58. {
  59. $this->app->singleton(GateContract::class, function ($app) {
  60. return new Gate($app, function () use ($app) {
  61. return call_user_func($app['auth']->userResolver());
  62. });
  63. });
  64. }
  65. /**
  66. * Register a resolver for the authenticated user.
  67. *
  68. * @return void
  69. */
  70. protected function registerRequestRebindHandler()
  71. {
  72. $this->app->rebinding('request', function ($app, $request) {
  73. $request->setUserResolver(function ($guard = null) use ($app) {
  74. return call_user_func($app['auth']->userResolver(), $guard);
  75. });
  76. });
  77. }
  78. }