/laravel_tintuc/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php

https://gitlab.com/nmhieucoder/laravel_tintuc · PHP · 334 lines · 138 code · 49 blank · 147 comment · 7 complexity · d671eaedeeaeac1c90434cb66785b787 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Auth;
  3. use Closure;
  4. use Illuminate\Contracts\Auth\Factory as FactoryContract;
  5. use InvalidArgumentException;
  6. class AuthManager implements FactoryContract
  7. {
  8. use CreatesUserProviders;
  9. /**
  10. * The application instance.
  11. *
  12. * @var \Illuminate\Contracts\Foundation\Application
  13. */
  14. protected $app;
  15. /**
  16. * The registered custom driver creators.
  17. *
  18. * @var array
  19. */
  20. protected $customCreators = [];
  21. /**
  22. * The array of created "drivers".
  23. *
  24. * @var array
  25. */
  26. protected $guards = [];
  27. /**
  28. * The user resolver shared by various services.
  29. *
  30. * Determines the default user for Gate, Request, and the Authenticatable contract.
  31. *
  32. * @var \Closure
  33. */
  34. protected $userResolver;
  35. /**
  36. * Create a new Auth manager instance.
  37. *
  38. * @param \Illuminate\Contracts\Foundation\Application $app
  39. * @return void
  40. */
  41. public function __construct($app)
  42. {
  43. $this->app = $app;
  44. $this->userResolver = function ($guard = null) {
  45. return $this->guard($guard)->user();
  46. };
  47. }
  48. /**
  49. * Attempt to get the guard from the local cache.
  50. *
  51. * @param string|null $name
  52. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
  53. */
  54. public function guard($name = null)
  55. {
  56. $name = $name ?: $this->getDefaultDriver();
  57. return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
  58. }
  59. /**
  60. * Resolve the given guard.
  61. *
  62. * @param string $name
  63. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
  64. *
  65. * @throws \InvalidArgumentException
  66. */
  67. protected function resolve($name)
  68. {
  69. $config = $this->getConfig($name);
  70. if (is_null($config)) {
  71. throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
  72. }
  73. if (isset($this->customCreators[$config['driver']])) {
  74. return $this->callCustomCreator($name, $config);
  75. }
  76. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  77. if (method_exists($this, $driverMethod)) {
  78. return $this->{$driverMethod}($name, $config);
  79. }
  80. throw new InvalidArgumentException(
  81. "Auth driver [{$config['driver']}] for guard [{$name}] is not defined."
  82. );
  83. }
  84. /**
  85. * Call a custom driver creator.
  86. *
  87. * @param string $name
  88. * @param array $config
  89. * @return mixed
  90. */
  91. protected function callCustomCreator($name, array $config)
  92. {
  93. return $this->customCreators[$config['driver']]($this->app, $name, $config);
  94. }
  95. /**
  96. * Create a session based authentication guard.
  97. *
  98. * @param string $name
  99. * @param array $config
  100. * @return \Illuminate\Auth\SessionGuard
  101. */
  102. public function createSessionDriver($name, $config)
  103. {
  104. $provider = $this->createUserProvider($config['provider'] ?? null);
  105. $guard = new SessionGuard($name, $provider, $this->app['session.store']);
  106. // When using the remember me functionality of the authentication services we
  107. // will need to be set the encryption instance of the guard, which allows
  108. // secure, encrypted cookie values to get generated for those cookies.
  109. if (method_exists($guard, 'setCookieJar')) {
  110. $guard->setCookieJar($this->app['cookie']);
  111. }
  112. if (method_exists($guard, 'setDispatcher')) {
  113. $guard->setDispatcher($this->app['events']);
  114. }
  115. if (method_exists($guard, 'setRequest')) {
  116. $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
  117. }
  118. return $guard;
  119. }
  120. /**
  121. * Create a token based authentication guard.
  122. *
  123. * @param string $name
  124. * @param array $config
  125. * @return \Illuminate\Auth\TokenGuard
  126. */
  127. public function createTokenDriver($name, $config)
  128. {
  129. // The token guard implements a basic API token based guard implementation
  130. // that takes an API token field from the request and matches it to the
  131. // user in the database or another persistence layer where users are.
  132. $guard = new TokenGuard(
  133. $this->createUserProvider($config['provider'] ?? null),
  134. $this->app['request'],
  135. $config['input_key'] ?? 'api_token',
  136. $config['storage_key'] ?? 'api_token',
  137. $config['hash'] ?? false
  138. );
  139. $this->app->refresh('request', $guard, 'setRequest');
  140. return $guard;
  141. }
  142. /**
  143. * Get the guard configuration.
  144. *
  145. * @param string $name
  146. * @return array
  147. */
  148. protected function getConfig($name)
  149. {
  150. return $this->app['config']["auth.guards.{$name}"];
  151. }
  152. /**
  153. * Get the default authentication driver name.
  154. *
  155. * @return string
  156. */
  157. public function getDefaultDriver()
  158. {
  159. return $this->app['config']['auth.defaults.guard'];
  160. }
  161. /**
  162. * Set the default guard driver the factory should serve.
  163. *
  164. * @param string $name
  165. * @return void
  166. */
  167. public function shouldUse($name)
  168. {
  169. $name = $name ?: $this->getDefaultDriver();
  170. $this->setDefaultDriver($name);
  171. $this->userResolver = function ($name = null) {
  172. return $this->guard($name)->user();
  173. };
  174. }
  175. /**
  176. * Set the default authentication driver name.
  177. *
  178. * @param string $name
  179. * @return void
  180. */
  181. public function setDefaultDriver($name)
  182. {
  183. $this->app['config']['auth.defaults.guard'] = $name;
  184. }
  185. /**
  186. * Register a new callback based request guard.
  187. *
  188. * @param string $driver
  189. * @param callable $callback
  190. * @return $this
  191. */
  192. public function viaRequest($driver, callable $callback)
  193. {
  194. return $this->extend($driver, function () use ($callback) {
  195. $guard = new RequestGuard($callback, $this->app['request'], $this->createUserProvider());
  196. $this->app->refresh('request', $guard, 'setRequest');
  197. return $guard;
  198. });
  199. }
  200. /**
  201. * Get the user resolver callback.
  202. *
  203. * @return \Closure
  204. */
  205. public function userResolver()
  206. {
  207. return $this->userResolver;
  208. }
  209. /**
  210. * Set the callback to be used to resolve users.
  211. *
  212. * @param \Closure $userResolver
  213. * @return $this
  214. */
  215. public function resolveUsersUsing(Closure $userResolver)
  216. {
  217. $this->userResolver = $userResolver;
  218. return $this;
  219. }
  220. /**
  221. * Register a custom driver creator Closure.
  222. *
  223. * @param string $driver
  224. * @param \Closure $callback
  225. * @return $this
  226. */
  227. public function extend($driver, Closure $callback)
  228. {
  229. $this->customCreators[$driver] = $callback;
  230. return $this;
  231. }
  232. /**
  233. * Register a custom provider creator Closure.
  234. *
  235. * @param string $name
  236. * @param \Closure $callback
  237. * @return $this
  238. */
  239. public function provider($name, Closure $callback)
  240. {
  241. $this->customProviderCreators[$name] = $callback;
  242. return $this;
  243. }
  244. /**
  245. * Determines if any guards have already been resolved.
  246. *
  247. * @return bool
  248. */
  249. public function hasResolvedGuards()
  250. {
  251. return count($this->guards) > 0;
  252. }
  253. /**
  254. * Forget all of the resolved guard instances.
  255. *
  256. * @return $this
  257. */
  258. public function forgetGuards()
  259. {
  260. $this->guards = [];
  261. return $this;
  262. }
  263. /**
  264. * Set the application instance used by the manager.
  265. *
  266. * @param \Illuminate\Contracts\Foundation\Application $app
  267. * @return $this
  268. */
  269. public function setApplication($app)
  270. {
  271. $this->app = $app;
  272. return $this;
  273. }
  274. /**
  275. * Dynamically call the default driver instance.
  276. *
  277. * @param string $method
  278. * @param array $parameters
  279. * @return mixed
  280. */
  281. public function __call($method, $parameters)
  282. {
  283. return $this->guard()->{$method}(...$parameters);
  284. }
  285. }