public function before()
{
parent::before();
// Set common variables
$this->a2 = A2::instance('auth');
$this->a1 = $this->a2->a1;
$this->session = Session::instance();
// Check if internal request
if ($this->request !== Request::instance() or Request::$is_ajax) {
$this->_internal = TRUE;
}
// Check if internal-only request
if (in_array($this->request->action, $this->_internal_only) and !$this->_internal) {
Kohana::$log->add(Kohana::INFO, 'Attempt to access internal URL, ' . $this->request->uri . ', externally');
Request::instance()->redirect(Route::get('admin')->uri());
}
// Perform resource loads and ACL check
try {
if (in_array($this->request->action, $this->_resource_required)) {
$this->_load_resource();
}
if ($this->_acl_required === 'all' or in_array($this->request->action, $this->_acl_required)) {
$privilege = isset($this->_acl_map[$this->request->action]) ? $this->_acl_map[$this->request->action] : $this->_acl_map['default'];
$this->a2->allowed($this->_resource, $privilege, TRUE);
}
} catch (A2_Exception $ae) {
// Redirect to login form if not logged in
if (!($user = $this->a2->get_user())) {
$this->session->set('referrer', Request::instance()->uri);
Message::instance()->error(Kohana::message('a2', 'login.required'));
$this->request->redirect(Route::get('admin/auth')->uri());
}
Kohana::$log->add('ACCESS', 'Failed attempt to access resource, ' . $this->_resource . ', by user, ' . $user->username . ', with url, ' . $this->request->uri);
Message::instance()->error($ae->getMessage(), array(':resource' => $this->_resource));
// If internal request, redirect to denied action
if ($this->_internal) {
$this->request->action = 'denied';
} else {
// If controller-level access is denied, redirect to admin main
if ($this->request->action == 'index') {
$this->request->redirect(Route::get('admin')->uri());
} else {
$this->request->redirect($this->request->uri(array('action' => 'index', 'id' => NULL)));
}
}
} catch (Kohana_Exception $ke) {
// Catch 404 exceptions triggered by invalid resource loads
if ($ke->getCode() == 404) {
Message::instance()->error($ke->getMessage());
$this->request->redirect($this->request->uri(array('action' => '', 'id' => NULL)));
} else {
throw $ke;
}
}
}