/ Published in: PHP
Simple Authentication Library
Expand |
Embed | Plain Text
/** * Auth Class * * Simple authentication library for my blog * * @author Mark A. LaDoux * @copyright Copyright © 2012 Mark LaDoux * @link http://markladoux.com/ * @version 1.0.0 */ class Auth { /** * Internal Variables * * @param object $ci CodeIgniter instance * @param integer $rounds number of iterations to process the hash * @param string $user_table table to use for user data * @param array $errors array of errors */ protected $ci = null; /** * __construct * * prepares the library for first use * * @access public * @since 1.0.0 * @return void */ public function __construct() { // get CodeIgniter Instance $this->ci =& get_instance(); // load required libraries $this->ci->load->library('database'); $this->ci->load->library('session'); // get config settings self::$rounds = $this->ci->config->item('auth_rounds'); self::$user_table = $this->ci->config->item('auth_user_table'); // run a couple tests if(self::$rounds < 4 || self::$rounds > 32) self::$rounds = 8; if(self::$user_table === false) self::$user_table = 'users'; } /** * generate_hash * * generates a random password hash * * @access protected * @since 1.0.0 * @param string $password password to hash * @return string hash of $password */ protected function _generate_hash($password) { // generate a salt $salt = ''; for($i = 0; $i < 22; $i++) { } // format salt // return hash } /** * _verify_hash * * tests to see if password matches hash evaluation * * @access protected * @since 1.0.0 * @param string $password password to check * @param string $stored_hash hash to check password against * @return bool true if passes, else false */ protected function _verify_password($password, $stored_hash) { return $test; } /** * _check_email * * tests if email is valid and available for registration * * @access protected * @since 1.0.0 * @param string $email email address to check * @return void */ protected function _check_email($email) { $valid_email = filter_var($email, FILTER_VALIDATE_EMAIL); if($valid_email) { $this->ci->db->where('email', $email); $this->ci->db->limit(1); $query = $this->db->get(self::$user_table); $in_use = ($query->num_rows() > 0) ? true : false; } if(! $valid_email) { self::$errors['email'] = 'invalid'; } elseif($in_use) { self::$errors['email'] = 'unavailable'; } } /** * _check_username * * tests if username is available for registration * * @access protected * @since 1.0.0 * @param string $username username to check * @return void */ protected function _check_username($username) { $this->ci->db->where('username', $username); $this->ci->db->limit(1); $query = $this->ci->db->get(self::$user_table); if($query->num_rows() > 0) { self::$errors['username'] = 'unavailable'; } } /** * login * * checks a users credentials and logs him in to the system. * * @access public * @since 1.0.0 * @param string $username user to log in * @param string $password user's password * @return bool true if logged in, else false */ public function login($username, $password) { $this->db->where('username', $username); $this->db->limit(1); $query = $this->db->get(self::$user_table); if($query->num_rows() < 1) { $this->ci->session->set_flashdata('login_error', 'invalid_username'); return false; } $data = $query->fetch_assoc(); $valid_password = $this->_verify_password($password, $data['password']); if($valid_password) { // remove password from data array // set the session $data['logged_in'] = true; $this->ci->session->set_userdata($row); return true; } else { $this->ci->session->set_flashdata('login_error', 'invalid_password'); return false; } } /** * logout * * logs user off site. * * @access public * @since 1.0.0 * @return void */ public function logout() { $this->ci->session->sess_destroy(); } /** * create * * creates a new user in the database * * @access public * @since 1.0.0 * @param string $username username to create * @param string $email email address of $username * @param string $password password for $username */ public function create($username, $email, $password) { $this->_check_username($username); $this->_check_email($email); { $this->ci->session->set_flashdata(self::$errors); return false; } $password_hash = $this->_generate_hash($password); $data['username'] = $username; $data['password'] = $password_hash; $data['email'] = $email; $this->ci->db->insert(self::$user_table, $data); return true; } /** * delete * * deletes a user from the database * * @access public * @since 1.0.0 * @param string $username username to remove * @return void */ public function delete($username) { } /** * set_password * * changes the password of an existing user * * @access public * @since 1.0.0 * @param string $username username to edit * @param string $password new password to set * @return void */ public function set_password($username, $password) { $password = $this->_generate_hash($password); $this->ci->db->where('username', $username); } /** * set_email * * changes the email of an existing user * * @access public * @since 1.0.0 * @param string $username username to edit * @param string $email new email address to set * @return void */ public function set_email($username, $email) { $this->_check_email($email); { $this->ci->session->set_flashdata(self::$errors); } else { $this->ci->db->where('username', $username); } } }
You need to login to post a comment.
