Updated to work with KH 3.1

This commit is contained in:
Deon George
2011-05-23 20:01:27 +10:00
parent 4765770a1a
commit a244d693b5
28 changed files with 1027 additions and 553 deletions

View File

@@ -11,6 +11,14 @@
* @license http://dev.leenooks.net/license.html
*/
abstract class Controller_lnApp_Default extends Controller {
/**
* The variable that our output is stored in
*/
protected $output = NULL;
/**
* @var string page media route as per [Route]
*/
protected $mediaroute = 'default/media';
/**
* Controls access to this controller.
* Can be set to a string or an array, for example 'login' or array('login', 'admin')
@@ -48,8 +56,8 @@ abstract class Controller_lnApp_Default extends Controller {
return FALSE;
return (($this->auth_required !== FALSE && Auth::instance()->logged_in() === FALSE) ||
(is_array($this->secure_actions) && array_key_exists($this->request->action,$this->secure_actions) &&
Auth::instance()->logged_in($this->secure_actions[$this->request->action]) === FALSE));
(is_array($this->secure_actions) && array_key_exists($this->request->action(),$this->secure_actions) &&
Auth::instance()->logged_in($this->secure_actions[$this->request->action()]) === FALSE));
}
public function before() {
@@ -58,16 +66,16 @@ abstract class Controller_lnApp_Default extends Controller {
// Check user auth and role
if ($this->_auth_required()) {
// For AJAX/JSON requests, authorisation is controlled in the method.
if (Request::$is_ajax && $this->request->action === 'json') {
if (Request::current()->is_ajax() && $this->request->action() === 'json') {
// Nothing required.
// For no AJAX/JSON requests, display an access page
} elseif (Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__)) {
Request::instance()->redirect('login/noaccess');
Request::current()->redirect('login/noaccess');
} else {
Session::instance()->set('afterlogin',Request::instance()->uri());
Request::instance()->redirect($this->noauth_redirect);
Session::instance()->set('afterlogin',Request::detect_uri());
Request::current()->redirect($this->noauth_redirect);
}
}
}

View File

@@ -12,34 +12,30 @@
* @also [logout]
*/
class Controller_lnApp_Login extends Controller_TemplateDefault {
protected $auth_required = FALSE;
public function action_index() {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
Request::instance()->redirect('welcome/index');
Request::current()->redirect('welcome/index');
}
// If there is a post and $_POST is not empty
if ($_POST) {
// Store our details in a session key
Session::instance()->set('username',$_POST['username']);
Session::instance()->set(Kohana::config('auth.session_key'),$_POST['username']);
Session::instance()->set('password',$_POST['password']);
// Instantiate a new user
$user = ORM::factory('account');
// Check Auth
$status = $user->login($_POST);
// If the post data validates using the rules setup in the user model
if ($status) {
if (Auth::instance()->login($_POST['username'],$_POST['password'])) {
// Redirect to the user account
if ($redir = Session::instance()->get('afterlogin')) {
Session::instance()->delete('afterlogin');
Request::instance()->redirect($redir);
Request::current()->redirect($redir);
} else
Request::instance()->redirect('welcome/index');
Request::current()->redirect('welcome/index');
} else {
SystemMessage::add(array(
@@ -56,9 +52,6 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
'style'=>array('css/login.css'=>'screen'),
));
$this->template->control = HTML::anchor($this->request->uri(),'Login',array('id'=>'ajxbody'));
$this->template->content = Block::factory();
Script::add(array('type'=>'stdin','data'=>'
$(document).ready(function() {
$("#ajxbody").click(function() {$("#ajBODY").load("'.$this->request->uri().'/"); return false;});
@@ -70,7 +63,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
Request::instance()->redirect('welcome/index');
Request::current()->redirect('welcome/index');
}
// Instantiate a new user
@@ -82,7 +75,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
$status = $account->values($_POST)->check();
if (! $status) {
foreach ($account->validate()->errors() as $f=>$r) {
foreach ($account->validation()->errors('form/register') as $f => $r) {
// $r[0] has our reason for validation failure
switch ($r[0]) {
// Generic validation reason
@@ -90,7 +83,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
SystemMessage::add(array(
'title'=>_('Validation failed'),
'type'=>'error',
'body'=>sprintf(_('The defaults on your submission were not valid for field %s (%s).'),$f,$r[0])
'body'=>sprintf(_('The defaults on your submission were not valid for field %s (%s).'),$f,$r)
));
}
}
@@ -116,12 +109,9 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
'title'=>_('Register'),
'body'=>View::factory('bregister')
->set('account',$account)
->set('errors',$account->validate()->errors()),
'style'=>array('css/bregister.css'=>'screen'),
->set('errors',$account->validation()->errors('form/register')),
));
$this->template->control = HTML::anchor($this->request->uri(),'Register',array('id'=>'ajxbody'));
$this->template->content = Block::factory();
$this->template->left = HTML::anchor('login','Login').'...';
}
@@ -132,7 +122,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
Request::instance()->redirect('welcome/index');
Request::current()->redirect('welcome/index');
}
// If the user posted their details to reset their password
@@ -176,7 +166,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
// Redirect to our password reset, the Auth will validate the token.
} elseif (! empty($_REQUEST['token'])) {
Request::instance()->redirect(sprintf('user/account/resetpassword?token=%s',$_REQUEST['token']));
Request::current()->redirect(sprintf('user/account/resetpassword?token=%s',$_REQUEST['token']));
}
// Show our token screen even if the email was invalid.
@@ -187,7 +177,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
'style'=>array('css/login.css'=>'screen'),
));
else
Request::instance()->redirect('login');
Request::current()->redirect('login');
} else {
Block::add(array(
@@ -196,13 +186,9 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
'style'=>array('css/login.css'=>'screen'),
));
}
$this->template->content = Block::factory();
}
public function action_noaccess() {
$this->template->content = ' ';
SystemMessage::add(array(
'title'=>_('No access to requested resource'),
'type'=>'error',

View File

@@ -17,10 +17,10 @@ class Controller_lnApp_Logout extends Controller {
if (Auth::instance()->logged_in()!= 0) {
Auth::instance()->logout();
Request::instance()->redirect('login');
Request::current()->redirect('login');
}
Request::instance()->redirect('welcome/index');
Request::current()->redirect('welcome/index');
}
}
?>

View File

@@ -15,10 +15,6 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
* @var string page template
*/
public $template = 'lnapp/default';
/**
* @var string page media route as per [Route]
*/
protected $mediaroute = 'default/media';
/**
* @var object meta object information as per [meta]
*/
@@ -62,8 +58,8 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
return FALSE;
return (($this->auth_required !== FALSE && Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__) === FALSE) ||
(is_array($this->secure_actions) && array_key_exists($this->request->action,$this->secure_actions) &&
Auth::instance()->logged_in($this->secure_actions[$this->request->action],get_class($this).'|'.__METHOD__) === FALSE));
(is_array($this->secure_actions) && array_key_exists($this->request->action(),$this->secure_actions) &&
Auth::instance()->logged_in($this->secure_actions[$this->request->action()],get_class($this).'|'.__METHOD__) === FALSE));
}
/**
@@ -74,7 +70,7 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
*/
public function before() {
// Do not template media files
if ($this->request->action === 'media') {
if ($this->request->action() === 'media') {
$this->auto_render = FALSE;
return;
}
@@ -84,7 +80,7 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
// Check user auth and role
if ($this->_auth_required()) {
if (Kohana::$is_cli)
throw new Kohana_Exception('Cant run :method, authentication not possible',array(':method'=>$this->request->action));
throw new Kohana_Exception('Cant run :method, authentication not possible',array(':method'=>$this->request->action()));
// If auth is required and the user is logged in, then they dont have access.
// (We have already checked authorisation.)
@@ -93,24 +89,24 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
SystemMessage::add(array(
'title'=>_('Insufficient Access'),
'type'=>'debug',
'body'=>Kohana::debug(array('required'=>$this->auth_required,'action'=>$this->request->action,'user'=>Auth::instance()->get_user()->username)),
'body'=>Kohana::debug(array('required'=>$this->auth_required,'action'=>$this->request->action(),'user'=>Auth::instance()->get_user()->username)),
));
// @todo Login No Access redirects are not handled in JS?
if (Request::$is_ajax) {
if ($this->request->is_ajax()) {
echo _('You dont have enough permissions.');
die();
} else
Request::instance()->redirect('login/noaccess');
Request::current()->redirect('login/noaccess');
} else {
Session::instance()->set('afterlogin',Request::instance()->uri());
Request::instance()->redirect($this->noauth_redirect);
Session::instance()->set('afterlogin',Request::detect_uri());
Request::current()->redirect($this->noauth_redirect);
}
}
// For AJAX calls, we dont need to render the complete page.
if (Request::$is_ajax) {
if ($this->request->is_ajax()) {
$this->auto_render = FALSE;
return;
}
@@ -148,6 +144,9 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
}
public function after() {
if (! is_string($this->template) AND empty($this->template->content))
$this->template->content = Block::factory();
if ($this->auto_render) {
// Application Title
$this->meta->title = 'Application Title';
@@ -181,18 +180,16 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
$this->template->footer = $this->_footer();
// For any ajax rendered actions, we'll need to capture the content and put it in the response
} elseif (Request::$is_ajax && isset($this->template->content) && ! $this->request->response) {
} elseif ($this->request->is_ajax() && isset($this->template->content) && ! $this->response->body()) {
// @todo move this formatting to a view?
if ($s = $this->_sysmsg() AND (string)$s) {
$this->request->response = sprintf('<table class="sysmsg"><tr><td>%s</td></tr></table>',$s);
} else
$this->request->response = '';
if ($s = $this->_sysmsg() AND (string)$s)
$this->response->body(sprintf('<table class="sysmsg"><tr><td>%s</td></tr></table>',$s));
# In case there any style sheets or scrpits for this render.
$this->request->response .= Style::factory();
$this->response->bodyadd(Style::factory());
# Get the response body
$this->request->response .= sprintf('<table class="content"><tr><td>%s</td></tr></table>',$this->template->content);
$this->response->bodyadd(sprintf('<table class="content"><tr><td>%s</td></tr></table>',$this->template->content));
}
parent::after();
@@ -244,7 +241,7 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
*/
final public function action_media() {
// Generate and check the ETag for this file
$this->request->check_cache(sha1($this->request->uri));
$this->response->check_cache(NULL,$this->request);
// Get the file path from the request
$file = $this->request->param('file');
@@ -258,22 +255,22 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
// First try and find media files for the site_id
if ($f = Kohana::find_file(sprintf('media/%s',Config::siteid()), $file, $ext)) {
// Send the file content as the response
$this->request->response = file_get_contents($f);
$this->response->body(file_get_contents($f));
// If not found try a default media file
} elseif ($f = Kohana::find_file('media', $file, $ext)) {
// Send the file content as the response
$this->request->response = file_get_contents($f);
$this->response->body(file_get_contents($f));
} else {
// Return a 404 status
$this->request->status = 404;
$this->response->status(404);
}
// Set the proper headers to allow caching
$this->request->headers['Content-Type'] = File::mime_by_ext($ext);
$this->request->headers['Content-Length'] = filesize($f);
$this->request->headers['Last-Modified'] = date('r', filemtime($f));
$this->response->headers('Content-Type',File::mime_by_ext($ext));
$this->response->headers('Content-Length',(string)filesize($f));
$this->response->headers('Last-Modified',date('r', filemtime($f)));
}
}
?>

View File

@@ -11,22 +11,20 @@
* @license http://dev.osbill.net/license.html
*/
class Controller_lnApp_Tree extends Controller_Default {
// Our tree data
protected $treedata;
/**
* @var string page media route as per [Route]
*/
protected static $mediaroute = 'default/media';
protected static $jsmediaroute = 'default/media';
public function after() {
parent::after();
$this->request->headers['Content-Type'] = 'application/json';
$this->request->response = sprintf('[%s]',json_encode($this->treedata));
$this->response->headers('Content-Type','application/json');
$this->response->body(sprintf('[%s]',json_encode($this->output)));
}
public static function js() {
$mediapath = Route::get(static::$mediaroute);
$mediapath = Route::get(static::$jsmediaroute);
return '
<div id="tree" class=""></div>
@@ -85,28 +83,18 @@ $(function () {
*
* @param id
*/
public function action_json($id=null) {
if (! Auth::instance()->logged_in()) {
$this->treedata = array('attr'=>array('id'=>'a_login'),
public function action_json($id=null,array $data=array()) {
if ($this->_auth_required() AND ! Auth::instance()->logged_in()) {
$this->output = array('attr'=>array('id'=>'a_login'),
'data'=>array('title'=>_('Please Login').'...','attr'=>array('id'=>'N_login','href'=>URL::site('/login'))));
return;
}
$this->treedata = array();
$data = array();
// @todo Our menu options
array_push($data,array(
'id'=>'node',
'name'=>'Node Info',
'state'=>'none',
'attr_id'=>'1',
'attr_href'=>URL::Site('/node'),
));
$this->output = array();
foreach ($data as $branch) {
array_push($this->treedata,array(
array_push($this->output,array(
'attr'=>array('id'=>sprintf('B_%s',$branch['id'])),
'state'=>$branch['state'],
'data'=>array('title'=>$branch['name']),

View File

@@ -1,4 +1,16 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
class Controller_TemplateDefault extends Controller_lnApp_TemplateDefault {}
/**
* This class provides the default template controller for rendering pages.
*
* @package PTA
* @subpackage Page/Template
* @category Controllers
* @author Deon George
* @copyright (c) 2010 phpTSMadmin Development Team
* @license http://phptsmadmin.sf.net/license.html
*/
class Controller_TemplateDefault extends Controller_lnApp_TemplateDefault {
protected $auth_required = TRUE;
}
?>

View File

@@ -1,4 +1,37 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
class Controller_Tree extends Controller_lnApp_Tree {}
/**
* This class extends renders OSB menu tree.
*
* @package lnApp
* @subpackage Tree
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Tree extends Controller_lnApp_Tree {
protected $auth_required = TRUE;
/**
* Draw the Tree Menu
*
* The incoming ID is either a Branch B_x or a Node N_x
* Where X is actually the module.
*
* @param id
*/
public function action_json($id=null,array $data=array()) {
// @todo Our menu options
array_push($data,array(
'id'=>'node',
'name'=>'Node Info',
'state'=>'none',
'attr_id'=>'1',
'attr_href'=>URL::Site('/node'),
));
return parent::action_json($id,$data);
}
}
?>

View File

@@ -1,16 +1,21 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* phpTSMadmin Main home page
*
* @package PTA
* @subpackage Page/Home
* @category Controllers
* @author Deon George
* @copyright (c) 2010 phpTSMadmin Development Team
* @license http://phptsmadmin.sf.net/license.html
*/
class Controller_Welcome extends Controller_TemplateDefault {
public function action_index()
{
public function action_index() {
Block::add(array(
'title'=>sprintf('%s: %s (%s)',_('Server'),TSM::name(),TSM::version()),
'body'=>'hello, world!',
));
$this->template->content = Block::factory();
}
} // End Welcome
}
?>