Mine
public text v1 · immutable<?php
class Eauth_Core {
$db = NULL;
function __construct()
{
$this->db = new Database();
}
function try_login($condition = array())
{
$this->db->select('*');
$this->db->from('awp_users');
$this->db->join('awp_roles', 'awp_roles.id = awp_users.role_id', 'LEFT');
$this->db->where($condition);
$this->db->where('validated', 1);
$query = $this->db->get();
if ($query->num_rows !== 1)
{
return FALSE;
}
else
{
$row = $query->row();
$userdata = array(
'user_id' => $row->id,
'authenticated' => TRUE
);
$this->session->set($userdata);
return TRUE;
}
}
function try_session_login()
{
if ($this->session->get('user_id'))
{
$query = $this->db->query('SELECT COUNT(*) AS total FROM awp_users WHERE id = ' . $this->session->get('user_id'));
$row = $query->row();
if ($row->total != 1)
{
// Bad session - kill it
$this->logout();
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return FALSE;
}
}
function logout()
{
$this->session->destroy();
}
function get_field($field = '')
{
$this->db->select($field);
$query = $this->db->getwhere('awp_users', array('id'=>$this->session->get('user_id')), 1, 0);
if ($query->num_rows() == 1)
{
$row = $query->row();
return $row->$field;
}
}
function get_role($role)
{
$this->db->select('awp_roles.name');
$this->db->JOIN('awp_roles', 'awp_users.role_id = awp_roles.id');
$query = $this->db->getwhere('awp_users', array('awp_users.id'=>$this->session->get('user_id')), 1, 0);
if($query->num_rows() === 1)
{
$row = $query->row();
if($row->name === $role)
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
}
?>