<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class PermisosMiddleware
{
    protected $controller;
    protected $ci;

    public function __construct($controller, $ci)
    {
        $this->controller = $controller;
        $this->ci = $ci;
    }

    public function run()
    {

        $method = $_SERVER['REQUEST_METHOD'];// optenemos el metodo
        $Ruta   = $_SERVER['REQUEST_URI']; // optenemos la ruta 
        $array = explode ( '/', $Ruta );
        $Ruta =$array[2]; // optenemos la parte de la ruta que nos interesa
       // $ID =10; //$this->ci->session->userdata("User") //optenemos la sesion id de usuario
       $Token = obtnerToken();
  $Autho = "";
 if($Token != FALSE){
  $Autho =str_replace('Bearer ','', $Token);
 }
 if( $Autho !=""){
  $Autho =str_replace('Bearer ','', $Autho);
  $Token = ValidarToken($Autho);
}
       if ( $Token === FALSE ){
        $DatosGET= isset($_GET["x"]) ? $_GET["x"]: "" ;
       
        $DatosGET =  $DatosGET!=""?ValidarToken($DatosGET) :"";
        $Token  =$DatosGET!=''?$DatosGET: $Token ;
       }
       $ID =  $Token== FALSE ? $this->ci->session->userdata("User") :  $Token ; 
     
     
        if( $ID===null){
            redirect("/");
            return; 

        }
        $User = $this->ci->UserModels->getUser($ID);
       
        $Nivel =$User[0]->Nivel;
        $posicion_coincidencia = strpos($Ruta,'Meta');
      
        if( $posicion_coincidencia != false){
            return;
        }
        $Ruta = str_replace("Meta","",$Ruta);
        $Ruta = str_replace("Pdf","",$Ruta);
        $Ruta = str_replace("Excel","",$Ruta);
        
        $Permiso = $this->ci->PermisosModels->getPermisosFiltro("sRuta^$Ruta|Nivel^  $Nivel");
        
        
        if($User[0]->Nivel == 0){
            return;
        }
       
        if(count($Permiso)===0){
      
            if(count($array)>3){
                
                $Codigo = "CÃ³digo";
                if($array[2] === "User" AND $array[3] === $User[0]->$Codigo ){
                    return; 
                }
            }
         return;
            redirect("/");
            return; 
        }
      
     
        $Permiso = $Permiso[0]->Permiso;
        switch ($Permiso){
            case 1: 
                return;
            break;
            case 2: 
            if($method !='POST'){
                redirect("/");
                return;
            }
            break;
            case 3:
                if($method !='GET'){
                    redirect("/");
                    return;
                }
             break;
             case 4:
                if($method !='GET' AND $method !='POST'){
                    redirect("/");
                    return;
                }
             break;
             case 5:
                if($method !='GET' AND $method !='POST' AND  $method !='PUT'){
                    redirect("/");
                    return;
                }
             break;
             case 6:
                redirect("/");
                return;
             break;
            default:
            redirect("/");
            break;
        }   
    }
} 