fluent.php 1.6 KB

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