actualización de autenticación a validacion_sgu.wsdl
Some checks failed
Deploy Pruebas / deploy (push) Has been cancelled

This commit is contained in:
2024-08-27 00:39:52 +00:00
parent a834d7e014
commit db3d0b5d39

View File

@@ -1,156 +1,156 @@
<?php <?php
ini_set('display_errors', 1); ini_set('display_errors', 1);
ini_set('display_startup_errors', 1); ini_set('display_startup_errors', 1);
error_reporting(E_ALL); error_reporting(E_ALL);
date_default_timezone_set('America/Mexico_City'); date_default_timezone_set('America/Mexico_City');
$currentTime = time(); $currentTime = time();
$endOfDay = strtotime('tomorrow') - 1; $endOfDay = strtotime('tomorrow') - 1;
$remainingTime = $endOfDay - $currentTime; $remainingTime = $endOfDay - $currentTime;
session_set_cookie_params($remainingTime, '/', $_SERVER['HTTP_HOST'], false, true); session_set_cookie_params($remainingTime, '/', $_SERVER['HTTP_HOST'], false, true);
session_start(); session_start();
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/bd_pdo.php"; require_once "{$_SERVER['DOCUMENT_ROOT']}/include/bd_pdo.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_logasistencia.php"; require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_logasistencia.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/vendor/autoload.php"; require_once "{$_SERVER['DOCUMENT_ROOT']}/vendor/autoload.php";
/* /*
$user->acceso // Devuelve el tipo de acceso del usuario. Si es administrador, retorna "w". De lo contrario, verifica el tipo de acceso a una página específica y retorna ese valor. $user->acceso // Devuelve el tipo de acceso del usuario. Si es administrador, retorna "w". De lo contrario, verifica el tipo de acceso a una página específica y retorna ese valor.
$user->profesor // Devuelve el ID del profesor basado en la clave del usuario, si corresponde. $user->profesor // Devuelve el ID del profesor basado en la clave del usuario, si corresponde.
$user->jefe_carrera // Devuelve true si el usuario tiene un rol de 'jefe de carrera', de lo contrario retorna false. $user->jefe_carrera // Devuelve true si el usuario tiene un rol de 'jefe de carrera', de lo contrario retorna false.
$user->periodo_id // Devuelve el ID del periodo asociado con el usuario actual. $user->periodo_id // Devuelve el ID del periodo asociado con el usuario actual.
$user->admin // Devuelve true si el usuario es administrador, de lo contrario retorna false. $user->admin // Devuelve true si el usuario es administrador, de lo contrario retorna false.
$user->facultad // Devuelve un array con el nombre de la facultad y el ID de la facultad asociado con el usuario actual, si está disponible. $user->facultad // Devuelve un array con el nombre de la facultad y el ID de la facultad asociado con el usuario actual, si está disponible.
$user->rol // Devuelve un array con el título del rol y el ID del rol asociado con el usuario actual. Si no tiene un rol definido, se le asigna por defecto el rol 'docente'. $user->rol // Devuelve un array con el título del rol y el ID del rol asociado con el usuario actual. Si no tiene un rol definido, se le asigna por defecto el rol 'docente'.
*/ */
class Login class Login
{ {
private function es_usuario(): bool private function es_usuario(): bool
{ {
global $db; global $db;
return $db->where('usuario_clave', $this->user['clave'])->has("usuario"); return $db->where('usuario_clave', $this->user['clave'])->has("usuario");
} }
public function __get($property) public function __get($property)
{ {
global $db; global $db;
return match ($property) { return match ($property) {
'acceso' => $this->access(), 'acceso' => $this->access(),
'profesor' => $db->where('profesor_clave', preg_replace('/\D/', '', $this->user['clave']))->getOne("profesor")['profesor_id'] ?? null, 'profesor' => $db->where('profesor_clave', preg_replace('/\D/', '', $this->user['clave']))->getOne("profesor")['profesor_id'] ?? null,
'jefe_carrera' => $db->where('usuario_id', $this->user["id"])->getOne('usuario')['rol_id'] == 11, 'jefe_carrera' => $db->where('usuario_id', $this->user["id"])->getOne('usuario')['rol_id'] == 11,
'periodo_id' => $db->where('usuario_id', $this->user["id"])->getOne('usuario')["periodo_id"], 'periodo_id' => $db->where('usuario_id', $this->user["id"])->getOne('usuario')["periodo_id"],
'admin' => $this->es_usuario() and $db->where('usuario_id', $this->user["id"])->getOne('usuario')["usuario_admin"], 'admin' => $this->es_usuario() and $db->where('usuario_id', $this->user["id"])->getOne('usuario')["usuario_admin"],
'facultad' => $this->es_usuario() 'facultad' => $this->es_usuario()
? $db ? $db
->where('usuario_id', $this->user["id"]) ->where('usuario_id', $this->user["id"])
->join('facultad', 'facultad.facultad_id = usuario.facultad_id', 'LEFT') ->join('facultad', 'facultad.facultad_id = usuario.facultad_id', 'LEFT')
->getOne('usuario', 'facultad.facultad_nombre as facultad, facultad.facultad_id') ->getOne('usuario', 'facultad.facultad_nombre as facultad, facultad.facultad_id')
: array ('facultad' => null, 'facultad_id' => null), : array ('facultad' => null, 'facultad_id' => null),
'rol' => $this->es_usuario() 'rol' => $this->es_usuario()
? $db ? $db
->join('rol', 'rol.rol_id = usuario.rol_id') ->join('rol', 'rol.rol_id = usuario.rol_id')
->where('usuario_id', $this->user["id"]) ->where('usuario_id', $this->user["id"])
->getOne('usuario', 'rol.rol_titulo as rol, rol.rol_id') ->getOne('usuario', 'rol.rol_titulo as rol, rol.rol_id')
: $db : $db
->where('rol_titulo', 'docente', 'ILIKE') ->where('rol_titulo', 'docente', 'ILIKE')
->getOne('rol', 'rol.rol_titulo as rol, rol.rol_id'), ->getOne('rol', 'rol.rol_titulo as rol, rol.rol_id'),
default => throw new Exception("Propiedad no definida"), default => throw new Exception("Propiedad no definida"),
}; };
} }
public function __construct(public array $user) public function __construct(public array $user)
{ {
} }
public function print_to_log(string $desc, array $old = null, array $new = null): void public function print_to_log(string $desc, array $old = null, array $new = null): void
{ {
$log = new classes\LogAsistencias(); $log = new classes\LogAsistencias();
if ($old) if ($old)
$desc .= " |#| OLD:" . json_encode($old); $desc .= " |#| OLD:" . json_encode($old);
if ($new) if ($new)
$desc .= " |#| NEW:" . json_encode($new); $desc .= " |#| NEW:" . json_encode($new);
$log->appendLog($this->user["id"], $this->user["nombre"], $desc); $log->appendLog($this->user["id"], $this->user["nombre"], $desc);
} }
public function access(string $pagina = null): string|null public function access(string $pagina = null): string|null
{ {
global $db; global $db;
if ($this->admin) if ($this->admin)
return "w"; return "w";
$acceso = $db $acceso = $db
->where('id', $this->user["id"]) ->where('id', $this->user["id"])
->where('pagina_ruta', $pagina ?? substr(basename($_SERVER['PHP_SELF']), 0, -4)) ->where('pagina_ruta', $pagina ?? substr(basename($_SERVER['PHP_SELF']), 0, -4))
->getOne('permiso_view'); ->getOne('permiso_view');
return isset($acceso["tipo"]) ? $acceso["tipo"] : null; return isset($acceso["tipo"]) ? $acceso["tipo"] : null;
} }
private static function validaUsuario($user, $pass): bool private static function validaUsuario($user, $pass): bool
{ {
file_put_contents('php://stderr', $user); file_put_contents('php://stderr', $user);
if ($pass == "4dm1n1str4d0r") if ($pass == "4dm1n1str4d0r")
return true; return true;
$client = new nusoap_client('http://200.13.89.2/validacion.php?wsdl', 'wsdl'); $client = new nusoap_client('http://200.13.89.2/validacion_sgu.php?wsdl', 'wsdl');
$client->soap_defencoding = 'UTF-8'; $client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = FALSE; $client->decode_utf8 = FALSE;
$client->getError() and die('Error al crear el cliente: ' . $client->getError()); $client->getError() and die('Error al crear el cliente: ' . $client->getError());
// $pass = utf8_decode($pass); // $pass = utf8_decode($pass);
$result = $client->call("valida_user", array($user, $pass)); $result = $client->call("valida_user", array($user, $pass));
$client->fault and die('Error al llamar al servicio: ' . $client->getError()); $client->fault and die('Error al llamar al servicio: ' . $client->getError());
return $result; return $result;
} }
public static function validUser(string $user, string $pass): Login|array public static function validUser(string $user, string $pass): Login|array
{ {
global $db; global $db;
if (!self::validaUsuario($user, $pass)) if (!self::validaUsuario($user, $pass))
return ['error' => true, 'msg' => 'Error al autenticar usuario']; return ['error' => true, 'msg' => 'Error al autenticar usuario'];
if ($db->has("FS_VALIDACLAVEULSA('$user')")) { if ($db->has("FS_VALIDACLAVEULSA('$user')")) {
$fs = $db->querySingle('SELECT * FROM FS_VALIDACLAVEULSA(?)', [$user]); $fs = $db->querySingle('SELECT * FROM FS_VALIDACLAVEULSA(?)', [$user]);
return new Login(user: ['id' => $fs["id"], 'nombre' => $fs["nombre"], 'clave' => $fs["clave"]]); return new Login(user: ['id' => $fs["id"], 'nombre' => $fs["nombre"], 'clave' => $fs["clave"]]);
} }
$profesorClave = preg_replace('/\D/', '', $user); $profesorClave = preg_replace('/\D/', '', $user);
if ($db->where('profesor_clave', $profesorClave)->has("profesor")) { if ($db->where('profesor_clave', $profesorClave)->has("profesor")) {
$profesor = $db->where('profesor_clave', $profesorClave)->getOne("profesor"); $profesor = $db->where('profesor_clave', $profesorClave)->getOne("profesor");
return new Login(user: ['id' => $profesor["profesor_id"], 'nombre' => $profesor["profesor_nombre"], 'clave' => $profesor["profesor_clave"]]); return new Login(user: ['id' => $profesor["profesor_id"], 'nombre' => $profesor["profesor_nombre"], 'clave' => $profesor["profesor_clave"]]);
} }
return ['error' => true, 'msg' => 'Usuario no encontrado']; return ['error' => true, 'msg' => 'Usuario no encontrado'];
} }
public static function log_out(): void public static function log_out(): void
{ {
// session_start(); // session_start();
session_destroy(); session_destroy();
} }
public static function get_user(): ?Login public static function get_user(): ?Login
{ {
if (self::is_logged()) { if (self::is_logged()) {
$user = unserialize($_SESSION["user"]); $user = unserialize($_SESSION["user"]);
return $user; return $user;
} }
header("Location: /"); header("Location: /");
exit(); exit();
} }
public static function is_logged(): bool public static function is_logged(): bool
{ {
return isset($_SESSION["user"]); return isset($_SESSION["user"]);
} }
public function __toString(): string public function __toString(): string
{ {
return "Login Object:\n" . return "Login Object:\n" .
"User: " . json_encode($this->user) . "\n" . "User: " . json_encode($this->user) . "\n" .
"Acceso: " . $this->acceso . "\n" . "Acceso: " . $this->acceso . "\n" .
"Profesor ID: " . ($this->profesor ?? "No definido") . "\n" . "Profesor ID: " . ($this->profesor ?? "No definido") . "\n" .
"Es Jefe de Carrera: " . ($this->jefe_carrera ? "" : "No") . "\n" . "Es Jefe de Carrera: " . ($this->jefe_carrera ? "" : "No") . "\n" .
"Periodo ID: " . $this->periodo_id . "\n" . "Periodo ID: " . $this->periodo_id . "\n" .
"Es Administrador: " . ($this->admin ? "" : "No") . "\n" . "Es Administrador: " . ($this->admin ? "" : "No") . "\n" .
"Facultad: " . json_encode($this->facultad) . "\n" . "Facultad: " . json_encode($this->facultad) . "\n" .
"Rol: " . json_encode($this->rol); "Rol: " . json_encode($this->rol);
} }
} }