eloquent.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 $id
  9. * @return mixed|null
  10. */
  11. public function retrieve($id)
  12. {
  13. if (filter_var($id, FILTER_VALIDATE_INT) !== false)
  14. {
  15. return $this->model()->find($id);
  16. }
  17. }
  18. /**
  19. * Attempt to log a user into the application.
  20. *
  21. * @param array $arguments
  22. * @return void
  23. */
  24. public function attempt($arguments = array())
  25. {
  26. $user = $this->model()->where(function($query) use($arguments) {
  27. $username = Config::get('auth.username');
  28. $query->where($username, '=', $arguments['username']);
  29. foreach( array_except($arguments, array('username', 'password')) as $column => $val )
  30. {
  31. $query->where($column, '=', $val);
  32. }
  33. })->first();
  34. // If the credentials match what is in the database we will just
  35. // log the user into the application and remember them if asked.
  36. $password = $arguments['password'];
  37. $password_field = Config::get('auth.password', 'password');
  38. if ( ! is_null($user) and Hash::check($password, $user->get_attribute($password_field)))
  39. {
  40. return $this->login($user->id, array_get($arguments, 'remember'));
  41. }
  42. return false;
  43. }
  44. /**
  45. * Get a fresh model instance.
  46. *
  47. * @return Eloquent
  48. */
  49. protected function model()
  50. {
  51. $model = Config::get('auth.model');
  52. return new $model;
  53. }
  54. }