Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. <?php
  2.     class authHelper{
  3.         protected $sessionHelper, $redirectorHelper, $tableName, $userColumn,
  4.                   $passColumn, $user, $pass, $loginController = 'index', $loginAction = 'index',
  5.                   $logoutController = 'index', $logoutAction = 'index';
  6.  
  7.         public function __construct(){
  8.             $this->sessionHelper = new SessionHelper();
  9.             $this->redirectorHelper = new RedirectorHelper();
  10.             return $this;
  11.         }
  12.  
  13.         public function setTableName( $val ){
  14.             $this->tableName = $val;
  15.             return $this;
  16.         }
  17.  
  18.         public function setUserColumn( $val ){
  19.             $this->userColumn = $val;
  20.             return $this;
  21.         }
  22.  
  23.         public function setPassColumn( $val ){
  24.             $this->passColumn = $val;
  25.             return $this;
  26.         }
  27.  
  28.         public function setUser( $val ){
  29.             $this->user = $val;
  30.             return $this;
  31.         }
  32.  
  33.         public function setPass( $val ){
  34.             $this->pass = $val;
  35.             return $this;
  36.         }
  37.  
  38.         public function setLoginControllerAction( $controller, $action ){
  39.             $this->loginController = $controller;
  40.             $this->loginAction = $action;
  41.             return $this;
  42.         }
  43.  
  44.         public function setLogoutControllerAction( $controller, $action ){
  45.             $this->logoutController = $controller;
  46.             $this->logoutAction = $action;
  47.             return $this;
  48.         }
  49.  
  50.         public function login(){
  51.             $db = new Model();
  52.             $db->_tabela = $this->tableName;
  53.             $where = $this->userColumn."='".$this->user."' and ".$this->passColumn."='".md5($this->pass)."'";
  54.             $sql = $db->read($where, '1');
  55.  
  56.             if ( count($sql) > 0 ):
  57.                 $this->sessionHelper->createSession("userAuth", true)
  58.                                     ->createSession('userData', $sql[0]);
  59.             else:
  60.                 die("Usuario nao existe.");
  61.             endif;
  62.  
  63.             $this->redirectorHelper->goToControllerAction($this->loginController, $this->loginAction);
  64.             return $this;
  65.         }
  66.  
  67.         public function logout(){
  68.             $this->sessionHelper->deleteSession("userAuth")
  69.                           ->deleteSession("userData");
  70.             $this->redirectorHelper->goToControllerAction($this->logoutController, $this->logoutAction);
  71.             return $this;
  72.         }
  73.  
  74.         public function checkLogin( $action = "boolean" ){
  75.             switch ($action) {
  76.                 case "boolean":
  77.                     if ( !$this->sessionHelper->checkSession("userAuth") )
  78.                         return false;
  79.                     else
  80.                         return true;
  81.                     break;
  82.                 case "redirect":
  83.                     if ( !$this->sessionHelper->checkSession("userAuth") )
  84.                         if ( $this->redirectorHelper->getCurrentController() != $this->loginController || $this->redirectorHelper->getCurrentAction() != $this->loginAction )
  85.                             $this->redirectorHelper->goToControllerAction($this->loginController, $this->loginAction);
  86.                     break;
  87.                 case "stop":
  88.                     if ( !$this->sessionHelper->checkSession("userAuth") )
  89.                         exit;
  90.                     break;
  91.             }
  92.             return $this;
  93.         }
  94.  
  95.         public function userData( $key ){
  96.             $s = $this->sessionHelper->selectSession("userData");
  97.             return $s[$key];
  98.         }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement