PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/Sigpot/AirSpot
PHP | 294 lines | 122 code | 41 blank | 131 comment | 8 complexity | 02f4e502a4c88bd1f5c65b92f2d6be2f MD5 | raw file
  1. <?php
  2. namespace Illuminate\Auth;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Illuminate\Contracts\Auth\Factory as FactoryContract;
  6. class AuthManager implements FactoryContract
  7. {
  8. use CreatesUserProviders;
  9. /**
  10. * The application instance.
  11. *
  12. * @var \Illuminate\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\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 $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 isset($this->guards[$name])
  58. ? $this->guards[$name]
  59. : $this->guards[$name] = $this->resolve($name);
  60. }
  61. /**
  62. * Resolve the given guard.
  63. *
  64. * @param string $name
  65. * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
  66. *
  67. * @throws \InvalidArgumentException
  68. */
  69. protected function resolve($name)
  70. {
  71. $config = $this->getConfig($name);
  72. if (is_null($config)) {
  73. throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
  74. }
  75. if (isset($this->customCreators[$config['driver']])) {
  76. return $this->callCustomCreator($name, $config);
  77. } else {
  78. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  79. if (method_exists($this, $driverMethod)) {
  80. return $this->{$driverMethod}($name, $config);
  81. } else {
  82. throw new InvalidArgumentException("Auth guard driver [{$name}] is not defined.");
  83. }
  84. }
  85. }
  86. /**
  87. * Call a custom driver creator.
  88. *
  89. * @param string $name
  90. * @param array $config
  91. * @return mixed
  92. */
  93. protected function callCustomCreator($name, array $config)
  94. {
  95. return $this->customCreators[$config['driver']]($this->app, $name, $config);
  96. }
  97. /**
  98. * Create a session based authentication guard.
  99. *
  100. * @param string $name
  101. * @param array $config
  102. * @return \Illuminate\Auth\SessionGuard
  103. */
  104. public function createSessionDriver($name, $config)
  105. {
  106. $provider = $this->createUserProvider($config['provider']);
  107. $guard = new SessionGuard($name, $provider, $this->app['session.store']);
  108. // When using the remember me functionality of the authentication services we
  109. // will need to be set the encryption instance of the guard, which allows
  110. // secure, encrypted cookie values to get generated for those cookies.
  111. if (method_exists($guard, 'setCookieJar')) {
  112. $guard->setCookieJar($this->app['cookie']);
  113. }
  114. if (method_exists($guard, 'setDispatcher')) {
  115. $guard->setDispatcher($this->app['events']);
  116. }
  117. if (method_exists($guard, 'setRequest')) {
  118. $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
  119. }
  120. return $guard;
  121. }
  122. /**
  123. * Create a token based authentication guard.
  124. *
  125. * @param string $name
  126. * @param array $config
  127. * @return \Illuminate\Auth\TokenGuard
  128. */
  129. public function createTokenDriver($name, $config)
  130. {
  131. // The token guard implements a basic API token based guard implementation
  132. // that takes an API token field from the request and matches it to the
  133. // user in the database or another persistence layer where users are.
  134. $guard = new TokenGuard(
  135. $this->createUserProvider($config['provider']),
  136. $this->app['request']
  137. );
  138. $this->app->refresh('request', $guard, 'setRequest');
  139. return $guard;
  140. }
  141. /**
  142. * Get the guard configuration.
  143. *
  144. * @param string $name
  145. * @return array
  146. */
  147. protected function getConfig($name)
  148. {
  149. return $this->app['config']["auth.guards.{$name}"];
  150. }
  151. /**
  152. * Get the default authentication driver name.
  153. *
  154. * @return string
  155. */
  156. public function getDefaultDriver()
  157. {
  158. return $this->app['config']['auth.defaults.guard'];
  159. }
  160. /**
  161. * Set the default guard driver the factory should serve.
  162. *
  163. * @param string $name
  164. * @return void
  165. */
  166. public function shouldUse($name)
  167. {
  168. $this->setDefaultDriver($name);
  169. $this->userResolver = function ($name = null) {
  170. return $this->guard($name)->user();
  171. };
  172. }
  173. /**
  174. * Set the default authentication driver name.
  175. *
  176. * @param string $name
  177. * @return void
  178. */
  179. public function setDefaultDriver($name)
  180. {
  181. $this->app['config']['auth.defaults.guard'] = $name;
  182. }
  183. /**
  184. * Register a new callback based request guard.
  185. *
  186. * @param string $driver
  187. * @param callable $callback
  188. * @return $this
  189. */
  190. public function viaRequest($driver, callable $callback)
  191. {
  192. return $this->extend($driver, function () use ($callback) {
  193. $guard = new RequestGuard($callback, $this->app['request']);
  194. $this->app->refresh('request', $guard, 'setRequest');
  195. return $guard;
  196. });
  197. }
  198. /**
  199. * Get the user resolver callback.
  200. *
  201. * @return \Closure
  202. */
  203. public function userResolver()
  204. {
  205. return $this->userResolver;
  206. }
  207. /**
  208. * Set the callback to be used to resolve users.
  209. *
  210. * @param \Closure $userResolver
  211. * @return $this
  212. */
  213. public function resolveUsersUsing(Closure $userResolver)
  214. {
  215. $this->userResolver = $userResolver;
  216. return $this;
  217. }
  218. /**
  219. * Register a custom driver creator Closure.
  220. *
  221. * @param string $driver
  222. * @param \Closure $callback
  223. * @return $this
  224. */
  225. public function extend($driver, Closure $callback)
  226. {
  227. $this->customCreators[$driver] = $callback;
  228. return $this;
  229. }
  230. /**
  231. * Register a custom provider creator Closure.
  232. *
  233. * @param string $name
  234. * @param \Closure $callback
  235. * @return $this
  236. */
  237. public function provider($name, Closure $callback)
  238. {
  239. $this->customProviderCreators[$name] = $callback;
  240. return $this;
  241. }
  242. /**
  243. * Dynamically call the default driver instance.
  244. *
  245. * @param string $method
  246. * @param array $parameters
  247. * @return mixed
  248. */
  249. public function __call($method, $parameters)
  250. {
  251. return call_user_func_array([$this->guard(), $method], $parameters);
  252. }
  253. }