Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. /application/bootstrap.php
  2.  
  3. Kohana::modules(array(
  4.     'auth'       => MODPATH.'auth',       // Basic authentication
  5.     // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
  6.     'database'   => MODPATH.'database',   // Database access
  7.     // 'image'      => MODPATH.'image',      // Image manipulation
  8.     'orm'        => MODPATH.'orm',        // Object Relationship Mapping
  9.     // 'pagination' => MODPATH.'pagination', // Paging of results
  10.     // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
  11.     ));
  12.  
  13. application/config/auth.php
  14.  
  15. <?php defined('SYSPATH') OR die('No direct access allowed.');
  16.  
  17. return array
  18. (
  19.     'driver' => 'ORM',
  20.     'hash_method' => 'sha1',
  21.     'salt_pattern' => '1, 3, 5, 9, 14, 15, 20, 21, 28, 30',
  22.     'lifetime' => 1209600, //2 weeks
  23.     'session_key' => 'auth_user',
  24.     'users' => array
  25.     (
  26.         // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
  27.     ),
  28. );
  29.  
  30.  
  31. application/classes/controller/register.php
  32.  
  33. <?php defined('SYSPATH') OR die('No direct access allowed.');
  34.  
  35. class Controller_Register extends Controller_Common
  36. {
  37.     public function action_index()
  38.     {
  39.         $user = ORM::factory('user');
  40.  
  41.         $post = Validate::factory($_POST)
  42.        
  43.             ->filter(TRUE, 'trim')
  44.             ->filter('username', 'strtolower')
  45.  
  46.             ->rule('username', 'not_empty')
  47.             ->rule('username', 'regex', array('/^[a-z_.0-9]++$/iD'))
  48.             ->callback('username', array($user, 'username_available'))
  49.  
  50.             ->rule('password', 'not_empty')
  51.  
  52.             ->rule('confirm', 'not_empty')
  53.             ->rule('confirm', 'matches', array('password'))
  54.  
  55.             ->rule('email', 'not_empty')
  56.             ->rule('email', 'email')
  57.             ->callback('email', array($user, 'email_available'));
  58.  
  59.         if ($post->check())
  60.         {
  61.             // Create a new user record in the database
  62.             $user = Model_Auth_User::factory('user');
  63.             $user->username = $_POST['username'];
  64.             $user->email = $_POST['email'];
  65.             $user->password = $_POST['email'];
  66.             $user->save();
  67.  
  68.             $login_role = new Model_Role(array('name' =>'login'));
  69.             $user->add('roles',$login_role);
  70.  
  71.             $this->request->redirect('/?msg='.urldecode('please log in now. sorry!'));
  72.         }
  73.  
  74.         // Validation failed, collect the errors
  75.         $errors = $post->errors('user');
  76.  
  77.         // Display the registration form
  78.         $this->template
  79.             ->bind('post', $post)
  80.             ->bind('errors', $errors);
  81.     }
  82. }
  83.  
  84.  
  85. application/classes/controller/default.php
  86.  
  87. <?php defined('SYSPATH') OR die('No direct access allowed.');
  88.  
  89. class Controller_Default extends Controller_Common
  90. {
  91.     public function action_index()
  92.     {
  93.         $msg = '';
  94.         $auth = Auth::instance();
  95.  
  96.         if ($auth->logged_in()) {
  97.             $this->request->redirect('/chat');
  98.         }
  99.  
  100.         $post = Validate::factory($_POST)
  101.             ->filter(TRUE, 'trim')
  102.             ->filter('username', 'strtolower')
  103.  
  104.             ->rule('username', 'not_empty')
  105.  
  106.             ->rule('password', 'not_empty');
  107.  
  108.         if ($post->check()) {
  109.             $login = $auth->login($_POST['username'], $_POST['password'], true);
  110.            
  111.             if ($login) {
  112.                 $this->request->redirect('/chat');
  113.             } else {
  114.                 $msg = 'wrong password.';
  115.             }
  116.         }
  117.  
  118.         $msg = !empty($_REQUEST['msg']) ? $_REQUEST['msg'] : $msg;
  119.  
  120.         $this->template->post = $post;
  121.         $this->template->errors = $post->errors('user');
  122.         $this->template->msg = $msg;
  123.        
  124.     }
  125. }
  126.  
  127.  
  128. /application/classes/controller/common.php (simple extension that sets up my templates based on the requested path)
  129. <?php defined('SYSPATH') or die('No direct script access.');
  130.  
  131. class Controller_Common extends Controller_Template {
  132.     public function __construct(Kohana_Request $request)
  133.     {
  134.         parent::__construct($request);
  135.         $this->template = $request->controller . '/' . $request->action;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement