php_coder
Новичок
После авторизации через Auth (ORM) любой переход по ссылке внутри сайта порождает запрос к БД.
(Код, выводимый профайлером Коханы):
SELECT `user`.`id` AS `id`, `user`.`email` AS `email`, `user`.`username` AS `username`, `user`.`password` AS `password`, `user`.`logins` AS `logins`, `user`.`last_login` AS `last_login` FROM `users` AS `user` WHERE `user`.`id` = '2' LIMIT 1 (1).
Собсно вопросы:
Зачем он нужен?
Какая полседовательность вызовов методов его порождает?
Как его отключить?
(Код, выводимый профайлером Коханы):
SELECT `user`.`id` AS `id`, `user`.`email` AS `email`, `user`.`username` AS `username`, `user`.`password` AS `password`, `user`.`logins` AS `logins`, `user`.`last_login` AS `last_login` FROM `users` AS `user` WHERE `user`.`id` = '2' LIMIT 1 (1).
Собсно вопросы:
Зачем он нужен?
Какая полседовательность вызовов методов его порождает?
Как его отключить?
Код:
<?php defined('SYSPATH') or die('No direct script access.');
//Пользовательский контроллер подготовительных действий.
abstract class Controller_Prepare extends Controller_Template {
public $template = 'Base';//основной (главный) шаблон по умолчанию
protected $session;
protected $auth;//Экземпляр объекта Auth.
protected $user;//Текущий пользователь.
public function before()
{
parent::before();
View::set_global('bodyClass', 'index');
View::set_global('title', 'Cool site');
View::set_global('description', 'Cool site');
View::set_global('navActive', array('', '', '', '', ''));
$this->auth = Auth::instance();//Получаем экземпляр объекта Auth.
//Получаем текущего пользователя из сессии (если он залогинился).
$this->user = $this->auth->get_user();
//$this->session = Session::instance();
//echo $this->user->get('username');
echo View::factory('profiler/stats');
$username = '';
if($this->user)
$username = $this->user->get('username');
View::set_global('username', $username);
//View::set_global('username', '');
$this->template->content = '';
$this->template->styles = ['public/css/style'];
$this->template->scripts = [];
}
}
Код:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Controller_Auth extends Controller_Common
{
//Создание пользователя.
private function userCreate()
{
$authTpl = View::factory('auth/create')
->bind('errors', $errors)
->bind('message', $message);
if (HTTP_Request::POST == $this->request->method())
{
try
{
// Создать юзера, используя данные формы.
$user = ORM::factory('user')->create_user($this->request->post(), array(
'username',
'password',
'email'
));
// Предоставить юзеру логин роль.
$user->add('roles', ORM::factory('role', array('name' => 'login')));
// Сбросить значения формы.
$_POST = array();
// Установка "успешного" сообщения.
$message = "Пользователь '{$user->username}' добавлен в БД";
}
catch (ORM_Validation_Exception $e)
{
// Установка "неудачного" сообщения.
$message = 'Имеются поля с некорректными данными.';
// Установка сообщений об ошибках.
$errors = $e->errors('models');
}
}
return $authTpl;
}
//Вход пользователя.
private function userLogin()
{
$authTpl = View::factory('auth/login')
->bind('errors', $errors)
->bind('message', $message);
if (HTTP_Request::POST == $this->request->method())
{
// "Запомнить меня".
$remember = array_key_exists('remember', $this->request->post()) ?
(bool) $this->request->post('remember') : FALSE;
$user = $this->auth->login($this->request->post('username'),
$this->request->post('password'), $remember);
// Редирект юзера в случае успеха логина.
if ($this)
{
$this->user = $this->auth->get_user();
View::set_global('username', $this->user->get('username'));
// Сбросить значения формы.
$_POST = array();
$message = 'С возвращением, '.$this->user->get('username').'!';
}
else $message = 'Неверное имя или пароль';
}
return $authTpl;
}
//Выход пользователя.
private function userLogout()
{
// Разлогиниваем юзера.
$this->auth->logout();
// Редирект на ту же страницу.
View::set_global('username', null);
HTTP::redirect($_SERVER['HTTP_REFERER']);
}
//Общий экшн авторизации/регистрации.
private function action_index($arg = '')
{
View::set_global('bodyClass', 'movies');
$errors = array();
//Если юзер уже вошёл или "запомнен" в куках.
if ($this->user)
{
if($arg == 'login')
$this->colL_content .= '<h3>Вы уже вошли на сайт.</h3>';
else if($arg == 'reg')
$this->colL_content .= '<h3>Вы уже зарегистрированы.</h3>';
else if($arg == 'logout')
$this->colL_content .= $this->userLogout();
}
else
{
if($arg == 'login')
$this->colL_content .= $this->userLogin();
else if($arg == 'reg')
$this->colL_content .= $this->userCreate();
}
$this->action_pageBuild();
$this->template->scripts[] = 'public/js/jquery-1.10.1.min';
$this->template->scripts[] = 'public/js/select';
$this->template->content = $this->content;
}
//Экшн авторизации.
public function action_login(){ $this->action_index('login'); }
//Экшн регистрации.
public function action_registration(){ $this->action_index('reg'); }
//Экшн логаута.
public function action_logout(){ $this->action_index('logout'); }
}