Guest User

Kohana 3 AUTH

a guest
Apr 27th, 2010
2,982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.91 KB | None | 0 0
  1. create controller/register.php
  2.  
  3.     <?php defined('SYSPATH') or die('No direct script access.');
  4.  
  5.     class Controller_Register extends Controller_Template
  6.     {
  7.         public $template = 'register';
  8.  
  9.         function action_index()
  10.         {
  11.             $this->template->form = array
  12.             (
  13.                 'username' => '',
  14.                 'password' => '',
  15.                 'email' => ''
  16.             );
  17.             $this->template->errors = $this->template->form;
  18.  
  19.             if (Auth::instance()->logged_in() != 0)
  20.             {
  21.                 Request::instance()->redirect('/');
  22.             }
  23.  
  24.             if ($_POST)
  25.             {
  26.                 $user = ORM::factory('user');
  27.                 $post = $user->validate_create($_POST);
  28.                 if ($post->check())
  29.                 {
  30.                     $user->username = $_POST['username'];
  31.                     $user->email = $_POST['email'];
  32.                     $user->password = $_POST['password'];
  33.                     $user->save();
  34.                     $user->add('roles',ORM::factory('role', array('name' => 'login')));
  35.                     echo 'SAVED';
  36.                 }
  37.                 else
  38.                 {
  39.                     $this->template->form = arr::overwrite($this->template->form, $post->as_array());
  40.                     $this->template->errors = arr::overwrite($this->template->errors, $post->errors('register'));
  41.                 }
  42.             }
  43.         }
  44.     }
  45.  
  46.     ?>
  47.    
  48. create model/user.php (for validate of $_POST)
  49.  
  50.     <?php defined('SYSPATH') or die('No direct script access.');
  51.  
  52.     class Model_User extends Model_Auth_User
  53.     {
  54.         protected $_rules = array
  55.         (
  56.             'username' => array
  57.             (
  58.                 'not_empty' => NULL,
  59.                 'min_length' => array(4),
  60.                 'max_length' => array(32),
  61.                 'regex' => array('/^[-\pL\pN_.]++$/uD')
  62.             ),
  63.             'password' => array
  64.             (
  65.                 'not_empty' => NULL,
  66.                 'min_length' => array(5),
  67.                 'max_length' => array(42)
  68.             ),
  69.             'email' => array
  70.             (
  71.                 'not_empty' => NULL,
  72.                 'min_length' => array(5),
  73.                 'max_length' => array(127),
  74.                 'validate::email' => NULL
  75.             )
  76.         );
  77.  
  78.         protected $_callbacks = array
  79.         (
  80.             'username' => array('username_available'),
  81.             'email' => array('email_available')
  82.         );
  83.  
  84.         public function validate_create(&$array)
  85.         {
  86.             $array = Validate::factory($array)
  87.                 ->filter(TRUE, 'trim')
  88.                 ->rules('password', $this->_rules['password'])
  89.                 ->rules('username', $this->_rules['username'])
  90.                 ->rules('email', $this->_rules['email']);
  91.  
  92.             foreach ($this->_callbacks as $field => $callbacks)
  93.             {
  94.                 foreach ($callbacks as $callback)
  95.                 {
  96.                     $array->callback($field, array($this, $callback));
  97.                 }
  98.             }
  99.             return $array;
  100.         }
  101.     }
  102.  
  103.     ?>
  104.  
  105.    
  106. create views/register.php
  107.  
  108.     <?php defined('SYSPATH') or die('No direct access allowed.');
  109.  
  110.     echo form::open(null);
  111.     echo form::label('username', 'Name: ');
  112.     echo form::input('username', $form['username'], array('id' => 'username'));
  113.     echo (empty($errors['username'])) ? '' : $errors['username'];
  114.     echo '<br />';
  115.  
  116.     echo form::label('password', 'Key: ') ;
  117.     echo form::password('password', $form['password'], array('id' => 'password'));
  118.     echo ( empty ($errors['password'])) ? '' : $errors['password'];
  119.     echo '<br />';
  120.     echo form::label('email', 'E-Mail: ');
  121.     echo form::input('email', $form['email'], array('id' => 'email'));
  122.     echo (empty($errors['email'])) ? '' : $errors['email'];
  123.     echo '<br />';
  124.  
  125.     echo '<br />';
  126.     echo form::submit('Register', 'Register');
  127.     echo form::close();
  128.  
  129.     ?>
  130.  
  131. create controller/login.php
  132.  
  133.     <?php defined('SYSPATH') or die('No direct access allowed.');
  134.  
  135.     class Controller_Login extends Controller_Template
  136.     {
  137.         public $template = 'login';
  138.  
  139.         public function action_index()
  140.         {
  141.             if (Auth::instance()->logged_in())
  142.             {
  143.                 Request::instance()->redirect(Session::instance()->get('requested_url', '/'));
  144.             }
  145.             else
  146.             {
  147.                 if ($_POST)
  148.                 {
  149.                     $remember = isset($_POST['remember']) ? TRUE : FALSE;
  150.  
  151.                     $auth = Auth::instance();
  152.                     if ($auth->login($_POST['username'], $_POST['password'], $remember))
  153.                     {
  154.                         Request::instance()->redirect(Session::instance()->get('requested_url', '/'));
  155.                     }
  156.                     else
  157.                     {
  158.                         echo 'ERROR';
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.     ?>
  166.  
  167. create views/login.php
  168.  
  169.     <?php defined('SYSPATH') or die('No direct access allowed.');
  170.  
  171.     echo form::open(null);
  172.     echo form::label('username', 'Name: ');
  173.     echo form::input('username','',array('id' => 'username'));
  174.     echo '<br />';
  175.     echo form::label('password', 'Key: ');
  176.     echo form::password('password','',array('id' => 'password'));
  177.     echo '<br />';
  178.     echo form::label('remember', 'Remember me: ');
  179.     echo form::checkbox('remember', '1', FALSE, array('id' => 'remember'));
  180.  
  181.     echo '<br />';
  182.  
  183.     echo form::submit('login', 'Login');
  184.     echo form::close();
  185.  
  186.     ?>
  187.  
  188. In each Controller you use before() for create session and logged in
  189.  
  190.     class Controller_Portal extends Controller_Template
  191.     {
  192.         public function before()
  193.         {
  194.             parent::before();
  195.             $this->session = Session::instance();
  196.             $auth = Auth::instance();
  197.             $auth->auto_login();
  198.             if ($auth->logged_in())
  199.             {
  200.                 $this->user = Auth::instance()->get_user();
  201.             }
  202.         }
  203.  
  204.         public function action_index()
  205.         {
  206.             if (Auth::instance()->logged_in())
  207.             {
  208.                 echo $this->user->username;
  209.             }
  210.         }
  211.     }
Advertisement
Add Comment
Please, Sign In to add comment