diff --git a/application/config/auth.php b/application/config/auth.php index 7c3ee766..ae84efbc 100644 --- a/application/config/auth.php +++ b/application/config/auth.php @@ -4,41 +4,61 @@ /* |-------------------------------------------------------------------------- - | Retrieve Users By ID + | Retrieve The Current User |-------------------------------------------------------------------------- | - | This method is called by the Auth::user() method when attempting to - | retrieve a user by their user ID, such as when retrieving a user by the - | user ID stored in the session. + | This closure is called by the Auth::user() method when attempting to + | retrieve a user by their ID stored in the session. | - | You are free to change this method for your application however you wish. + | Simply return an object representing the user with the given ID. Or, if + | no user with the given ID is registered to use your application, you do + | not need to return anything. + | + | Of course, a simple, elegant authentication solution is already provided + | for you using Eloquent and the default Laravel hashing engine. | */ - 'by_id' => function($id) + 'user' => function($id) { - return User::find($id); + if ( ! is_null($id)) return User::find($id); }, /* |-------------------------------------------------------------------------- - | Retrieve Users By Username + | Authenticate User Credentials |-------------------------------------------------------------------------- | - | This method is called by the Auth::check() method when attempting to - | retrieve a user by their username, such as when checking credentials - | received from a login form. + | This closure is called by the Auth::attempt() method when attempting to + | authenticate a user that is logging into your application. | - | You are free to change this method for your application however you wish. + | If the provided credentials are correct, simply return an object that + | represents the user being authenticated. If the credentials are not + | valid, don't return anything. | - | Note: This method must return an object that has "id" and "password" - | properties. The type of object returned does not matter. + | Note: If a user object is returned, it must have an "id" property. | */ - 'by_username' => function($username) + 'attempt' => function($username, $password) { - return User::where_email($username)->first(); + if ( ! is_null($user = User::where('email', '=', $username)->first())) + { + if (Hasher::check($password, $user->password)) return $user; + } }, + /* + |-------------------------------------------------------------------------- + | Logout + |-------------------------------------------------------------------------- + | + | Here you may do anything that needs to be done when a user logs out of + | your application, such as call the logout method on a third-party API + | you are using for authentication, or anything else you desire. + | + */ + + 'logout' => function($id) {} + ); \ No newline at end of file diff --git a/laravel/config/container.php b/laravel/config/container.php index b90de644..6fe96050 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -14,9 +14,9 @@ }), - 'laravel.auth' => array('resolver' => function($container) + 'laravel.auth' => array('singleton' => true, 'resolver' => function($container) { - return new Security\Authenticator($container->resolve('laravel.session'), $container->resolve('laravel.hasher')); + return new Security\Authenticator($container->resolve('laravel.config'), $container->resolve('laravel.session')); }), diff --git a/laravel/security/authenticator.php b/laravel/security/authenticator.php index 1bb0a15f..e0ea9107 100644 --- a/laravel/security/authenticator.php +++ b/laravel/security/authenticator.php @@ -1,6 +1,5 @@ hasher = $hasher; - $this->session = $driver; + $this->config = $config; + $this->session = $session; } /** @@ -64,58 +51,43 @@ public function check() /** * Get the current user of the application. * - * To retrieve the user, the user ID stored in the session will be passed to - * the "by_id" closure in the authentication configuration file. The result - * of the closure will be cached and returned. + * If the current user is not authenticated, NULL will be returned. * * @return object */ public function user() { - if (is_null($this->user) and $this->session->has(static::$key)) - { - $this->user = call_user_func(Config::get('auth.by_id'), $this->session->get(static::$key)); - } + if ( ! is_null($this->user)) return $this->user; - return $this->user; + return $this->user = call_user_func($this->config->get('auth.user'), $this->session->get('laravel_user_id')); } /** - * Attempt to log a user into your application. + * Attempt to log a user into the application. * - * If the user credentials are valid. The user's ID will be stored in the session and the - * user will be considered "logged in" on subsequent requests to the application. + * If the given credentials are valid, the user will be considered logged into the + * application and their user ID will be stored in the session data. * - * The password passed to the method should be plain text, as it will be hashed - * by the Hash class when authenticating. - * - * @param string $username - * @param string $password + * @param string $username + * @param string $password * @return bool */ - public function login($username, $password) + public function attempt($username, $password = null) { - if ( ! is_null($user = call_user_func(Config::get('auth.by_username'), $username))) + if ( ! is_null($user = call_user_func($this->config->get('auth.attempt'), $username, $password))) { - if ($this->hasher->check($password, $user->password)) - { - $this->remember($user); + $this->remember($user); - return true; - } + return true; } return false; } /** - * Log a user into your application. + * Log a user into the application. * - * The user's ID will be stored in the session and the user will be considered - * "logged in" on subsequent requests to your application. This method is called - * by the login method after determining a user's credentials are valid. - * - * Note: The user given to this method should be an object having an "id" property. + * The user ID will be stored in the session so it is available on subsequent requests. * * @param object $user * @return void @@ -124,22 +96,21 @@ public function remember($user) { $this->user = $user; - $this->session->put(static::$key, $user->id); + $this->session->put('laravel_user_id', $user->id); } /** - * Log the user out of your application. - * - * The user ID will be removed from the session and the user will no longer - * be considered logged in on subsequent requests to your application. + * Log the current user out of the application. * * @return void */ public function logout() { + call_user_func($this->config->get('auth.logout'), $this->user()->id); + $this->user = null; - $this->session->forget(static::$key); + $this->session->forget('laravel_user_id'); } } \ No newline at end of file diff --git a/laravel/security/hashing/hasher.php b/laravel/security/hashing/hasher.php deleted file mode 100644 index 15bb228a..00000000 --- a/laravel/security/hashing/hasher.php +++ /dev/null @@ -1,39 +0,0 @@ -engine = $engine - } - - /** - * Magic Method for delegating method calls to the hashing engine. - */ - public function __call($method, $parameters) - { - return call_user_func_array(array($this->engine, $method), $parameters); - } - - /** - * Magic Method for performing methods on the default hashing engine. - */ - public static function __callStatic($method, $parameters) - { - return call_user_func_array(array(static::make()->engine, $method), $parameters); - } - -} \ No newline at end of file