Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Controller_Users extends Controller_Commons {
- public function action_index()
- {
- $view = View::factory('users/login');
- $view->title = 'Login Panel';
- $this->response->body = $view;
- }
- public function action_login()
- {
- if(Auth::check())
- {
- echo 'ya logeado';
- Response::redirect('/'); // user already logged in
- }
- $val = Validation::factory('users');
- $val->add_field('username', 'Your username', 'required|min_length[3]|max_length[16]');
- $val->add_field('password', 'Your password', 'required|min_length[3]|max_length[16]');
- $val->add_field('email', 'Your email', 'required|min_length[3]|max_length[16]');
- if($val->run())
- {
- $auth = Auth::instance();
- if($auth->login($val->validated('username'), $val->validated('passwd')))
- {
- Session::set_flash('notice', 'FLASH: logged in');
- Response::redirect('users');
- }
- else
- {
- $data['username'] = $val->validated('username');
- $data['errors'] = 'Wrong username/password. Try again';
- }
- }
- else
- {
- if($_POST)
- {
- $data['username'] = $val->validated('username');
- $data['errors'] = 'Wrong username/password combo. Try again';
- }
- else
- {
- $data['errors'] = false;
- }
- }
- $this->template->title = 'Login';
- $this->template->logged_in = false;
- $this->template->errors = @$data['errors'];
- $this->template->content = View::factory('users/login', $data);
- }
- public function action_logout()
- {
- Auth::instance()->logout();
- Response::redirect('/');
- }
- public function action_signup()
- {
- if ( Auth::check())
- {
- Response::redirect('/');
- }
- $val = Validation::factory('user_signup');
- $val->add_field('username', 'Your username', 'required|min_length[3]|max_length[16]');
- $val->add_field('password', 'Your password', 'required|min_length[3]|max_length[16]');
- $val->add_field('email', 'Your email', 'required|min_length[3]|max_length[16]');
- # print_r($val);
- if ( $val->run() )
- {
- $create_user = Auth::instance()->create_user($val->validated('username'),$val->validated('password'),$val->validated('email'));
- if( $create_user )
- {
- Session::set_flash('notice', 'FLASH: User created.');
- Response::redirect('users');
- }
- else
- {
- throw new Exception('An unexpected error occurred. Please try again.');
- }
- }
- else
- {
- if( $_POST )
- {
- $data['username'] = $val->validated('username');
- $data['login_error'] = 'All fields are required.';
- }
- else
- {
- $data['login_error'] = false;
- }
- }
- $this->template->title = 'Sign Up';
- $this->template->errors = @$data['login_error'];
- $this->template->content = View::factory('users/signup');
- }
- }
- ?>
Add Comment
Please, Sign In to add comment