Merge from lnApp

This commit is contained in:
Deon George
2011-05-09 17:40:50 +10:00
parent 8814447096
commit 8b658b497a
46 changed files with 466 additions and 200 deletions

View File

@@ -0,0 +1,213 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides login capability
*
* @package lnApp
* @subpackage Page/Login
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
* @also [logout]
*/
class Controller_lnApp_Login extends Controller_TemplateDefault {
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');
}
// 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('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) {
// Redirect to the user account
if ($redir = Session::instance()->get('afterlogin')) {
Session::instance()->delete('afterlogin');
Request::instance()->redirect($redir);
} else
Request::instance()->redirect('welcome/index');
} else {
SystemMessage::add(array(
'title'=>_('Invalid username or password'),
'type'=>'error',
'body'=>_('The username or password was invalid.')
));
}
}
Block::add(array(
'title'=>_('Login to server'),
'body'=>View::factory('login'),
'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;});
});'
));
}
public function action_register() {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
Request::instance()->redirect('welcome/index');
}
// Instantiate a new user
$account = ORM::factory('account');
// If there is a post and $_POST is not empty
if ($_POST) {
// Check Auth
$status = $account->values($_POST)->check();
if (! $status) {
foreach ($account->validate()->errors() as $f=>$r) {
// $r[0] has our reason for validation failure
switch ($r[0]) {
// Generic validation reason
default:
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])
));
}
}
}
$ido = ORM::factory('module')
->where('name','=','account')
->find();
$account->id = $ido->record_id->next_id($ido->id);
// Save the user details
if ($account->save()) {}
}
SystemMessage::add(array(
'title'=>_('Already have an account?'),
'type'=>'info',
'body'=>_('If you already have an account, please login..')
));
Block::add(array(
'title'=>_('Register'),
'body'=>View::factory('bregister')
->set('account',$account)
->set('errors',$account->validate()->errors()),
'style'=>array('css/bregister.css'=>'screen'),
));
$this->template->control = HTML::anchor($this->request->uri(),'Register',array('id'=>'ajxbody'));
$this->template->content = Block::factory();
$this->template->left = HTML::anchor('login','Login').'...';
}
/**
* Enable user password reset
*/
public function action_reset() {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
Request::instance()->redirect('welcome/index');
}
// If the user posted their details to reset their password
if ($_POST) {
// If the email address is correct, create a method token
if (! empty($_POST['email']) AND ($ao=ORM::factory('account',array('email'=>$_POST['email']))) AND $ao->loaded()) {
$mt = ORM::factory('module_method_token');
// Find out our password reset method id
// @todo move this to a more generic method, so that it can be called by other methods
$mo = ORM::factory('module',array('name'=>'account'));
$mmo = ORM::factory('module_method',array('name'=>'user_resetpassword','module_id'=>$mo->id));
// Check to see if there is already a token, if so, do nothing.
if ($mt->where('account_id','=',$ao->id)->and_where('method_id','=',$mmo->id)->find()) {
if ($mt->date_expire < time()) {
$mt->delete();
$mt->clear();
}
}
if (! $mt->loaded()) {
$mt->account_id = $ao->id;
$mt->method_id = $mmo->id;
$mt->date_expire = time() + 15*3600;
$mt->token = md5(sprintf('%s:%s:%s',$mt->account_id,$mt->method_id,$mt->date_expire));
$mt->save();
// Send our email with the token
$et = EmailTemplate::instance('account_reset_password');
$et->to = array($mt->account->email=>sprintf('%s %s',$mt->account->first_name,$mt->account->last_name));
$et->variables = array(
'SITE'=>URL::base(TRUE,TRUE),
'SITE_ADMIN'=>Config::sitename(),
'SITE_NAME'=>Config::sitename(),
'TOKEN'=>$mt->token,
'USER_NAME'=>sprintf('%s %s',$mt->account->first_name,$mt->account->last_name),
);
$et->send();
}
// 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']));
}
// Show our token screen even if the email was invalid.
if (isset($_POST['email']))
Block::add(array(
'title'=>_('Reset your password'),
'body'=>View::factory('login_reset_sent'),
'style'=>array('css/login.css'=>'screen'),
));
else
Request::instance()->redirect('login');
} else {
Block::add(array(
'title'=>_('Reset your password'),
'body'=>View::factory('login_reset'),
'style'=>array('css/login.css'=>'screen'),
));
}
$this->template->content = Block::factory();
}
public function action_noaccess() {
$this->template->content = '&nbsp;';
SystemMessage::add(array(
'title'=>_('No access to requested resource'),
'type'=>'error',
'body'=>_('You do not have access to the requested resource, please contact your administrator.')
));
}
}
?>

View File

