Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- abstract class Controller_Template_Auth extends Controller_Template_Ajax {
- public $template = 'template';
- public $user;
- protected $auth;
- public function before()
- {
- $this->auth = Auth::instance();
- $this->user = $this->auth->get_user();
- parent::before();
- if ( ! $this->user)
- {
- switch(strtolower($this->request->action)) {
- case 'login':
- case 'logout':
- break;
- default :
- $this->request->redirect('auth/login');
- break;
- }
- }
- else
- {
- $this->request->redirect('investors');
- }
- }
- public function after()
- {
- }
- } // End Controller_Template_Auth
- ===================================================================
- <?php defined('SYSPATH') or die('No direct script access.');
- class Controller_Auth extends Controller_Template_Auth
- {
- // Main base template
- public $template = 'template';
- public $auth_required = FALSE;
- /**
- * Index
- */
- public function action_index()
- {
- // Just redirect to login page
- $this->request->redirect('auth/login');
- }
- /**
- * Login
- */
- public function action_login()
- {
- echo 'in auth login';exit;
- // If a user lands on this page, assume he wants to do a fresh login
- Auth::instance()->logout();
- $post = $_POST;
- // Load the login page
- $this->template->title = 'Login';
- $this->template->content = View::factory('auth/login')
- ->bind('user', $user)
- ->bind('errors', $errors)
- ->bind('post', $post);
- //if the user is trying to login
- if ($_POST)
- {
- // did the user check the Remember Checkbox?
- $remember = isset($_POST['remember']);
- // Use Auth to login the user
- Auth::instance()->login($_POST['username'], $_POST['password'], $remember);
- if ( ! Auth::instance()->logged_in() )
- {
- $errors = array('Login failed. Please use a valid username and password.');
- return;
- }
- //go to the home page if successful
- $this->request->redirect('investors/profile');
- }
- }
- /**
- * Logout
- */
- public function action_logout()
- {
- // If a user lands on this page, assume he wants to do a fresh login
- Auth::instance()->logout();
- Cookie::delete('user');
- // Load the login page
- $this->template->title = 'Logout';
- $this->template->content = View::factory('auth/logout')
- ->bind('user', $user)
- ->bind('errors', $errors);
- }
- /**
- * Register
- *
- * This method assumes that the person is registering to gain access to the site so
- * at the minimum, they need to provide 3 things: USERNAME, PASSWORD, EMAIL address
- * and everything else are optional (for now)
- *
- */
- public function action_register()
- {
- // If we are processing a new registration (somehow a user gets to this page)
- // we need to log them out first
- Auth::instance()->logout();
- // Instantiate some objects
- $user = Sprig::factory('user');
- // Load the view
- $this->template->content = View::factory('auth/register')
- ->bind('user', $user)
- ->bind('errors', $errors);
- // If there is a post and $_POST is not empty
- if ($_POST)
- {
- try
- {
- // TODO: Sanitize values coming in
- $user->values($_POST);
- // Add the login role to the user
- $user->values(array(
- 'roles' => Sprig::factory('role', array('name' => 'login'))->load()->id,
- ));
- echo 'before save';
- // Create the user
- $user->create();
- echo 'after save';exit;
- // Sign the user in
- Auth::instance()->login($_POST['username'], $_POST['password']);
- // redirect to the user account
- Request::instance()->redirect('investors/profile');
- }
- catch (Validate_Exception $e)
- {
- $errors = $e->array->errors('validate');
- }
- }
- }
- }
- ==========================================================================
- <?php defined('SYSPATH') or die('No direct script access.');
- abstract class Controller_Template_Ajax extends Controller_Template {
- protected $_ajax = FALSE;
- public function before()
- {
- // Open remote URLs in a new window
- html::$windowed_urls = TRUE;
- parent::before();
- $this->template->title =
- $this->template->content = '';
- if (Request::$is_ajax OR $this->request !== Request::instance())
- {
- // This is an AJAX-like request
- $this->_ajax = TRUE;
- }
- }
- public function after()
- {
- if ($this->_ajax === TRUE)
- {
- // Use the template content as the response
- $this->request->response = $this->template->content;
- }
- else
- {
- parent::after();
- }
- }
- } // End Controller_Template_Ajax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement