Skip to content

Instantly share code, notes, and snippets.

@thomaslarsson
Created August 11, 2012 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaslarsson/3320510 to your computer and use it in GitHub Desktop.
Save thomaslarsson/3320510 to your computer and use it in GitHub Desktop.
Library_User
<?php
/**
* Library_User
* User class providing authentication
*
* @package Core
* @author Thomas Larsson <thomas@juvenorge.com>
* @copyright (c)2012, Thomas Larsson
*
*/
class Library_User extends Library
{
/**
* Class constructor.
*
* @param Core $core The core system object
*/
public function __construct( Core $core )
{
// Assign core object
$this->core = $core;
}
/**
* login
* Performs a login against the database
*
* @param String $username
* @param String $password
* @return boolean True on valid login, else false
*/
public function login($email, $password)
{
// Check for existing login
if ( $this->is_logged_in() )
{
return true;
}
// Escape email for query
$email = $this->core->database->escape(trim($email));
// Run a query against the DB
$user = $this->core->database->query("SELECT * FROM core_users WHERE email_adress='{$email}' LIMIT 1");
// Make sure we found a user
if ( $user !== null )
{
// Validate password
if ( $this->validate_password($password, $user['password']) )
{
// Destoy old sessions
$this->core->library->session->destroy();
// Assign new user info
$this->assign_user_info($user);
// Correct username/password combo. User logged in
return true;
}
// Wrong password for username
return false;
}
// user not found found
return null;
}
public function logout( )
{
// Set login flag to false
$this->core->session->is_logged_in = false;
// Remove old sessions
$this->core->session->destroy();
}
/**
* is_logged_in
* Will return true if the user is logged in, else false.
*
* @return boolean True if logged in, else false.
*/
public function is_logged_in()
{
return $this->core->library->session->is_logged_in;
}
/**
* has_access
* Performs a access level check on the current user.
* The access check is inclusive.
*
* @param int $required_access_level The minimum access level required.
* @throws Exception
*/
public function has_access($required_access_level)
{
throw new Exception('Not yet implemented');
}
/**
* Simple get_name method to demonstrate valid writing to sessions.
*
* @Carebear
*/
public function get_name()
{
// Get session object
$session =& $this->core->library->session;
return "{$_SESSION['first_name']} {$_SESSION['middle_name']} {$_SESSION['last_name']}";
}
/**
* assign_user_info
* Assigns user info to the session array
*
* @param array $user
*/
private function assign_user_info( array $user )
{
// Don't save passwords
unset($user['password']);
// Add elements to session array
foreach ( $user as $key => $value )
{
$this->core->library->session->$key = $value;
}
// Set logged in to true
$this->core->library->session->is_logged_in = true;
// Set current time
$datetime = new DateTime("now");
$this->core->library->session->last_active = $datetime->format("d.m.Y H.i.s");
}
/**
* validate_password
* Validates a password submitted by a user against a blowfish hash.
*
* @see Library_Hash inside /library for internal workings
* @uses Library_Hash::verify()
*
* @param String $password The user submitted password
* @param String $hash The password's hash
* @return boolean True when password's match, else false
*/
private function validate_password( $password, $hash )
{
return $this->core->library->hash->verify($password, $hash);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment