This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
lnapp/classes/lnApp/Controller/Login.php

259 lines
7.5 KiB
PHP
Raw Normal View History

2013-04-22 05:50:28 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides login capability
*
* @package lnApp
2013-10-08 23:24:11 +00:00
* @category Controllers
2013-04-22 05:50:28 +00:00
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
* @also [logout]
*/
class lnApp_Controller_Login extends Controller_TemplateDefault {
protected $auth_required = FALSE;
2014-09-29 04:47:51 +00:00
/**
* Activate an account so that it can login and use the site
*/
public function action_activate() {
if ($this->request->post()) {
$ao = ORM::factory('Account',array('id'=>$this->request->param('id'),'email'=>$this->request->post('email')));
if ($ao->loaded()) {
if ($ao->activated())
HTTP::redirect('login');
2014-10-03 14:35:26 +00:00
elseif (! $ao->active) {
SystemMessage::factory()
->title(_('Account NOT Activated'))
->type('warning')
->body(_('Your account cannot been activated, please contact us.'));
} elseif ($ao->activate_code() == $this->request->post('code')) {
2014-10-02 05:33:07 +00:00
$ao->verified = TRUE;
$ao->save();
2014-09-29 04:47:51 +00:00
SystemMessage::factory()
->title(_('Account Activated'))
->type('info')
->body(_('Your account has been activated.'));
}
2014-10-03 14:35:26 +00:00
HTTP::redirect('welcome');
2014-09-29 04:47:51 +00:00
}
} elseif (! $this->request->param('id'))
HTTP::redirect('login/activate_resend');
Block::factory()
->title('Activate account')
->title_icon('fa-wrench')
->type('form-horizontal')
2014-10-03 14:35:26 +00:00
->body(View::factory('login/activate')->set('o',Session::instance()->get_once('activate'))->set('email',$this->request->query('email')));
2014-09-29 04:47:51 +00:00
}
/**
* Send the account activation code to the email address, validating the email address
*/
public function action_activate_resend() {
if ($this->request->post('email')) {
$ao = ORM::factory('Account',array('email'=>$this->request->post('email')));
if ($ao->loaded()) {
if ($ao->activated())
HTTP::redirect('login');
else {
$co = Company::instance();
// Send our email with the token
$email = Email::factory('login_activate')
->set('SITE',URL::base(TRUE,TRUE))
->set('SITE_ADMIN',$co->admin()->name())
->set('CODE',$ao->activate_code())
->set('EMAIL',$ao->email)
->set('ID',$ao->id)
->set('USER_NAME',$ao->name());
$email->to = array('email'=>array($ao->email=>$ao->name()));
$email->from = array('email'=>array($co->admin()->email=>$co->admin()->name()));
$email->subject = 'Activation Code for '.$co->name();
$email->deliver();
// Log the password reset
$ao->log('Activation code sent');
2014-10-02 05:33:07 +00:00
2014-09-29 04:47:51 +00:00
Session::instance()->set('activate',$ao);
}
}
HTTP::redirect('login/activate/'.$ao->id);
} else {
Block::factory()
->title('Activate account')
->title_icon('fa-wrench')
->type('form-horizontal')
->body(View::factory('login/activate_resend'));
}
}
/**
* Login to the site
*/
2013-04-22 05:50:28 +00:00
public function action_index() {
2013-04-25 00:22:36 +00:00
$output = '';
2014-09-08 13:42:05 +00:00
if (! array_key_exists('auth',Kohana::modules()))
throw HTTP_Exception::factory(501,'Auth not enabled.');
2013-04-22 05:50:28 +00:00
// If user already signed-in
2013-04-25 00:22:36 +00:00
if (Auth::instance()->logged_in())
2013-04-22 05:50:28 +00:00
HTTP::redirect(URL::link('user','welcome/index'));
// If there is a post and $_POST is not empty
2014-09-29 04:47:51 +00:00
if ($this->request->post()) {
2013-04-22 05:50:28 +00:00
// If the post data validates using the rules setup in the user model
2014-09-29 04:47:51 +00:00
if (Auth::instance()->login($this->request->post('username'),$this->request->post('password'))) {
2013-04-22 05:50:28 +00:00
// Redirect to the user account
if ($redir = Session::instance()->get('afterlogin')) {
Session::instance()->delete('afterlogin');
HTTP::redirect($redir);
} else
HTTP::redirect(URL::link('user','welcome/index'));
} else {
2013-04-25 00:22:36 +00:00
SystemMessage::factory()
->title(_('Invalid username or password'))
2014-09-08 13:42:05 +00:00
->type('danger')
2013-04-25 00:22:36 +00:00
->body(_('The username or password was invalid.'));
2013-04-22 05:50:28 +00:00
}
}
if (array_key_exists('oauth',Kohana::modules()))
$oauthlogin = is_null($x=Session::instance()->get_once('login-no-oauth',NULL)) ? TRUE : ! $x;
else
$oauthlogin = FALSE;
2014-09-29 04:47:51 +00:00
$output .= View::factory('login')
2013-05-23 05:42:56 +00:00
->set('oauth',$oauthlogin);
2013-04-25 00:22:36 +00:00
2013-05-23 05:42:56 +00:00
Style::factory()
->type('file')
->data('media/css/auth-buttons.css');
if ($oauthlogin)
foreach (ORM::factory('Oauth')->list_active() as $oo)
$output .= $oo->plugin()->html();
2013-04-25 00:22:36 +00:00
$this->template->content = $output;
$this->template->shownavbar = FALSE;
2013-04-22 05:50:28 +00:00
}
2014-09-29 04:47:51 +00:00
/**
* Method redirect when authenticated user doesnt have access to the url
*/
2013-04-22 05:50:28 +00:00
public function action_noaccess() {
2013-04-25 00:22:36 +00:00
SystemMessage::factory()
->title(_('No access to requested resource'))
2014-09-08 13:42:05 +00:00
->type('danger')
2013-04-25 00:22:36 +00:00
->body(_('You do not have access to the requested resource, please contact your administrator.'));
2013-04-22 05:50:28 +00:00
}
2014-09-29 04:47:51 +00:00
/**
* Register for an account on the site
*/
public function action_register() {
$ao = ORM::factory('Account',$this->request->param('id'));
if ($this->request->post() AND $ao->values($this->request->post())->changed() AND (! $this->save($ao)))
2014-10-02 05:33:07 +00:00
$ao->reload()->values($this->request->post());
if ($ao->loaded()) {
$co = Company::instance();
// Send our email with the token
$email = Email::factory('login_activate')
->set('SITE',URL::base(TRUE,TRUE))
->set('SITE_ADMIN',$co->admin()->name())
->set('CODE',$ao->activate_code())
->set('EMAIL',$ao->email)
->set('ID',$ao->id)
->set('USER_NAME',$ao->name());
$email->to = array('email'=>array($ao->email=>$ao->name()));
$email->from = array('email'=>array($co->admin()->email=>$co->admin()->name()));
$email->subject = 'Please activate your account for '.$co->name();
$email->deliver();
SystemMessage::factory()
->title(_('Account Registered'))
->type('info')
->body(_('Please check your email for more instructions!'));
Session::instance()->set('activate',$ao);
HTTP::redirect('login/activate/'.$ao->id);
}
2014-09-29 04:47:51 +00:00
Block::factory()
->type('form-horizontal')
->title('Register Account')
->title_icon('fa-edit')
->body(View::factory('account/user/edit')->set('o',$ao));
}
/**
* Enable user password reset
*/
public function action_reset() {
// Minutes to keep our token
2014-10-02 05:33:07 +00:00
$token_expire = 15*60;
2014-09-29 04:47:51 +00:00
// If the user posted their details to reset their password
if ($this->request->post()) {
// If the username is correct, create a method token
if ($ao=ORM::factory('Account',array('email'=>$this->request->post('username'))) AND $ao->loaded()) {
2014-10-02 05:33:07 +00:00
$token = $ao->token($token_expire,'account','user:resetpassword',2);
if ($token) {
$co = Company::instance();
2014-09-29 04:47:51 +00:00
// Send our email with the token
$email = Email::factory('login_reset')
->set('SITE',URL::base(TRUE,TRUE))
->set('SITE_ADMIN',$co->admin()->name())
2014-10-02 05:33:07 +00:00
->set('TOKEN',$token)
2014-09-29 04:47:51 +00:00
->set('TOKEN_EXPIRE_MIN',$token_expire)
2014-10-02 05:33:07 +00:00
->set('USER_NAME',$ao->name());
2014-09-29 04:47:51 +00:00
2014-10-02 05:33:07 +00:00
$email->to = array('email'=>array($ao->email=>$ao->name()));
2014-09-29 04:47:51 +00:00
$email->from = array('email'=>array($co->admin()->email=>$co->admin()->name()));
$email->subject = 'Login Reset Token for '.$co->name();
$email->deliver();
// Log the password reset
$ao->log('Password reset token sent');
}
// Redirect to our password reset, the Auth will validate the token.
} elseif ($this->request->post('token')) {
HTTP::redirect(URL::link('user','account/resetpassword?token='.$this->request->post('token')));
}
// Show our token screen even if the email was invalid.
if ($this->request->post('username'))
$output = View::factory('login/reset_sent');
else
HTTP::redirect('login');
} else {
$output = View::factory('login/reset');
}
$this->template->content = $output;
$this->template->shownavbar = FALSE;
}
2013-04-22 05:50:28 +00:00
}
?>