Guest User

Untitled

a guest
Oct 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2.  
  3. class Controller_Users extends Controller_Commons {
  4.  
  5.     public function action_index()
  6.     {
  7.         $view = View::factory('users/login');
  8.        
  9.         $view->title = 'Login Panel';
  10.        
  11.         $this->response->body = $view;
  12.     }
  13.  
  14.     public function action_login()
  15.     {
  16.         if(Auth::check())
  17.         {
  18.             echo 'ya logeado';
  19.             Response::redirect('/'); // user already logged in
  20.         }
  21.  
  22.         $val = Validation::factory('users');
  23.         $val->add_field('username', 'Your username', 'required|min_length[3]|max_length[16]');
  24.         $val->add_field('password', 'Your password', 'required|min_length[3]|max_length[16]');
  25.         $val->add_field('email', 'Your email', 'required|min_length[3]|max_length[16]');
  26.         if($val->run())
  27.         {
  28.             $auth = Auth::instance();
  29.             if($auth->login($val->validated('username'), $val->validated('passwd')))
  30.             {
  31.                 Session::set_flash('notice', 'FLASH: logged in');
  32.                 Response::redirect('users');
  33.             }
  34.             else
  35.             {
  36.                 $data['username'] = $val->validated('username');
  37.                 $data['errors'] = 'Wrong username/password. Try again';
  38.             }
  39.         }
  40.         else
  41.         {
  42.             if($_POST)
  43.             {
  44.                 $data['username'] = $val->validated('username');
  45.                 $data['errors'] = 'Wrong username/password combo. Try again';
  46.             }
  47.             else
  48.             {
  49.                 $data['errors'] = false;
  50.             }
  51.         }
  52.         $this->template->title = 'Login';
  53.         $this->template->logged_in = false;
  54.         $this->template->errors = @$data['errors'];
  55.         $this->template->content = View::factory('users/login', $data);
  56.     }
  57.    
  58.     public function action_logout()
  59.     {
  60.         Auth::instance()->logout();
  61.         Response::redirect('/');
  62.     }
  63.    
  64.     public function action_signup()
  65.     {
  66.     if ( Auth::check())
  67.     {
  68.         Response::redirect('/');
  69.     }
  70.     $val = Validation::factory('user_signup');
  71.     $val->add_field('username', 'Your username', 'required|min_length[3]|max_length[16]');
  72.     $val->add_field('password', 'Your password', 'required|min_length[3]|max_length[16]');
  73.     $val->add_field('email', 'Your email', 'required|min_length[3]|max_length[16]');
  74.    # print_r($val);
  75.    if ( $val->run() )
  76.     {
  77.         $create_user = Auth::instance()->create_user($val->validated('username'),$val->validated('password'),$val->validated('email'));
  78.         if( $create_user )
  79.         {
  80.             Session::set_flash('notice', 'FLASH: User created.');
  81.             Response::redirect('users');
  82.         }
  83.         else
  84.         {
  85.             throw new Exception('An unexpected error occurred. Please try again.');
  86.         }
  87.     }
  88.     else
  89.     {
  90.         if( $_POST )
  91.         {
  92.             $data['username'] = $val->validated('username');
  93.             $data['login_error'] = 'All fields are required.';
  94.         }
  95.         else
  96.         {
  97.             $data['login_error'] = false;
  98.         }
  99.     }
  100.     $this->template->title = 'Sign Up';
  101.     $this->template->errors = @$data['login_error'];
  102.     $this->template->content = View::factory('users/signup');
  103.     }
  104. }
  105. ?>
Add Comment
Please, Sign In to add comment