ValidaSesion.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Clase para validar la sesión del usuario y que el usuario esté activo.
  4. *
  5. * @author Alejandro
  6. */
  7. class ValidaSesion {
  8. private $acceso, $edicion, $periodo;//guarda si el usuario puede acceder y/o editar
  9. private $error, $hasError = false;
  10. private $fecha_jefes;//fecha límite para jefes de carrera
  11. /**
  12. * Constructor
  13. *
  14. * @param object $pdo Conexión activa a base de datos a través de PDO
  15. * @param int $submenu ID del submenú que se busca
  16. * @return boolean
  17. */
  18. function __construct($pdo, $submenu, $sistema){
  19. if (session_status() == PHP_SESSION_NONE) {
  20. session_start();
  21. }
  22. $session_life = 1*60*60;//1 hora convertido a segundos
  23. // check to see if $_SESSION["timeout"] is set
  24. if (isset($_SESSION["timeout"])) {
  25. // calculate the session's "time to live"
  26. $sessionTTL = time() - $_SESSION["timeout"];
  27. if ($sessionTTL > $session_life) {
  28. $this->terminaSesion();
  29. }
  30. }else
  31. $this->terminaSesion();
  32. $_SESSION["timeout"] = time();
  33. if(!isset($_SESSION["usuario_id"]) || $_SESSION["usuario_id"] == "") $this->terminaSesion();
  34. if(!isset($_SESSION["periodo_id"]) || $_SESSION["periodo_id"] == "") $this->periodo = false;
  35. else $this->periodo = true;
  36. //existe id en sesión?
  37. if(array_key_exists("usuario_id", $_SESSION) && is_numeric($_SESSION["usuario_id"]) && $_SESSION["usuario_id"] > 0){
  38. if(is_int($submenu)){
  39. //valida permisos
  40. $stmt = $pdo->prepare('Select * from fs_validapermisos(:usr, :sub, :sist)');//devuelve: permiso, edicion
  41. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  42. $stmt->bindParam(":sub", $submenu);
  43. $stmt->bindParam(":sist", $sistema);
  44. if(!$stmt->execute()){
  45. $this->hasError = true;
  46. $this->error = $stmt->errorInfo();
  47. }else{
  48. $rs = $stmt->fetch();
  49. $stmt->closeCursor(); // cierra conexion de resultado
  50. $stmt = null; // cierra conexion
  51. //si es administrador de sistema, siempre es true
  52. $this->acceso = (bool) $rs["permiso"];
  53. $this->edicion = (bool) $rs["edicion"];
  54. $rs = null;
  55. }
  56. }else if(is_array($submenu)){
  57. $stmt = $pdo->prepare('Select * from fs_validapermisos(:usr, :sub, :sist)');//devuelve: permiso, edicion
  58. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  59. $stmt->bindParam(":sist", $sistema);
  60. foreach($submenu as $subItem){
  61. if(!$this->hasError){
  62. //valida permisos
  63. $stmt->bindParam(":sub", $subItem);
  64. if(!$stmt->execute()){
  65. $this->hasError = true;
  66. $this->error = $stmt->errorInfo();
  67. }else{
  68. $rs = $stmt->fetch();
  69. //si es administrador de sistema, siempre es true
  70. $this->acceso = $this->acceso || (bool) $rs["permiso"];
  71. $this->edicion = $this->edicion || (bool) $rs["edicion"];
  72. $rs = null;
  73. }
  74. }
  75. }
  76. $stmt->closeCursor(); // cierra conexion de resultado
  77. $stmt = null; // cierra conexion
  78. }else if(is_null($submenu)){
  79. //Valida permisos de acceso sistema
  80. $stmt = $pdo->prepare('Select * from fs_validasistema(:usr, :sist)');//devuelve: permiso, edicion
  81. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  82. $stmt->bindParam(":sist", $sistema);
  83. if(!$stmt->execute()){
  84. $this->hasError = true;
  85. $this->error = $stmt->errorInfo();
  86. }else{
  87. $rs = $stmt->fetch();
  88. $stmt->closeCursor(); // cierra conexion de resultado
  89. $stmt = null; // cierra conexion
  90. $this->acceso = (bool) $rs["permiso"];
  91. $this->edicion = false;//acceso a sistema no tiene edición
  92. $rs = null;
  93. }
  94. }else{
  95. $this->hasError = true;
  96. $this->error = "Dato no válido en validación de permisos";
  97. }
  98. //carga datos de periodo
  99. if(!is_null($pdo) && !$this->hasError && $this->periodo){
  100. $stmt = $pdo->prepare('Select * from fs_periodo(:id, NULL, NULL, NULL)');
  101. $stmt->bindParam(":id", $_SESSION["periodo_id"]);
  102. if(!$stmt->execute()){
  103. $this->hasError = true;
  104. $this->error = $stmt->errorInfo();
  105. }else{
  106. $rs = $stmt->fetch();
  107. $stmt->closeCursor(); // cierra conexion de resultado
  108. $stmt = null; // cierra conexion
  109. if($rs["Periodo_id"] == "") $this->periodo = false;
  110. else{
  111. $this->periodo = true;
  112. $this->fecha_jefes = $rs["Periodo_fecha_jefes"];
  113. }
  114. }
  115. }
  116. }else{
  117. $this->error = "No existe la llave en sesión";
  118. $this->hasError = true;
  119. $this->acceso = false;
  120. $this->edicion = false;
  121. }
  122. if($this->hasError){
  123. $this->acceso = false;
  124. $this->edicion = false;
  125. }
  126. }
  127. function fechaJefes(){//devuelve true si están dentro de fecha del periodo
  128. if($this->periodo){
  129. if($_SESSION["jefe_carrera"]){
  130. $hoy = strtotime(date('Y-m-d'));
  131. if($hoy <= strtotime($this->fecha_jefes)){//dentro del límite
  132. return true;
  133. }
  134. }else
  135. return true;
  136. }
  137. return false;
  138. }
  139. function getFechaJefes(){//devuelve fecha
  140. return $this->fecha_jefes;
  141. }
  142. function tieneAcceso(){
  143. return $this->acceso;
  144. }
  145. function puedeEditar(){
  146. return $this->acceso && $this->edicion;
  147. }
  148. function tieneError(){
  149. return $this->hasError;
  150. }
  151. function getError(){
  152. return $this->error;
  153. }
  154. function terminaSesion(){
  155. $_SESSION = array();
  156. session_destroy();
  157. //$pag = $this->getFirstDirectory($_SERVER['PHP_SELF'])."/salir.php?expired=1";
  158. $pag = $_SERVER['SERVER_NAME']."/salir.php?expired=1";
  159. header("Location: http://".$pag);
  160. exit();
  161. }
  162. function validaPeriodoUsuario(){
  163. if(!$this->periodo){
  164. $pag = $this->getFirstDirectory($_SERVER['PHP_SELF'])."/main.php?error=0";
  165. header("Location: ".$pag);
  166. //$pag = $_SERVER["SERVER_NAME"]."/apsa/main.php?error=0";
  167. //header("Location: http://".$pag);
  168. exit();
  169. }
  170. //return $this->periodo;
  171. }
  172. private function getFirstDirectory($pag){
  173. if(dirname($pag) == '/'){
  174. return $pag;
  175. }
  176. return $this->getFirstDirectory(dirname($pag));
  177. }
  178. }