Forked from smgladkovskiy/Controller_Ajax_Template.php
Created
August 4, 2011 08:46
-
-
Save nexeck/1124749 to your computer and use it in GitHub Desktop.
Ajax Template Controller for Kohana 3.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct access allowed.'); | |
/** | |
* Ajax Template Controller template | |
* | |
* @author Sergei Gladkovskiy <smgladkovskiy@gmail.com> | |
*/ | |
abstract class Controller_Ajax_Template extends Controller { | |
protected $_auth_required = FALSE; | |
protected $_user; | |
public function before() | |
{ | |
if($this->request->method() === HTTP_Request::POST and $this->request->post('is_ajax') === 'true') | |
$this->request->requested_with('xmlhttprequest'); | |
parent::before(); | |
// Ajax Request check | |
if( ! $this->request->is_ajax() AND Kohana::$environment == Kohana::PRODUCTION) | |
{ | |
throw new HTTP_Exception_403('non-ajax request!'); | |
} | |
// Auth requirement | |
if ($this->_auth_required AND ! Auth::instance()->logged_in()) | |
{ | |
throw new HTTP_Exception_401('Auth required!'); | |
} | |
elseif(($this->_auth_required AND Auth::instance()->logged_in()) OR Auth::instance()->logged_in()) | |
{ | |
$this->_user = Auth::instance()->get_user(); | |
$this->_is_admin = $this->_user->has_role('admin'); | |
} | |
$this->response->headers('Content-Type', 'application/json; charset=utf-8'); | |
} | |
} // End Controller_Ajax_Template |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment