From 2c157b77bd08e659c5a7358891819804abfcfde2 Mon Sep 17 00:00:00 2001 From: Artem Daniliants Date: Tue, 16 Mar 2010 15:17:36 +0200 Subject: [PATCH] Finished working on login functionality and fixed some bugs --- Server/application/config/api.php | 2 +- Server/application/config/config.php | 2 +- Server/application/controllers/api.php | 30 +++++++++++++++--------------- Server/application/models/user.php | 24 ++++++++++++------------ 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Server/application/config/api.php b/Server/application/config/api.php index c61e18c..a38951c 100644 --- a/Server/application/config/api.php +++ b/Server/application/config/api.php @@ -7,7 +7,7 @@ * URL where to redirect if no parameters are given to API controller */ $config['default_redirect'] = 'http://www.speedfreak-app.com'; - + /* * Salt for hashing (should always be changed on deployment!) */ diff --git a/Server/application/config/config.php b/Server/application/config/config.php index 5510ddc..688d093 100644 --- a/Server/application/config/config.php +++ b/Server/application/config/config.php @@ -121,5 +121,5 @@ $config['modules'] = array // MODPATH.'gmaps', // Google Maps integration // MODPATH.'archive', // Archive utility // MODPATH.'payment', // Online payments - // MODPATH.'unit_test', // Unit testing + // MODPATH.'unit_test' // Unit testing ); diff --git a/Server/application/controllers/api.php b/Server/application/controllers/api.php index 9692244..6b05e45 100644 --- a/Server/application/controllers/api.php +++ b/Server/application/controllers/api.php @@ -48,27 +48,27 @@ class Api_Controller extends Controller{ } return $xml; } - + /* * Check that supplied credentials are valid using basic authentication * */ public function login(){ - if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){ - $user = new User_Model(); - if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) - print "OK"; - else { - header('HTTP/1.0 401 Unauthorized'); - print "Invalid credentials"; - die; - } - } - else { - header('HTTP/1.0 401 Unauthorized'); + if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){ + $user = new User_Model(); + if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) + print "OK"; + else { + header('HTTP/1.0 401 Unauthorized'); + print "Invalid credentials"; + die; + } + } + else { + header('HTTP/1.0 401 Unauthorized'); print "No credentials supplied"; die; - } - + } + } } \ No newline at end of file diff --git a/Server/application/models/user.php b/Server/application/models/user.php index 95daac7..4440cdf 100644 --- a/Server/application/models/user.php +++ b/Server/application/models/user.php @@ -53,25 +53,25 @@ class User_Model extends Model { * @return bool Returns True if operation was successfull and exception otherwise */ private function register($username, $password, $email){ - // hash password + // hash password $password = $this->hash($password); - + // @todo I can't seem to get query working when password binding has '' around it like others if ($this->user_exists($username, $email)==false) - return $this->db->query("INSERT into users SET username = '?', password = ?, email = '?'", - $username, $password, $email); + return $this->db->query("INSERT into users SET username = '?', password = ?, email = '?'", + $username, $password, $email); else return false; } - + /* * Hash password supplied by user using salt stored in config file - * + * * @param string $password Password in plain text format * @return string Returns string containing hash generated from password */ private function hash($password){ - return sha1($password.Kohana::config('api.salt')); + return sha1($password.Kohana::config('api.salt')); } /* @@ -82,16 +82,16 @@ class User_Model extends Model { * @return bool Returns True if user exists and false otherwise */ private function user_exists($username, $email){ - if ($this->db->query("SELECT id FROM users WHERE username = '?' OR email = '?'", + if ($this->db->query("SELECT id FROM users WHERE username = '?' OR email = '?'", $username, $email)->count()>0) return true; else - return false; + return false; } - + /* * Check if supplied credentials are valid - * + * * @param string $username Username * @param string $password Password in plain text format * @return bool True if credentials match and false if supplied credentials are invalid @@ -99,7 +99,7 @@ class User_Model extends Model { public function login($username, $password){ // hash password $password = $this->hash($password); - + if ($this->db->query("SELECT id FROM users WHERE username = ? AND password = ?", $username, $password)->count()>0) return true; -- 1.7.9.5