Chciał bym abyście ocenili i skomentowali mój model autoryzacji. Jestem właśnie na etapie jego pisania więc chciał bym się dowiedzieć co zmodyfikować, dodać i usunąć.
<?php defined('SYSPATH') or
die('No direct script access.');
class User_Model extends Model
{
protected $prefix;
protected $session;
protected $input;
public function __construct()
{
parent::__construct();
$this->session = new Session;
$this->input = new Input;
$this->prefix = config::item('database.default.table_prefix');
}
public function makeAutoLogIn()
{
$cookie_data = cookie::get('Authentication');
$login = NULL;
$cookie_key = NULL;
if($cookie_data !== NULL and $this->session->get('isLogin', FALSE) === FALSE)
{
$data = explode('-', $cookie_data); $cookie_key = $this->session->id();
$cookie_name = 'Authentication';
$cookie_value = $data[0].'-'.$cookie_key;
$cookie_expire = 60*60*24*30;
cookie::delete($cookie_name);
cookie::set($cookie_name, $cookie_value, $cookie_expire);
$this->db->select('users.user_id, users.user_email, users.user_name, users.user_logins_count, users
.user_last_login, users_roles.role_id');
$this->db->from('users');
$this->db->join('users_roles', 'users_roles.user_id = users.user_id');
$this->db->where(array('users.user_id' => $data[0], 'users.user_cookie_key' => $data[1
])); $query = $this->db->get()->result_array();
foreach($query as $row)
{
$this->session->set('isLogin', TRUE);
$this->session->set('id', $row->user_id);
$this->session->set('login', $row->user_name);
$this->session->set('email', $row->user_email);
$this->session->set('lastvisit', $row->user_last_login);
$this->session->set('role', $row->role_id);
$this->db->from('users');
$this->db->set(array('user_logins_count' => $row->user_logins_count+1, 'user_cookie_key' => $cookie_key, 'user_last_ip' => $this->input->ip_address(), 'user_last_login' => mktime())); $this->db->where(array('user_id' => $data[0], 'user_cookie_key' => $data[1
])); $this->db->update();
}
return (bool) TRUE;
}
else
{
return (bool) FALSE;
}
}
public function makeLogIn($login = NULL, $password = NULL, $auto = FALSE)
{
$cookie_key = $this->session->id();
cookie::delete('Authentication');
$check = $this->db->select('user_id, user_salt_begin, user_salt_end')->from('users')->where('users.user_name', $login)->get()->current();
if($auto === TRUE)
{
$cookie_name = 'Authentication';
$cookie_value = $check->user_id.'-'.$cookie_key;
$cookie_expire = 60*60*24*30;
cookie::set($cookie_name, $cookie_value, $cookie_expire);
}
if($login !== NULL and $password !== NULL and $this->session->get('isLogin', FALSE) === FALSE)
{
$this->db->select('users.user_id, users.user_email, users.user_name, users.user_logins_count, users
.user_last_login, users_roles.role_id');
$this->db->from('users');
$this->db->join('users_roles', 'users_roles.user_id = users.user_id');
$this->db->where(array('users.user_name' => $login, 'users.user_password' => sha1
($check->user_salt_begin.md5($password).$check->user_salt_end))); $query = $this->db->get()->result_array();
foreach($query as $row)
{
$this->session->set('isLogin', TRUE);
$this->session->set('id', $row->user_id);
$this->session->set('login', $row->user_name);
$this->session->set('email', $row->user_email);
$this->session->set('lastvisit', $row->user_last_login);
$this->session->set('role', $row->role_id);
$this->db->from('users');
$this->db->set(array('user_logins_count' => $row->user_logins_count+1, 'user_cookie_key' => $cookie_key, 'user_last_ip' => $this->input->ip_address(), 'user_last_login' => mktime())); $this->db->where(array('users.user_name' => $login, 'users.user_password' => sha1
($check->user_salt_begin.md5($password).$check->user_salt_end))); $this->db->update();
}
return (bool) TRUE;
}
else
{
return (bool) FALSE;
}
}
public function makeLogOut()
{
$this->session->destroy();
cookie::delete('Authentication');
}
public function isAnonymous()
{
if($this->session->get('isLogin', FALSE) === FALSE)
{
return (bool) TRUE;
}
else
{
return (bool) FALSE;
}
}
public function getData()
{
'isLogin' => (bool) $this->session->get('isLogin', FALSE),
'id' => (int) $this->session->get('id', 0),
'login' => (string) $this->session->get('login', NULL),
'email' => (string) $this->session->get('email', NULL),
'lastvisit' => (string
) date('d.m.Y, H:i:s', $this->session->get('lastvisit', mktime())), 'role' => (int) $this->session->get('role', 1),
'IP' => (string) $this->input->ip_address(),
'Browser' => (string) Kohana::user_agent()
);
}
public function getId()
{
return (int) $this->session->get('id', 0);
}
public function getEmail()
{
return (string) $this->session->get('email', NULL);
}
public function getLogin()
{
return (string) $this->session->get('login', NULL);
}
public function getLastvisit()
{
return (int
) $this->session->get('lastvisit', mktime()); }
public function getRole()
{
return (int) $this->session->get('role', 1);
}
public function getIp()
{
return (string) $this->input->ip_address();
}
public function getBrowser()
{
return (string) Kohana::user_agent();
}
} // End User Model
?>