/ Published in: PHP
A simple user management library. It makes no attempt at handling sessions or cookies, I'll leave that part up to you. It's just a drop in that will allow you to create users, update passwords, and make sure that user supplied information is correct. Should easily integrate into any application setting that you might be able to think of. The library is completely database agnostic, as it makes use of the dbforge library to automatically generate databases on first use. As long as CodeIgniter supports the database, this library will work with it.
Expand |
Embed | Plain Text
/** * mAuth Authentication Library * * Simple authentication library for CodeIgniter 2. This class does not * make any attempts at handling access control, so you will need another * library or helper to do that for you. This library also doesn't perform any * cookie or session management, that should be left up to your individual * application. This is a simple drop in user creation and verification library. * * @author Mark LaDoux <[email protected]> * @version 20120708 * @copyright Copyright (c) 2012, Mark LaDoux */ class mAuth { /** * CodeIgniter Object * * @access protected * @since 20120708 * @var object */ protected $ci; /** * Database table to use * * @access protected * @since 20120708 * @var string */ protected $table; /** * Number of iterations to process hash with * * @access protected * @since 20120708 * @var int */ protected $rounds; /** * Class Constructor * * Initializes the class for first use! * * @access public * @since 20120708 * @return void */ public function __construct() { // get CodeIgniter instance $this->ci =& get_instance(); // retrieve settings $this->ci->config->load('mauth'); $this->table = $this->ci->config->item('mauth_table'); $this->rounds = $this->ci->config->item('mauth_rounds'); // verify secure settings for $this->rounds if($this->rounds < 4 || $this->rounds > 32) $this->rounds = 8; // load dependancies $this->ci->load->database(); // Install database if necessary if(! $this->ci->db->table_exists($this->table)) { $this->_install(); } } /** * Install database table * * @access protected * @since 20120708 * @return void */ protected function _install() { // load dbforge $this->ci->load->dbforge(); // prepare fields 'type' => 'INT', 'unsigned' => TRUE, 'auto_increment' => TRUE, ), 'type' => 'VARCHAR', 'constraint' => '50', ), 'type' => 'VARCHAR', 'constraint' => '255', ), 'type' => 'CHAR', 'constraint' => '60', ), ); $this->ci->dbforge->add_field($fields); // configure indexes $this->ci->dbforge->add_key('user_id', TRUE); $this->ci->dbforge->add_key('username'); $this->ci->dbforge->add_key('email'); // create table $this->ci->dbforge->create_table($this->table, TRUE); } /** * Generate password hash * * Generates a password hash using the bcrypt algorithm * * @access protected * @since 20120708 * @param string $password plaintext password to hash * @return string hashed password */ protected function _hash_pass($password) { // generate a salt $salt = ''; for($i = 0; $i < 22; $i++) { } // format salt // return hash } /** * Verify password hash * * Checks plaintext password against stored hash to see if it is valid * * @access protected * @since 20120708 * @param string $password plaintext password to verify * @param string $stored hash from database to check against * @return bool */ protected function _verify_pass($password, $stored) { return $check; } /** * Check if username is in use * * @access public * @since 20120708 * @param string $username username to check * @return bool */ public function check_user($username) { $this->ci->db->where('username', $username); $query = $this->ci->db->get($this->table); // check if the username is in use if($query->num_rows() > 0) { // username is in use return TRUE; } // username is not in use return FALSE; } /** * Check if email address is in use * * @access public * @since 20120708 * @param string $email email address to check * @return bool */ public function check_email($email) { $this->ci->db-where('email', $email); $query = $this->ci->db->get($this->table); // check if email is in use if($query->num_rows() > 0) { // email is in use return TRUE; } // email is not in use return FALSE; } /** * Create user * * @access public * @since 20120708 * @param string $username username to set * @param string $email email address to set * @param string $password password to set * @return bool */ public function create_user($username, $email, $password) { // check to make sure username is not taken if($this->check_username !== FALSE) { return FALSE; } // check to make sure email address is not taken if($this->check_email !== FALSE) { return FALSE; } // check to make sure email is valid if(! filter_var($email, FILTER_VALIDATE_EMAIL)) { return FALSE; } // prepare our password hash $hash = $this->_hash_pass($password); // prepare user information 'username' => $username, 'email' => $email, 'password' => $hash, ); // add user to the database $this->ci->db->insert($this->table, $data); return TRUE; } /** * Verify user * * This function is set up to verify a user with either a username or an * email address, and will check for which you are using automatically so * that you can set up your application to operate however you prefer. * * @access public * @param string $user username or email address to verify * @param string $password password for the user to verify * @return bool */ public function verify_user($user, $password) { $valid = FALSE; if(filter_var($user, FILTER_VALIDATE_EMAIL)) { $this->ci->db->where('email', $user); } else { $this->ci->db->where('username', $user); } $query = $this->ci->db->get($this->table); if($query->num_rows() > 0) { $row = $query->result(); $valid = $this->_verify_pass($password, $row->password); } return $valid; } /** * Change password * * This application is set up to change a password using the username * or email address, and will check for which you are using automatically * so that you can set up your application however you prefer. * * @access public * @since 20120708 * @param string $user user to update password for * @param string $password new password to set * @return void */ public function change_password($user, $password) { // prepare hash $hash = $this->_hash_pass($password); // prepare where statement if(filter_var($user, FILTER_VALIDATE_EMAIL)) { $this->ci->db->where('email', $user); } else { $this->ci->db->where('username', $user); } // update password $this->ci->db->update($this->table, $hash); } /** * Retrieve user info * * This function will retrieve user info using either an username or * email address. It will check which you are using automatically so * that you can set up your application to use whatever method you prefer. * This function will also strip password information from the array, so * that you don't have to worry about it accidentally getting out. * * @access public * @param string $user username or email address to retrieve info for * @return array */ public function user_info($user) { // retrieve data if(filter_var($user, FILTER_VALIDATE_EMAIL)) { $this->ci->db->where('email', $user); } else { $this->ci->db->where('username', $user); } $query = $this->ci->db->get($this->table); $row = $query->result(); // prepare data 'user_id' => $row->user_id, 'username' => $row->username, 'email' => $row->email, ); // return data for further processing return $data; } /** * Delete user * * This function will delete a user using either the username or the * email address. It will check which you are using automatically, so that * you can set up your application to use whichever that you prefer. * * @access public * @param string $user username or email address of the user to delete. * @return void */ public function delete_user($user) { if(filter_var($user, FILTER_VALIDATE_EMAIL)) { $this->ci->db->where('email', $user); } else { $this->ci->db->where('username', $user); } $this->ci->db->delete($this->table); } }
You need to login to post a comment.
