public
Last active

Aqui estaran las funcionalidades de login y registro controller_Account

  • Download Gist
account.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Controller_Account
*
* Aqui estaran las funcionalidades de login y registro
*
* @author Javier
* @package Backend
*
*/
class Controller_Account extends Controller {
 
/**
*
* @var User $auth contains object a1 user
*/
public $auth;
/**
*
* Variable que guardara la sesiĆ³n
* @var Session $session
*/
public $session;
/**
*
* Vista que usara el sistema
* @var View|String $view
*/
public $view;
/**
*
* Aqui se cargara el modelo users
* @var Model_Users $users
*/
public $users;
/**
* (non-PHPdoc)
* @see system/classes/kohana/Kohana_Controller::before()
*/
public function before()
{
parent::before();
$this->auth = A1::instance();
$this->session = Session::instance();
$this->users = new Model_Users();
}
public function action_index()
{
$this->action_login();
}
public function action_login()
{
$errors = array();
if($this->request->method()=="POST")
{
$username = Arr::get($_POST, 'username');
$password = Arr::get($_POST, 'password');
$remember = (isset($_POST['remember']))?TRUE:FALSE;
if($this->auth->login($username,$password,$remember))
{
$session = $this->auth->session()->as_array();
$session["user"] = $this->auth->get_user();
$this->session = $session;
$this->request->redirect(Route::url('default',array('controller' => 'backend','action' => FALSE)));
}
else
{
$errors['login'] = "Datos no validos";
}
}
$this->view = View::factory('backend/login')
->set('posturl',Route::url('default',array('controller' => 'account', 'action' => 'login')))
->set('registerurl',Route::url('default',array('controller' => 'account', 'action' => 'register')))
->set("errors",$errors);
}
public function action_register()
{
$errors = array();
if($this->request->method()== "POST")
{
$username = Arr::get($_POST, 'username');
$password = Arr::get($_POST, 'password');
$errors['register'] = "Error al intentar registrarse";
if($this->users->unique_user($username)=== FALSE)
{
$new_user = $this->users->save_user($username,$password,$this->auth);
if($new_user->saved())
{
$this->auth->complete_login($new_user);
$this->request->redirect(Route::url('default',array('controller' => 'account', 'action' => 'login')).'?saved');
}
$errors["register"] = "No se pudo guardar el nuevo registro";
}
$errors['register'] = "El usuario {$username} ya existe en la base de datos";
}
$this->view = View::factory('backend/register')
->set('errors',$errors)
->set('posturl',Route::url('default',array('controller' => 'account', 'action' => 'login')))
->set('registerurl',Route::url('default',array('controller' => 'account', 'action' => 'register')));
}
public function action_logout()
{
$this->auth->logout();
$this->request->redirect(Route::url('default',array('controller' => 'backend','action' => FALSE)));
}
public function after()
{
$this->response->body($this->view);
parent::after();
}
 
} // End Account

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.