Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Resolved conflicts from merge with jedd
  • Loading branch information
Adam Thody committed Aug 15, 2009
2 parents cb58fad + aa06844 commit 4344258
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
112 changes: 55 additions & 57 deletions User.php
Expand Up @@ -34,17 +34,19 @@ function User($params = array())
* Insert user into the users table
*
* @access public
* @param array
* @param array $user
* @return bool
*/
function create($user = array())
{
// Make sure required fields are set
// Note: username/password validation criteria are application specific and should't be established here
if (empty($user['username']) || empty($user['password']) || empty($user['email'])) return FALSE;
if (empty($user['username']) OR empty($user['password']) OR empty($user['email']))
return FALSE;

// Return false if username already exists (replace this with more useful error info)
if ($this->check_username($user['username'])) return FALSE;
if ($this->check_username($user['username']))
return FALSE;

// Encrypt password
$user['password'] = $this->_salt($user['password']);
Expand All @@ -65,8 +67,8 @@ function create($user = array())
* Update user in the users table
*
* @access public
* @param string
* @param array
* @param string $user_id
* @param array $user
* @return bool
*/
function update($user_id, $user = array())
Expand Down Expand Up @@ -97,20 +99,13 @@ function update($user_id, $user = array())
* Delete user from the users table
*
* @access public
* @param string
* @param string $identifier Can be user's ID or user's username
* @return bool
*/
function delete($identifier)
{
// Check if we're dealing with the username or the user id
if (is_numeric($identifier))
{
$field = 'id';
}
else
{
$field = 'username';
}
$field = (is_numeric($identifier)) ? "id" : "username";

$this->CI->db->where($field, $identifier);

Expand All @@ -123,23 +118,23 @@ function delete($identifier)
* Login user
*
* @access public
* @param string
* @param string
* @param string $username
* @param string $password Unencrypted version
* @param bool $persistent
* @return bool
*/
function login($username = FALSE, $password = FALSE, $persistent = FALSE)
{
// Make sure $username and $password are set
if ( ! $username || ! $password ) return FALSE;
if ( !( $username AND $password ) )
return FALSE;

// Look for valid user
$user = $this->_test_user_credentials($username, $password);

// Handle failed login
if ( ! $user )
{
return FALSE;
}

// Set initial user session
$this->_set_user_session($user);
Expand All @@ -159,8 +154,8 @@ function login($username = FALSE, $password = FALSE, $persistent = FALSE)
* Test user credentials
*
* @access private
* @param string
* @param string
* @param string $username
* @param string $password Unencrypted version
* @return bool
*/
function _test_user_credentials($username, $password)
Expand All @@ -187,7 +182,7 @@ function _test_user_credentials($username, $password)
* Sets user session
*
* @access private
* @param array
* @param array $user
* @return bool
*/
function _set_user_session($user = array())
Expand All @@ -202,7 +197,7 @@ function _set_user_session($user = array())
* Sets persistent user session
*
* @access private
* @param array
* @param array $user
* @return bool
*/
function _set_persistent_session($user = array())
Expand Down Expand Up @@ -239,7 +234,6 @@ function _set_persistent_session($user = array())
* Checks for a persistent user session
*
* @access public
* @param array
* @return bool
*/
function check_persistent_session()
Expand Down Expand Up @@ -280,8 +274,8 @@ function check_persistent_session()
* Resets the persistent session data
*
* @access private
* @param array
* @param string
* @param array $user
* @param string $token
* @return bool
*/
function _reset_persistent_session($user, $token)
Expand All @@ -299,15 +293,18 @@ function _reset_persistent_session($user, $token)
* Deletes a persistent session data
*
* @access private
* @param string
* @param string
* @param string $username
* @param string $token
* @return bool
*/
function _delete_persistent_session($username, $token)
{
// Delete current db entry
$this->CI->db->where('username', $username);
$this->CI->db->where('token', $token);
$this->CI->db->delete('persistent_sessions');

return TRUE; // @todo We might start checking results on db->delete's laterWe might start checking results on db->delete's later
}


Expand All @@ -318,15 +315,19 @@ function _delete_persistent_session($username, $token)
* Get safe user data
*
* @access private
* @param string
* @return bool
* @param string $username
* @return array
*/
function _get_user_array($username)
{
$this->CI->db->select('id AS user_id, username');
$this->CI->db->where('username', $username);
$query = $this->CI->db->get('users');
return $query->row_array();

if ($query->num_rows() == 1)
return $query->row_array();
else
return FALSE;
}

// --------------------------------------------------------------------
Expand All @@ -347,6 +348,8 @@ function logout()

// Destroy session
$this->CI->session->sess_destroy();

// @todo the only reason we would want to return TRUE here is /if/ we could return FALSE under some circumstances?
return TRUE;
}

Expand All @@ -360,14 +363,7 @@ function logout()
*/
function logged_in()
{
if (is_array($this->CI->session->userdata('user')))
{
return TRUE;
}
else
{
return FALSE;
}
return (is_array($this->CI->session->userdata('user'))) ? TRUE : FALSE;
}

// --------------------------------------------------------------------
Expand All @@ -376,13 +372,13 @@ function logged_in()
* Salt and hash a string
*
* @access private
* @param string
* @param string $string_to_salt
* @return string
*/
function _salt( $string )
function _salt( $string_to_salt )
{
$this->CI->load->helper('security');
return dohash($this->CI->config->item('encryption_key') . $string);
return dohash($this->CI->config->item('encryption_key') . $string_to_salt);
}

// --------------------------------------------------------------------
Expand All @@ -391,13 +387,17 @@ function _salt( $string )
* Gets attr from session array
*
* @access private
* @param string
* @param string $attr_to_get
* @return string
*/
function _get_session_attr($field)
function _get_session_attr($attr_to_get)
{
$user = $this->CI->session->userdata('user');
return $user[$field];

if (isset ($user[$attr_to_get]))
return $user[$attr_to_get];
else
return FALSE;
}

// --------------------------------------------------------------------
Expand All @@ -406,32 +406,30 @@ function _get_session_attr($field)
* Gets user meta
*
* @access public
* @param string
* @param string $attr_to_get
* @return string
*/

function get_meta($field)

function get_meta($attr_to_get)
{
switch ($field)
switch ($attr_to_get)
{
// Pull this meta from the session
case 'user_id' :
return $this->_get_session_attr('user_id');
break;

case 'username' :
return $this->_get_session_attr('username');
return $this->_get_session_attr($attr_to_get);
break;

// Pull this meta from the db
case 'email' :
$this->CI->db->select($field);
$this->CI->db->select($attr_to_get);
$this->CI->db->from('users');
$this->CI->db->where('id', $this->_get_session_attr('user_id'));
break;

default :
$this->CI->db->select($field);
$this->CI->db->select($attr_to_get);
$this->CI->db->from('user_meta');
$this->CI->db->where('user_id', $this->_get_session_attr('user_id'));
break;
Expand All @@ -440,7 +438,7 @@ function get_meta($field)
$query = $this->CI->db->get();
$row = $query->row();

return (!empty($row->{$field})) ? $row->{$field} : NULL;
return (!empty($row->{$attr_to_get})) ? $row->{$attr_to_get} : NULL;
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -481,7 +479,7 @@ function set_meta($field, $value)
* Checks if a username is already in the database
*
* @access public
* @param string
* @param string $username
* @return boolean
*/
function check_username($username)
Expand All @@ -499,7 +497,7 @@ function check_username($username)
* Checks if an email is already in the database
*
* @access public
* @param string
* @param string $email
* @return boolean
*/
function check_email($email)
Expand Down
6 changes: 5 additions & 1 deletion tables.sql
Expand Up @@ -3,6 +3,7 @@
-- --------------------------------------------------------

CREATE TABLE `ci_sessions` (
`id` int(8) unsigned NOT NULL auto_increment,
`session_id` varchar(40) NOT NULL default '0',
`ip_address` varchar(16) NOT NULL default '0',
`user_agent` varchar(50) NOT NULL,
Expand All @@ -13,6 +14,7 @@ CREATE TABLE `ci_sessions` (


CREATE TABLE `persistent_sessions` (
`id` int(8) unsigned NOT NULL auto_increment,
`username` varchar(255) NOT NULL,
`token` varchar(32) NOT NULL,
`date_created` timestamp NOT NULL default CURRENT_TIMESTAMP,
Expand All @@ -29,6 +31,8 @@ CREATE TABLE `users` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `user_meta` (
`id` int(8) unsigned NOT NULL auto_increment,
`user_id` int(8) unsigned NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

0 comments on commit 4344258

Please sign in to comment.