PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/soknan/laravel_framework/src/Illuminate/Auth/Guard.php

https://bitbucket.org/helfreire/tccsite
PHP | 557 lines | 226 code | 78 blank | 253 comment | 19 complexity | e60bb1769e69983f20243591a8816be9 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_values(compact('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->user(), $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. $this->clearUserDataFromStorage();
  335. if (isset($this->events))
  336. {
  337. $this->events->fire('auth.logout', array($this->user));
  338. }
  339. $this->user = null;
  340. $this->loggedOut = true;
  341. }
  342. /**
  343. * Remove the user data from the session and cookies.
  344. *
  345. * @return void
  346. */
  347. protected function clearUserDataFromStorage()
  348. {
  349. $this->session->forget($this->getName());
  350. $recaller = $this->getRecallerName();
  351. $this->queuedCookies[] = $this->getCookieJar()->forget($recaller);
  352. }
  353. /**
  354. * Get the cookies queued by the guard.
  355. *
  356. * @return array
  357. */
  358. public function getQueuedCookies()
  359. {
  360. return $this->queuedCookies;
  361. }
  362. /**
  363. * Get the cookie creator instance used by the guard.
  364. *
  365. * @return \Illuminate\Cookie\CookieJar
  366. */
  367. public function getCookieJar()
  368. {
  369. if ( ! isset($this->cookie))
  370. {
  371. throw new \RuntimeException("Cookie jar has not been set.");
  372. }
  373. return $this->cookie;
  374. }
  375. /**
  376. * Set the cookie creator instance used by the guard.
  377. *
  378. * @param \Illuminate\Cookie\CookieJar $cookie
  379. * @return void
  380. */
  381. public function setCookieJar(CookieJar $cookie)
  382. {
  383. $this->cookie = $cookie;
  384. }
  385. /**
  386. * Get the event dispatcher instance.
  387. *
  388. * @return \Illuminate\Events\Dispatcher
  389. */
  390. public function getDispatcher()
  391. {
  392. return $this->events;
  393. }
  394. /**
  395. * Set the event dispatcher instance.
  396. *
  397. * @param \Illuminate\Events\Dispatcher
  398. */
  399. public function setDispatcher(Dispatcher $events)
  400. {
  401. $this->events = $events;
  402. }
  403. /**
  404. * Get the session store used by the guard.
  405. *
  406. * @return \Illuminate\Session\Store
  407. */
  408. public function getSession()
  409. {
  410. return $this->session;
  411. }
  412. /**
  413. * Get the user provider used by the guard.
  414. *
  415. * @return \Illuminate\Auth\UserProviderInterface
  416. */
  417. public function getProvider()
  418. {
  419. return $this->provider;
  420. }
  421. /**
  422. * Return the currently cached user of the application.
  423. *
  424. * @return \Illuminate\Auth\UserInterface|null
  425. */
  426. public function getUser()
  427. {
  428. return $this->user;
  429. }
  430. /**
  431. * Set the current user of the application.
  432. *
  433. * @param \Illuminate\Auth\UserInterface $user
  434. * @return void
  435. */
  436. public function setUser(UserInterface $user)
  437. {
  438. $this->user = $user;
  439. $this->loggedOut = false;
  440. }
  441. /**
  442. * Get the current request instance.
  443. *
  444. * @return \Symfony\Component\HttpFoundation\Request
  445. */
  446. public function getRequest()
  447. {
  448. return $this->request ?: Request::createFromGlobals();
  449. }
  450. /**
  451. * Set the current request instance.
  452. *
  453. * @param \Symfony\Component\HttpFoundation\Request
  454. * @return \Illuminate\Auth\Guard
  455. */
  456. public function setRequest(Request $request)
  457. {
  458. $this->request = $request;
  459. return $this;
  460. }
  461. /**
  462. * Get a unique identifier for the auth session value.
  463. *
  464. * @return string
  465. */
  466. public function getName()
  467. {
  468. return 'login_'.md5(get_class($this));
  469. }
  470. /**
  471. * Get the name of the cookie used to store the "recaller".
  472. *
  473. * @return string
  474. */
  475. public function getRecallerName()
  476. {
  477. return 'remember_'.md5(get_class($this));
  478. }
  479. }