Skip to content

Commit 5a35e35

Browse files
committed
Modified confirmation code. Added basic session handler, and added user data returns
1 parent 8eca766 commit 5a35e35

File tree

4 files changed

+85
-18
lines changed

4 files changed

+85
-18
lines changed

Diff for: controladores/LoginController.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public static function login(){
2828
$socio = Socio::createLogin($correo,$contrasena);
2929

3030
if(self::dataMatches($socio)){
31-
echo json_encode(array("success" => true, "m"=> "Login exitoso"));
31+
32+
$membresia = SocioController::getMembresia($correo);
33+
$_SESSION["id"] = $membresia;
34+
$data = SocioController::getSocio($membresia);
35+
echo json_encode(array("success" => true, "m"=> "Login exitoso", "d" => $data[0]));
3236
return;
3337
}
3438

Diff for: controladores/SocioController.php

+68-15
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,35 @@ public static function sendMail($body, $subject, $socio)
5959

6060
public static function findOne($membresia)
6161
{
62-
self::listar($membresia);
62+
$data = self::getSocio($membresia);
63+
64+
if (!$data) {
65+
echo json_encode(array("success" => false, "m"=> "No se encontraron registros"));
66+
return;
67+
}
68+
69+
echo json_encode(array("success" => true, "d"=> $data));
6370
}
6471

6572
public static function findAll()
6673
{
67-
self::listar(false);
74+
$data = self::getSocio(false);
75+
76+
if (!$data) {
77+
echo json_encode(array("success" => false, "m"=> "No se encontraron registros"));
78+
return;
79+
}
80+
81+
echo json_encode(array("success" => true, "d"=> $data));
6882
}
6983

70-
private static function listar($membresia)
84+
public static function getSocio($membresia)
7185
{
7286

7387
$conexion = Conexion::conectar();
7488

75-
$query = "SELECT CONCAT(Nombre, ' ', ApellidoP) AS Nombre, Email AS Correo FROM personas AS p
76-
JOIN usuarios AS usr ON usr.ID_Persona = p.id ";
89+
$query = "SELECT CONCAT(Nombre, ' ', ApellidoP) AS Nombre, Email AS Correo , p.ID AS membresia, Activo, tipo_membresia AS tipoMembresia FROM personas AS p
90+
JOIN usuarios AS usr ON usr.ID_Persona = p.ID ";
7791

7892
$stmt = $conexion->prepare($query);
7993

@@ -87,17 +101,46 @@ private static function listar($membresia)
87101

88102
$result = $stmt->get_result();
89103

90-
if ($result->num_rows==0) {
91-
echo json_encode(array("success" => false, "m"=> "No se encontraron registros"));
92-
return;
93-
}
104+
if ($result->num_rows==0)
105+
return array();
94106

95107
while ($row = $result->fetch_assoc()) {
108+
$membresia = $row["membresia"];
109+
$row["vinculados"] = self::getVinculados($membresia);
110+
96111
$rows[] = $row;
97112
}
98113

99-
echo json_encode(array("success" => true, "d"=> $rows));
100-
return;
114+
115+
return $rows;
116+
}
117+
118+
private static function getVinculados($membresia){
119+
120+
$conexion = Conexion::conectar();
121+
122+
123+
$query = "SELECT p.Nombre, vp.parentesco FROM personas AS p
124+
JOIN vinculacion_persona AS vp ON vp.idVinculado= p.id
125+
WHERE vp.idPersona = ?";
126+
127+
$stmt = $conexion->prepare($query);
128+
$stmt->bind_param('i', $membresia);
129+
$stmt->execute();
130+
131+
132+
$result = $stmt->get_result();
133+
$vinculados = array();
134+
135+
while($row = $result->fetch_assoc()){
136+
137+
array_push($vinculados,$row);
138+
139+
}
140+
141+
return $vinculados;
142+
143+
101144
}
102145

103146

@@ -136,11 +179,15 @@ public static function delete($membresia)
136179
public static function update($codigo)
137180
{
138181

139-
if (!self::codigoValido($codigo)) {
182+
$membresia = self::codigoValido($codigo);
183+
184+
if (!$membresia) {
140185
echo json_encode(array("success" => false, "m"=> "Código incorrecto"));
141186
return;
142187
}
143188

189+
190+
144191
$query = "UPDATE usuarios SET activado = 1 WHERE codigo = ? ";
145192
$conexion = Conexion::conectar();
146193

@@ -149,7 +196,8 @@ public static function update($codigo)
149196
$stmt->execute();
150197

151198
if ($conexion->affected_rows==1) {
152-
echo json_encode(array("success" => true, "m"=> "Código confirmado"));
199+
$data = self::getSocio($membresia);
200+
echo json_encode(array("success" => true, "m"=> "Código confirmado", "d" => $data[0]));
153201
return;
154202
}
155203

@@ -182,7 +230,7 @@ public static function getName($membresia)
182230
*/
183231
private static function codigoValido($codigo)
184232
{
185-
$query = "SELECT codigo FROM usuarios where codigo = ?";
233+
$query = "SELECT codigo, ID_Persona FROM usuarios where codigo = ?";
186234
$conexion = Conexion::conectar();
187235

188236
$stmt = $conexion->prepare($query);
@@ -191,7 +239,12 @@ private static function codigoValido($codigo)
191239

192240
$result = $stmt->get_result();
193241

194-
return ( $result->num_rows > 0 );
242+
if ($result->num_rows > 0){
243+
$membresia = $result->fetch_assoc()["ID_Persona"];
244+
return $membresia;
245+
}
246+
247+
return false;
195248
}
196249

197250

Diff for: index.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
header("Content-Type: application/json; charset=utf-8");
1010
include("routes/SociosRouter.php");
1111
include("routes/LoginRouter.php");
12+
13+
session_start();
1214

1315
$base_url = getCurrentUri();
1416
$routes = array();
@@ -30,7 +32,11 @@
3032
//Sends each requests accordingly to its router
3133
switch($routes[1]){
3234
case "socio":
33-
SociosRouter::enrutar($request,$routes);
35+
if(isset($_SESSION["id"]) || $request == "POST"){
36+
SociosRouter::enrutar($request,$routes);
37+
}else{
38+
sendSessionMessage();
39+
}
3440
break;
3541
case "login":
3642
LoginRouter::enrutar($request,$routes);
@@ -44,6 +50,10 @@ function sendErrorMessage(){
4450
echo json_encode(array("success" => false, "m" => "Petición incorrecta"));
4551
}
4652

53+
function sendSessionMessage(){
54+
echo json_encode(array("success" => false, "m" => "No se ha inciado sesión"));
55+
}
56+
4757
/**
4858
* Returns the adress of the current request who called the script.
4959
*

Diff for: modelos/Socio.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private function setUserData($nombre,$membresia,$correo,$contrasena){
4040
$this->nombre = $nombre;
4141
$this->correo = $correo;
4242
$this->contrasena = password_hash($contrasena,PASSWORD_BCRYPT);
43-
$this->key = md5(microtime().rand());
43+
$this->key = rand(1234,9999);
4444
$this->activado = 0;
4545
}
4646

0 commit comments

Comments
 (0)