eloquent.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php namespace Laravel\Auth\Drivers; use Laravel\Hash, Laravel\Config;
  2. class Eloquent extends Driver {
  3. /**
  4. * Get the current user of the application.
  5. *
  6. * If the user is a guest, null should be returned.
  7. *
  8. * @param int|object $token
  9. * @return mixed|null
  10. */
  11. public function retrieve($token)
  12. {
  13. // We return an object here either if the passed token is an integer (ID)
  14. // or if we are passed a model object of the correct type
  15. if (filter_var($token, FILTER_VALIDATE_INT) !== false)
  16. {
  17. return $this->model()->find($token);
  18. }
  19. else if (is_object($token) and get_class($token) == Config::get('auth.model'))
  20. {
  21. return $token;
  22. }
  23. }
  24. /**
  25. * Attempt to log a user into the application.
  26. *
  27. * @param array $arguments
  28. * @return void
  29. */
  30. public function attempt($arguments = array())
  31. {
  32. $user = $this->model()->where(function($query) use($arguments)
  33. {
  34. $username = Config::get('auth.username');
  35. $query->where($username, '=', $arguments['username']);
  36. foreach(array_except($arguments, array('username', 'password', 'remember')) as $column => $val)
  37. {
  38. $query->where($column, '=', $val);
  39. }
  40. })->first();
  41. // If the credentials match what is in the database we will just
  42. // log the user into the application and remember them if asked.
  43. $password = $arguments['password'];
  44. $password_field = Config::get('auth.password', 'password');
  45. if ( ! is_null($user) and Hash::check($password, $user->{$password_field}))
  46. {
  47. return $this->login($user->get_key(), array_get($arguments, 'remember'));
  48. }
  49. return false;
  50. }
  51. /**
  52. * Get a fresh model instance.
  53. *
  54. * @return Eloquent
  55. */
  56. protected function model()
  57. {
  58. $model = Config::get('auth.model');
  59. return new $model;
  60. }
  61. }