Advertisement
Guest User

Untitled

a guest
May 28th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.36 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. abstract class Controller_Template_Auth extends Controller_Template_Ajax {
  4.  
  5.     public $template = 'template';
  6.     public $user;
  7.     protected $auth;
  8.  
  9.     public function before()
  10.     {      
  11.         $this->auth = Auth::instance();
  12.                
  13.         $this->user = $this->auth->get_user();
  14.        
  15.         parent::before();
  16.         if ( ! $this->user)
  17.         {
  18.             switch(strtolower($this->request->action)) {
  19.                 case 'login':
  20.                 case 'logout':
  21.                     break;
  22.                 default :
  23.                     $this->request->redirect('auth/login');
  24.                     break;
  25.             }
  26.         }
  27.         else
  28.         {
  29.             $this->request->redirect('investors');
  30.         }
  31.     }
  32.  
  33.     public function after()
  34.     {
  35.        
  36.     }  
  37. } // End Controller_Template_Auth
  38.  
  39. ===================================================================
  40.  
  41. <?php defined('SYSPATH') or die('No direct script access.');
  42.  
  43. class Controller_Auth extends Controller_Template_Auth
  44. {
  45.  
  46.     // Main base template
  47.     public $template = 'template';
  48.  
  49.     public $auth_required = FALSE;  
  50.  
  51.     /**
  52.      * Index
  53.      */
  54.     public function action_index()
  55.     {
  56.         // Just redirect to login page
  57.         $this->request->redirect('auth/login');
  58.     }
  59.  
  60.     /**
  61.      * Login
  62.      */
  63.     public function action_login()
  64.     {
  65.         echo 'in auth login';exit;
  66.         // If a user lands on this page, assume he wants to do a fresh login
  67.         Auth::instance()->logout();        
  68.        
  69.         $post = $_POST;
  70.        
  71.         // Load the login page
  72.         $this->template->title = 'Login';
  73.         $this->template->content = View::factory('auth/login')
  74.             ->bind('user', $user)
  75.             ->bind('errors', $errors)
  76.             ->bind('post', $post);
  77.  
  78.         //if the user is trying to login
  79.         if ($_POST)
  80.         {
  81.             // did the user check the Remember Checkbox?
  82.             $remember = isset($_POST['remember']);
  83.  
  84.             // Use Auth to login the user
  85.             Auth::instance()->login($_POST['username'], $_POST['password'], $remember);
  86.             if ( ! Auth::instance()->logged_in() )
  87.             {
  88.                 $errors = array('Login failed.  Please use a valid username and password.');
  89.                 return;
  90.             }
  91.  
  92.             //go to the home page if successful
  93.             $this->request->redirect('investors/profile');
  94.         }
  95.     }
  96.  
  97.  
  98.     /**
  99.      * Logout
  100.      */
  101.     public function action_logout()
  102.     {
  103.         // If a user lands on this page, assume he wants to do a fresh login
  104.         Auth::instance()->logout();
  105.         Cookie::delete('user');    
  106.  
  107.         // Load the login page
  108.         $this->template->title = 'Logout';
  109.         $this->template->content = View::factory('auth/logout')
  110.             ->bind('user', $user)
  111.             ->bind('errors', $errors);
  112.     }
  113.  
  114.     /**
  115.      * Register
  116.      *
  117.      * This method assumes that the person is registering to gain access to the site so
  118.      * at the minimum, they need to provide 3 things:  USERNAME, PASSWORD, EMAIL address
  119.      * and everything else are optional (for now)
  120.      *
  121.      */
  122.     public function action_register()
  123.     {  
  124.         // If we are processing a new registration (somehow a user gets to this page)
  125.         // we need to log them out first
  126.         Auth::instance()->logout();
  127.  
  128.         // Instantiate some objects
  129.         $user = Sprig::factory('user');
  130.  
  131.         // Load the view
  132.         $this->template->content = View::factory('auth/register')
  133.             ->bind('user', $user)
  134.             ->bind('errors', $errors);
  135.  
  136.         // If there is a post and $_POST is not empty
  137.         if ($_POST)
  138.         {
  139.             try
  140.             {
  141.                 // TODO: Sanitize values coming in
  142.                 $user->values($_POST);
  143.                
  144.                 // Add the login role to the user
  145.                 $user->values(array(
  146.                     'roles' => Sprig::factory('role', array('name' => 'login'))->load()->id,
  147.                 ));
  148. echo 'before save';
  149.                 // Create the user
  150.                 $user->create();
  151. echo 'after save';exit;
  152.                 // Sign the user in
  153.                 Auth::instance()->login($_POST['username'], $_POST['password']);
  154.  
  155.                 // redirect to the user account
  156.                 Request::instance()->redirect('investors/profile');
  157.  
  158.             }
  159.             catch (Validate_Exception $e)
  160.             {
  161.                 $errors = $e->array->errors('validate');
  162.             }
  163.  
  164.         }
  165.     }
  166. }
  167.  
  168. ==========================================================================
  169.  
  170. <?php defined('SYSPATH') or die('No direct script access.');
  171.  
  172. abstract class Controller_Template_Ajax extends Controller_Template {
  173.  
  174.     protected $_ajax = FALSE;
  175.  
  176.     public function before()
  177.     {
  178.         // Open remote URLs in a new window
  179.         html::$windowed_urls = TRUE;
  180.  
  181.         parent::before();
  182.  
  183.         $this->template->title =
  184.         $this->template->content = '';
  185.  
  186.         if (Request::$is_ajax OR $this->request !== Request::instance())
  187.         {
  188.             // This is an AJAX-like request
  189.             $this->_ajax = TRUE;
  190.         }
  191.     }
  192.  
  193.     public function after()
  194.     {
  195.         if ($this->_ajax === TRUE)
  196.         {
  197.             // Use the template content as the response
  198.             $this->request->response = $this->template->content;
  199.         }
  200.         else
  201.         {
  202.             parent::after();
  203.         }
  204.     }
  205.  
  206. } // End Controller_Template_Ajax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement