eloquent.php 1.69 KB
Newer Older
1
<?php namespace Laravel\Auth\Drivers; use Laravel\Hash, Laravel\Config;
2 3 4 5 6 7 8 9

class Eloquent extends Driver {

	/**
	 * Get the current user of the application.
	 *
	 * If the user is a guest, null should be returned.
	 *
10
	 * @param  int|object  $token
11 12
	 * @return mixed|null
	 */
13
	public function retrieve($token)
14
	{
15 16
		// We return an object here either if the passed token is an integer (ID)
		// or if we are passed a model object of the correct type
17
		if (filter_var($token, FILTER_VALIDATE_INT) !== false)
18
		{
19
			return $this->model()->find($token);
20
		}
21
		else if (get_class($token) == Config::get('auth.model'))
22
		{
23
			return $token;
24
		}
25 26 27 28 29
	}

	/**
	 * Attempt to log a user into the application.
	 *
Jeffrey Way committed
30
	 * @param  array $arguments
31 32 33 34
	 * @return void
	 */
	public function attempt($arguments = array())
	{
35 36
		$user = $this->model()->where(function($query) use($arguments)
		{
37 38 39
			$username = Config::get('auth.username');
			
			$query->where($username, '=', $arguments['username']);
40

41
			foreach(array_except($arguments, array('username', 'password', 'remember')) as $column => $val)
42 43 44
			{
			    $query->where($column, '=', $val);
			}
45
		})->first();
46

47 48 49
		// If the credentials match what is in the database we will just
		// log the user into the application and remember them if asked.
		$password = $arguments['password'];
50

51
		$password_field = Config::get('auth.password', 'password');
52

53
		if ( ! is_null($user) and Hash::check($password, $user->{$password_field}))
54
		{
55
			return $this->login($user->get_key(), array_get($arguments, 'remember'));
56
		}
57 58 59 60 61 62 63 64 65 66 67

		return false;
	}

	/**
	 * Get a fresh model instance.
	 *
	 * @return Eloquent
	 */
	protected function model()
	{
68 69 70
		$model = Config::get('auth.model');

		return new $model;
71 72
	}

73
}