PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/gexge/framework/src/Illuminate/Auth/Guard.php

https://bitbucket.org/helfreire/tccwebservice
PHP | 576 lines | 231 code | 80 blank | 265 comment | 19 complexity | f8945d88ac339474de6b45078226c7c3 MD5 | raw file
  1. <?php namespace Illuminate\Auth;
  2. use Illuminate\Cookie\CookieJar;
  3. use Illuminate\Events\Dispatcher;
  4. use Illuminate\Encryption\Encrypter;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Illuminate\Session\Store as SessionStore;
  7. use Symfony\Component\HttpFoundation\Response;
  8. class Guard {
  9. /**
  10. * The currently authenticated user.
  11. *
  12. * @var UserInterface
  13. */
  14. protected $user;
  15. /**
  16. * The user provider implementation.
  17. *
  18. * @var \Illuminate\Auth\UserProviderInterface
  19. */
  20. protected $provider;
  21. /**
  22. * The session store used by the guard.
  23. *
  24. * @var \Illuminate\Session\Store
  25. */
  26. protected $session;
  27. /**
  28. * The Illuminate cookie creator service.
  29. *
  30. * @var \Illuminate\Cookie\CookieJar
  31. */
  32. protected $cookie;
  33. /**
  34. * The request instance.
  35. *
  36. * @var \Symfony\Component\HttpFoundation\Request
  37. */
  38. protected $request;
  39. /**
  40. * The cookies queued by the guards.
  41. *
  42. * @var array
  43. */
  44. protected $queuedCookies = array();
  45. /**
  46. * The event dispatcher instance.
  47. *
  48. * @var \Illuminate\Events\Dispatcher
  49. */
  50. protected $events;
  51. /**
  52. * Indicates if the logout method has been called.
  53. *
  54. * @var bool
  55. */
  56. protected $loggedOut = false;
  57. /**
  58. * Create a new authentication guard.
  59. *
  60. * @param \Illuminate\Auth\UserProviderInterface $provider
  61. * @param \Illuminate\Session\Store $session
  62. * @return void
  63. */
  64. public function __construct(UserProviderInterface $provider,
  65. SessionStore $session)
  66. {
  67. $this->session = $session;
  68. $this->provider = $provider;
  69. }
  70. /**
  71. * Determine if the current user is authenticated.
  72. *
  73. * @return bool
  74. */
  75. public function check()
  76. {
  77. return ! is_null($this->user());
  78. }
  79. /**
  80. * Determine if the current user is a guest.
  81. *
  82. * @return bool
  83. */
  84. public function guest()
  85. {
  86. return is_null($this->user());
  87. }
  88. /**
  89. * Get the currently authenticated user.
  90. *
  91. * @return \Illuminate\Auth\UserInterface|null
  92. */
  93. public function user()
  94. {
  95. if ($this->loggedOut) return;
  96. // If we have already retrieved the user for the current request we can just
  97. // return it back immediately. We do not want to pull the user data every
  98. // request into the method becaue that would tremendously slow the app.
  99. if ( ! is_null($this->user))
  100. {
  101. return $this->user;
  102. }
  103. $id = $this->session->get($this->getName());
  104. // First we will try to load the user using the identifier in the session if
  105. // one exists. Otherwise we will check for a "remember me" cookie in this
  106. // request, and if one exists, attempt to retrieve the user using that.
  107. $user = null;
  108. if ( ! is_null($id))
  109. {
  110. $user = $this->provider->retrieveByID($id);
  111. }
  112. // If the user is null, but we decrypt a "recaller" cookie we can attempt to
  113. // pull the user data on that cookie which serves as a remember cookie on
  114. // the application. Once we have a user we can return it to the caller.
  115. $recaller = $this->getRecaller();
  116. if (is_null($user) and ! is_null($recaller))
  117. {
  118. $user = $this->provider->retrieveByID($recaller);
  119. }
  120. return $this->user = $user;
  121. }
  122. /**
  123. * Get the decrypted recaller cookie for the request.
  124. *
  125. * @return string|null
  126. */
  127. protected function getRecaller()
  128. {
  129. if (isset($this->cookie))
  130. {
  131. return $this->getCookieJar()->get($this->getRecallerName());
  132. }
  133. }
  134. /**
  135. * Log a user into the application without sessions or cookies.
  136. *
  137. * @param array $credentials
  138. * @return bool
  139. */
  140. public function once(array $credentials = array())
  141. {
  142. if ($this->validate($credentials))
  143. {
  144. $this->setUser($this->provider->retrieveByCredentials($credentials));
  145. return true;
  146. }
  147. return false;
  148. }
  149. /**
  150. * Validate a user's credentials.
  151. *
  152. * @param array $credentials
  153. * @return bool
  154. */
  155. public function validate(array $credentials = array())
  156. {
  157. return $this->attempt($credentials, false, false);
  158. }
  159. /**
  160. * Attempt to authenticate using HTTP Basic Auth.
  161. *
  162. * @param string $field
  163. * @param \Symfony\Component\HttpFoundation\Request $request
  164. * @return \Symfony\Component\HttpFoundation\Response|null
  165. */
  166. public function basic($field = 'email', Request $request = null)
  167. {
  168. if ($this->check()) return;
  169. $request = $request ?: $this->getRequest();
  170. // If a username is set on the HTTP basic request, we will return out without
  171. // interrupting the request lifecycle. Otherwise, we'll need to generate a
  172. // request indicating that the given credentials were invalid for login.
  173. if ($this->attemptBasic($request, $field)) return;
  174. return $this->getBasicResponse();
  175. }
  176. /**
  177. * Perform a stateless HTTP Basic login attempt.
  178. *
  179. * @param string $field
  180. * @param \Symfony\Component\HttpFoundation\Request $request
  181. * @return \Symfony\Component\HttpFoundation\Response|null
  182. */
  183. public function onceBasic($field = 'email', Request $request = null)
  184. {
  185. $request = $request ?: $this->getRequest();
  186. if ( ! $this->once($this->getBasicCredentials($request, $field)))
  187. {
  188. return $this->getBasicResponse();
  189. }
  190. }
  191. /**
  192. * Attempt to authenticate using basic authentication.
  193. *
  194. * @param \Symfony\Component\HttpFoundation\Request $request
  195. * @param string $field
  196. * @return bool
  197. */
  198. protected function attemptBasic(Request $request, $field)
  199. {
  200. if ( ! $request->getUser()) return false;
  201. return $this->attempt($this->getBasicCredentials($request, $field));
  202. }
  203. /**
  204. * Get the credential array for a HTTP Basic request.
  205. *
  206. * @param \Symfony\Component\HttpFoundation\Request $request
  207. * @param string $field
  208. * @return array
  209. */
  210. protected function getBasicCredentials(Request $request, $field)
  211. {
  212. return array($field => $request->getUser(), 'password' => $request->getPassword());
  213. }
  214. /**
  215. * Get the response for basic authentication.
  216. *
  217. * @return \Symfony\Component\HttpFoundation\Response
  218. */
  219. protected function getBasicResponse()
  220. {
  221. $headers = array('WWW-Authenticate' => 'Basic');
  222. return new Response('Invalid credentials.', 401, $headers);
  223. }
  224. /**
  225. * Attempt to authenticate a user using the given credentials.
  226. *
  227. * @param array $credentials
  228. * @param bool $remember
  229. * @param bool $login
  230. * @return bool
  231. */
  232. public function attempt(array $credentials = array(), $remember = false, $login = true)
  233. {
  234. $this->fireAttemptEvent($credentials, $remember, $login);
  235. $user = $this->provider->retrieveByCredentials($credentials);
  236. // If an implementation of UserInterface was returned, we'll ask the provider
  237. // to validate the user against the given credentials, and if they are in
  238. // fact valid we'll log the users into the application and return true.
  239. if ($user instanceof UserInterface)
  240. {
  241. if ($this->provider->validateCredentials($user, $credentials))
  242. {
  243. if ($login) $this->login($user, $remember);
  244. return true;
  245. }
  246. }
  247. return false;
  248. }
  249. /**
  250. * Fire the attempt event with the arguments.
  251. *
  252. * @param array $credentials
  253. * @param bool $remember
  254. * @param bool $login
  255. * @return void
  256. */
  257. protected function fireAttemptEvent(array $credentials, $remember, $login)
  258. {
  259. if ($this->events)
  260. {
  261. $payload = array($credentials, $remember, $login);
  262. $this->events->fire('auth.attempt', $payload);
  263. }
  264. }
  265. /**
  266. * Register an authentication attempt event listener.
  267. *
  268. * @param mixed $callback
  269. * @return void
  270. */
  271. public function attempting($callback)
  272. {
  273. if ($this->events)
  274. {
  275. $this->events->listen('auth.attempt', $callback);
  276. }
  277. }
  278. /**
  279. * Log a user into the application.
  280. *
  281. * @param \Illuminate\Auth\UserInterface $user
  282. * @param bool $remember
  283. * @return void
  284. */
  285. public function login(UserInterface $user, $remember = false)
  286. {
  287. $id = $user->getAuthIdentifier();
  288. $this->session->put($this->getName(), $id);
  289. // If the user should be permanently "remembered" by the application we will
  290. // queue a permanent cookie that contains the encrypted copy of the user
  291. // identifier. We will then decrypt this later to retrieve the users.
  292. if ($remember)
  293. {
  294. $this->queuedCookies[] = $this->createRecaller($id);
  295. }
  296. // If we have an event dispatcher instance set we will fire an event so that
  297. // any listeners will hook into the authentication events and run actions
  298. // based on the login and logout events fired from the guard instances.
  299. if (isset($this->events))
  300. {
  301. $this->events->fire('auth.login', array($user, $remember));
  302. }
  303. $this->setUser($user);
  304. }
  305. /**
  306. * Log the given user ID into the application.
  307. *
  308. * @param mixed $id
  309. * @param bool $remember
  310. * @return \Illuminate\Auth\UserInterface
  311. */
  312. public function loginUsingId($id, $remember = false)
  313. {
  314. $this->session->put($this->getName(), $id);
  315. return $this->login($this->provider->retrieveById($id), $remember);
  316. }
  317. /**
  318. * Create a remember me cookie for a given ID.
  319. *
  320. * @param mixed $id
  321. * @return \Symfony\Component\HttpFoundation\Cookie
  322. */
  323. protected function createRecaller($id)
  324. {
  325. return $this->getCookieJar()->forever($this->getRecallerName(), $id);
  326. }
  327. /**
  328. * Log the user out of the application.
  329. *
  330. * @return void
  331. */
  332. public function logout()
  333. {
  334. $user = $this->user();
  335. // If we have an event dispatcher instance, we can fire off the logout event
  336. // so any further processing can be done. This allows the developer to be
  337. // listening for anytime a user signs out of this application manually.
  338. $this->clearUserDataFromStorage();
  339. if (isset($this->events))
  340. {
  341. $this->events->fire('auth.logout', array($user));
  342. }
  343. // Once we have fired the logout event we will clear the users out of memory
  344. // so they are no longer available as the user is no longer considered as
  345. // being signed into this application and should not be available here.
  346. $this->user = null;
  347. $this->loggedOut = true;
  348. }
  349. /**
  350. * Remove the user data from the session and cookies.
  351. *
  352. * @return void
  353. */
  354. protected function clearUserDataFromStorage()
  355. {
  356. $this->session->forget($this->getName());
  357. $recaller = $this->getRecallerName();
  358. $this->queuedCookies[] = $this->getCookieJar()->forget($recaller);
  359. }
  360. /**
  361. * Get the cookies queued by the guard.
  362. *
  363. * @return array
  364. */
  365. public function getQueuedCookies()
  366. {
  367. return $this->queuedCookies;
  368. }
  369. /**
  370. * Get the cookie creator instance used by the guard.
  371. *
  372. * @return \Illuminate\Cookie\CookieJar
  373. */
  374. public function getCookieJar()
  375. {
  376. if ( ! isset($this->cookie))
  377. {
  378. throw new \RuntimeException("Cookie jar has not been set.");
  379. }
  380. return $this->cookie;
  381. }
  382. /**
  383. * Set the cookie creator instance used by the guard.
  384. *
  385. * @param \Illuminate\Cookie\CookieJar $cookie
  386. * @return void
  387. */
  388. public function setCookieJar(CookieJar $cookie)
  389. {
  390. $this->cookie = $cookie;
  391. }
  392. /**
  393. * Get the event dispatcher instance.
  394. *
  395. * @return \Illuminate\Events\Dispatcher
  396. */
  397. public function getDispatcher()
  398. {
  399. return $this->events;
  400. }
  401. /**
  402. * Set the event dispatcher instance.
  403. *
  404. * @param \Illuminate\Events\Dispatcher
  405. */
  406. public function setDispatcher(Dispatcher $events)
  407. {
  408. $this->events = $events;
  409. }
  410. /**
  411. * Get the session store used by the guard.
  412. *
  413. * @return \Illuminate\Session\Store
  414. */
  415. public function getSession()
  416. {
  417. return $this->session;
  418. }
  419. /**
  420. * Get the user provider used by the guard.
  421. *
  422. * @return \Illuminate\Auth\UserProviderInterface
  423. */
  424. public function getProvider()
  425. {
  426. return $this->provider;
  427. }
  428. /**
  429. * Set the user provider used by the guard.
  430. *
  431. * @param \Illuminate\Auth\UserProviderInterface $provider
  432. * @return void
  433. */
  434. public function setProvider(UserProviderInterface $provider)
  435. {
  436. $this->provider = $provider;
  437. }
  438. /**
  439. * Return the currently cached user of the application.
  440. *
  441. * @return \Illuminate\Auth\UserInterface|null
  442. */
  443. public function getUser()
  444. {
  445. return $this->user;
  446. }
  447. /**
  448. * Set the current user of the application.
  449. *
  450. * @param \Illuminate\Auth\UserInterface $user
  451. * @return void
  452. */
  453. public function setUser(UserInterface $user)
  454. {
  455. $this->user = $user;
  456. $this->loggedOut = false;
  457. }
  458. /**
  459. * Get the current request instance.
  460. *
  461. * @return \Symfony\Component\HttpFoundation\Request
  462. */
  463. public function getRequest()
  464. {
  465. return $this->request ?: Request::createFromGlobals();
  466. }
  467. /**
  468. * Set the current request instance.
  469. *
  470. * @param \Symfony\Component\HttpFoundation\Request
  471. * @return \Illuminate\Auth\Guard
  472. */
  473. public function setRequest(Request $request)
  474. {
  475. $this->request = $request;
  476. return $this;
  477. }
  478. /**
  479. * Get a unique identifier for the auth session value.
  480. *
  481. * @return string
  482. */
  483. public function getName()
  484. {
  485. return 'login_'.md5(get_class($this));
  486. }
  487. /**
  488. * Get the name of the cookie used to store the "recaller".
  489. *
  490. * @return string
  491. */
  492. public function getRecallerName()
  493. {
  494. return 'remember_'.md5(get_class($this));
  495. }
  496. }