@@ -61,9 +61,9 @@ abstract class Controller_lnApp_TemplateDefault extends Controller_Template {
if (! Kohana::Config('config.method_security'))
return FALSE;
return (($this->auth_required !== FALSE && Auth::instance()->logged_in() === 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]) === FALSE));
Auth::instance()->logged_in($this->secure_actions[$this->request->action],get_class($this).'|'.__METHOD__) === FALSE));
}
/**

View File

@@ -1,138 +1,4 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides login capability
*
* @package lnApp
* @subpackage Page/Login
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
* @also [logout]
*/
class Controller_Login extends Controller_TemplateDefault {
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');
}
// If there is a post and $_POST is not empty
if ($_POST) {
// Store our details in a session key
Session::instance()->set('admin_name',$_POST['admin_name']);
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) {
// Redirect to the user account
if ($redir = Session::instance()->get('afterlogin')) {
Session::instance()->delete('afterlogin');
Request::instance()->redirect($redir);
} else
Request::instance()->redirect('welcome/index');
} else {
SystemMessage::add(array(
'title'=>_('Invalid username or password'),
'type'=>'error',
'body'=>_('The username or password was invalid.')
));
}
}
Block::add(array(
'title'=>_('Login to server'),
'body'=>View::factory('login'),
'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;});
});'
));
}
public function action_register() {
// If user already signed-in
if (Auth::instance()->logged_in()!= 0) {
// Redirect to the user account
Request::instance()->redirect('welcome/index');
}
// Instantiate a new user
$account = ORM::factory('account');
// If there is a post and $_POST is not empty
if ($_POST) {
// Check Auth
$status = $account->values($_POST)->check();
if (! $status) {
foreach ($account->validate()->errors() as $f=>$r) {
// $r[0] has our reason for validation failure
switch ($r[0]) {
// Generic validation reason
default:
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])
));
}
}
}
$ido = ORM::factory('module')
->where('name','=','account')
->find();
$account->id = $ido->record_id->next_id($ido->id);
// Save the user details
if ($account->save()) {}
}
SystemMessage::add(array(
'title'=>_('Already have an account?'),
'type'=>'info',
'body'=>_('If you already have an account, please login..')
));
Block::add(array(
'title'=>_('Register'),
'body'=>View::factory('bregister')
->set('account',$account)
->set('errors',$account->validate()->errors()),
'style'=>array('css/bregister.css'=>'screen'),
));
$this->template->control = HTML::anchor($this->request->uri(),'Register',array('id'=>'ajxbody'));
$this->template->content = Block::factory();
$this->template->left = HTML::anchor('login','Login').'...';
}
public function action_noaccess() {
$this->template->content = '&nbsp;';
SystemMessage::add(array(
'title'=>_('No access to requested resource'),
'type'=>'error',
'body'=>_('You do not have access to the requested resource, please contact your administrator.')
));
}
}
class Controller_Login extends Controller_lnApp_Login {}
?>

View File

@@ -55,7 +55,7 @@ class Database_TSM extends Database {
// Get user login details from user session - these are set by login
if (! $username)
$username = Session::instance()->get_once('admin_name');
$username = Session::instance()->get_once('username');
if (! $password)
$password = Session::instance()->get_once('password');

View File

@@ -12,39 +12,6 @@
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Config extends Kohana {
/**
* Find a list of all database enabled modules
*
* @uses cache
*/
public static function appmodules() {
$cacheable = TRUE;
if (array_key_exists('cache',Kohana::modules())) {
$cache = Cache::instance(static::cachetype());
if ($cacheable AND $cache->get('modules'))
return $cache->get('modules');
} else
$cache = '';
$modules = array();
$module_table = 'module';
if (class_exists('Model_'.ucfirst($module_table))) {
$mo = ORM::factory($module_table)->where('status','=',1)->find_all()->as_array();
foreach ($mo as $o)
$modules[$o->name] = MODPATH.$o->name;
}
if ($cache)
$cache->set('modules',$modules);
return $modules;
}
/**
* Return our site name
*/
@@ -90,7 +57,13 @@ abstract class lnApp_Config extends Kohana {
return Kohana::config('config.site_name');
}
public static function logo_file() {
// @todo Move the logo filename to a config file
return Kohana::find_file(sprintf('media/%s',Config::siteid()),'img/logo-small','png');
}
public static function logo() {
// @todo Move the logo filename to a config file
$mediapath = Route::get('default/media');
$logo = $mediapath->uri(array('file'=>'img/logo-small.png'),array('alt'=>static::sitename()));

View File

@@ -51,13 +51,13 @@ class Model_Auth_UserDefault extends Model_Auth_User {
* @return boolean
*/
public function login(array & $array, $redirect = FALSE) {
$fieldname = 'admin_name';
$fieldname = 'admin_name'; // @todo This should be defined in a config or database driver file
$array = Validate::factory($array)
->label('admin_name', $this->_labels[$fieldname])
->label('username', $this->_labels[$fieldname])
->label('password', $this->_labels['password'])
->filter(TRUE, 'trim')
->filter('admin_name','strtoupper')
->rules('admin_name', $this->_rules[$fieldname])
->filter('username','strtoupper')
->rules('username', $this->_rules[$fieldname])
->rules('password', $this->_rules['password']);
// Get the remember login option
@@ -69,7 +69,7 @@ class Model_Auth_UserDefault extends Model_Auth_User {
if ($array->check())
{
// Attempt to load the user
$this->where($fieldname, '=', $array['admin_name'])->find();
$this->where($fieldname, '=', $array['username'])->find();
if ($this->loaded() AND Auth::instance()->login($this, $array['password'], $remember))
{

View File

@@ -13,7 +13,7 @@
class TSM {
public function set($username,$password,Database_TSM_Result $server) {
// @todo Consider encrypting this
Session::instance()->set('admin_name',$username);
Session::instance()->set('username',$username);
Session::instance()->set('password',$password);
Session::instance()->set('SERVER',$server);