fluent.php 1.57 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5
<?php namespace Laravel\Auth\Drivers;

use Laravel\Hash;
use Laravel\Config;
use Laravel\Database as DB;
6 7 8 9 10 11 12 13

class Fluent extends Driver {

	/**
	 * Get the current user of the application.
	 *
	 * If the user is a guest, null should be returned.
	 *
Chris Berthe committed
14
	 * @param  int  $id
15 16 17 18 19 20
	 * @return mixed|null
	 */
	public function retrieve($id)
	{
		if (filter_var($id, FILTER_VALIDATE_INT) !== false)
		{
Taylor Otwell committed
21
			return DB::table(Config::get('auth.table'))->find($id);
22
		}
23 24 25 26 27
	}

	/**
	 * Attempt to log a user into the application.
	 *
Jeffrey Way committed
28
	 * @param  array $arguments
29 30 31 32
	 * @return void
	 */
	public function attempt($arguments = array())
	{
33
		$user = $this->get_user($arguments);
34

35
		// If the credentials match what is in the database we will just
36 37 38
		// log the user into the application and remember them if asked.
		$password = $arguments['password'];

39
		$password_field = Config::get('auth.password', 'password');
40

41
		if ( ! is_null($user) and Hash::check($password, $user->{$password_field}))
42 43 44 45 46 47 48
		{
			return $this->login($user->id, array_get($arguments, 'remember'));
		}

		return false;
	}

49
	/**
50
	 * Get the user from the database table.
51
	 *
Jeffrey Way committed
52
	 * @param  array  $arguments
53 54
	 * @return mixed
	 */
55
	protected function get_user($arguments)
56 57 58
	{
		$table = Config::get('auth.table');

59 60
		return DB::table($table)->where(function($query) use($arguments)
		{
61 62 63
			$username = Config::get('auth.username');
			
			$query->where($username, '=', $arguments['username']);
64

65
			foreach(array_except($arguments, array('username', 'password', 'remember')) as $column => $val)
66 67 68 69
			{
			    $query->where($column, '=', $val);
			}
		})->first();
70 71
	}

72
}