Skip to content

Instantly share code, notes, and snippets.

@weslleyaraujo
Last active December 19, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weslleyaraujo/5922557 to your computer and use it in GitHub Desktop.
Save weslleyaraujo/5922557 to your computer and use it in GitHub Desktop.
Login library for CI
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Login Class
* This class enables you to have a simple login route and permission levels
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Weslley Araujo (http://weslleydeveloper.com)
* @link http://pastebin.com/FJn3WhLg
*
* @param $login_table_name
* your users table name on yout database
*
* @param $login_user_field
* the name field on your table to compare with pass
*
* @param $login_pass_field
* the password field on your table to compare with username ( require to be a md5 hash )
*
* @param $login_session_name
* the name you want to save on the session
*
* You can configure those itens ate your config.php file or using initialize method
* $config['login_table_name'] = '';
* $config['login_user_field'] = '';
* $config['login_pass_field'] = '';
* $config['login_session_name'] = '';
*
*/
class Login
{
protected $ci;
private $login_table_name;
private $login_user_field;
private $login_pass_field;
private $login_session_name;
private $config;
public function __construct($config = array())
{
// get ci instance
$this->ci =& get_instance();
// initialize
$this->initialize($config);
// get user
$user = $this->checkUser();
}
/**
* Initialize preferences
*
* @param array
* @return void
*/
public function initialize($config = array())
{
$params = array(
'login_table_name',
'login_user_field',
'login_pass_field',
'login_session_name'
);
foreach ($params as $key => $value) {
if (array_key_exists($value, $config)) {
$this->$value = $config[$value];
}
else {
$this->$value = $this->ci->config->item($value);
}
}
}
/**
* Get user session
*
* @return boolean user identification if user is logged, false if not
*
* @access private
*/
public function checkUser()
{
return $this->ci->session->userdata($this->login_session_name);
}
/**
* Hash user password
*
* @param string $pass Password to hash
*
* @return string Password hashed;
* @access public
*/
public function hashPass($pass)
{
return md5($pass);
}
/**
* Set user data
*
* @param int|string userdata you want to save
*
* @return void
* @access public
*/
private function setUser($user)
{
$this->ci->session->set_userdata($this->login_session_name, $user);
}
/**
* Destroy user session
*
* @return void
* @access public
*/
public function destroy()
{
$this->ci->session->unset_userdata($this->login_session_name);
}
/**
* Authenticate usar in database
*
* @param string $user username to check
* @param string $pass password to check
*
* @return mixed
* array ( empty ) means false
* objcet user info on database ( password return null for safe)
* @access public
*/
public function authenticate($user = null, $pass = null)
{
$fields = array(
$this->login_user_field => $user != null ? $user : $this->ci->input->post($this->login_user_field),
$this->login_pass_field => $pass != null ? $this->hashPass($pass) : $this->hashPass($this->ci->input->post($this->login_pass_field))
);
$this->ci->db->where($fields);
$get = $this->ci->db->get($this->login_table_name);
$get = $get->row();
if (is_object($get)) {
// clear password
$get->{$this->login_pass_field} = null;
}
$this->setUser($get);
}
/**
* Requires user authentication
*
* @param string $redirect page you want to redirect in case of fail authentication
*
* @access public
*/
public function requireLogin($redirect = '')
{
if(!$this->checkUser())
redirect($redirect);
return true;
}
/**
* Not allow user get in page if is logged
*
* @param string $redirect page you want to redirect in case of user already logged
*
* @access public
*/
public function notAllowLogged($redirect = '')
{
if($this->checkUser())
redirect($redirect);
return true;
}
}
/* End of file login.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment