1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/selectFirst.php"; |
4 | require_once __DIR__ . "/../lib/php/insert.php"; |
5 | require_once __DIR__ . "/../lib/php/insertBridges.php"; |
6 | require_once __DIR__ . "/../lib/php/insert.php"; |
7 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
8 | require_once __DIR__ . "/TABLA_ROL.php"; |
9 | require_once __DIR__ . "/TABLA_USU_ROL.php"; |
10 | require_once __DIR__ . "/ROL_ID_CLIENTE.php"; |
11 | require_once __DIR__ . "/ROL_ID_ADMINISTRADOR.php"; |
12 | |
13 | class Bd |
14 | { |
15 | |
16 | private static ?PDO $pdo = null; |
17 | |
18 | static function pdo(): PDO |
19 | { |
20 | if (self::$pdo === null) { |
21 | |
22 | self::$pdo = new PDO( |
23 | // cadena de conexión |
24 | "sqlite:srvaut.db", |
25 | // usuario |
26 | null, |
27 | // contraseña |
28 | null, |
29 | // Opciones: pdos no persistentes y lanza excepciones. |
30 | [PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
31 | ); |
32 | |
33 | self::$pdo->exec( |
34 | 'CREATE TABLE IF NOT EXISTS USUARIO ( |
35 | USU_ID INTEGER, |
36 | USU_CUE TEXT NOT NULL, |
37 | USU_MATCH TEXT NOT NULL, |
38 | CONSTRAINT USU_PK |
39 | PRIMARY KEY(USU_ID), |
40 | CONSTRAINT USU_CUE_UNQ |
41 | UNIQUE(USU_CUE), |
42 | CONSTRAINT USU_CUE_NV |
43 | CHECK(LENGTH(USU_CUE) > 0) |
44 | )' |
45 | ); |
46 | self::$pdo->exec( |
47 | 'CREATE TABLE IF NOT EXISTS ROL ( |
48 | ROL_ID TEXT NOT NULL, |
49 | ROL_DESCRIPCION TEXT NOT NULL, |
50 | CONSTRAINT ROL_PK |
51 | PRIMARY KEY(ROL_ID), |
52 | CONSTRAINT ROL_ID_NV |
53 | CHECK(LENGTH(ROL_ID) > 0), |
54 | CONSTRAINT ROL_DESCR_UNQ |
55 | UNIQUE(ROL_DESCRIPCION), |
56 | CONSTRAINT ROL_DESCR_NV |
57 | CHECK(LENGTH(ROL_DESCRIPCION) > 0) |
58 | )' |
59 | ); |
60 | self::$pdo->exec( |
61 | 'CREATE TABLE IF NOT EXISTS USU_ROL ( |
62 | USU_ID INTEGER NOT NULL, |
63 | ROL_ID TEXT NOT NULL, |
64 | CONSTRAINT USU_ROL_PK |
65 | PRIMARY KEY(USU_ID, ROL_ID), |
66 | CONSTRAINT USU_ROL_USU_FK |
67 | FOREIGN KEY (USU_ID) REFERENCES USUARIO(USU_ID), |
68 | CONSTRAINT USU_ROL_ROL_FK |
69 | FOREIGN KEY (ROL_ID) REFERENCES ROL(ROL_ID) |
70 | )' |
71 | ); |
72 | |
73 | if (selectFirst( |
74 | pdo: self::$pdo, |
75 | from: ROL, |
76 | where: [ROL_ID => ROL_ID_ADMINISTRADOR] |
77 | ) === false) { |
78 | insert( |
79 | pdo: self::$pdo, |
80 | into: ROL, |
81 | values: [ |
82 | ROL_ID => ROL_ID_ADMINISTRADOR, |
83 | ROL_DESCRIPCION => "Administra el sistema." |
84 | ] |
85 | ); |
86 | } |
87 | |
88 | if (selectFirst(self::$pdo, ROL, [ROL_ID => ROL_ID_CLIENTE]) === false) { |
89 | insert( |
90 | pdo: self::$pdo, |
91 | into: ROL, |
92 | values: [ |
93 | ROL_ID => ROL_ID_CLIENTE, |
94 | ROL_DESCRIPCION => "Realiza compras." |
95 | ] |
96 | ); |
97 | } |
98 | } |
99 | |
100 | if (selectFirst(self::$pdo, USUARIO, [USU_CUE => "pepito"]) === false) { |
101 | insert( |
102 | pdo: self::$pdo, |
103 | into: USUARIO, |
104 | values: [ |
105 | USU_CUE => "pepito", |
106 | USU_MATCH => password_hash("cuentos", PASSWORD_DEFAULT) |
107 | ] |
108 | ); |
109 | $usuId = self::$pdo->lastInsertId(); |
110 | insertBridges( |
111 | pdo: self::$pdo, |
112 | into: USU_ROL, |
113 | valuesDePadre: [USU_ID => $usuId], |
114 | valueDeHijos: [ROL_ID => [ROL_ID_CLIENTE]] |
115 | ); |
116 | } |
117 | |
118 | if (selectFirst(self::$pdo, USUARIO, [USU_CUE => "susana"]) === false) { |
119 | insert( |
120 | pdo: self::$pdo, |
121 | into: USUARIO, |
122 | values: [ |
123 | USU_CUE => "susana", |
124 | USU_MATCH => password_hash("alegria", PASSWORD_DEFAULT) |
125 | ] |
126 | ); |
127 | $usuId = self::$pdo->lastInsertId(); |
128 | insertBridges( |
129 | pdo: self::$pdo, |
130 | into: USU_ROL, |
131 | valuesDePadre: [USU_ID => $usuId], |
132 | valueDeHijos: [ROL_ID => [ROL_ID_ADMINISTRADOR]] |
133 | ); |
134 | } |
135 | |
136 | if (selectFirst(self::$pdo, USUARIO, [USU_CUE => "bebe"]) === false) { |
137 | insert( |
138 | pdo: self::$pdo, |
139 | into: USUARIO, |
140 | values: [ |
141 | USU_CUE => "bebe", |
142 | USU_MATCH => password_hash("saurio", PASSWORD_DEFAULT) |
143 | ] |
144 | ); |
145 | $usuId = self::$pdo->lastInsertId(); |
146 | insertBridges( |
147 | pdo: self::$pdo, |
148 | into: USU_ROL, |
149 | valuesDePadre: [USU_ID => $usuId], |
150 | valueDeHijos: [ROL_ID => [ROL_ID_ADMINISTRADOR, ROL_ID_CLIENTE]] |
151 | ); |
152 | } |
153 | |
154 | return self::$pdo; |
155 | } |
156 | } |
157 |
1 | <?php |
2 | |
3 | const CUE = "cue"; |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/BAD_REQUEST.php"; |
4 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
5 | require_once __DIR__ . "/../lib/php/recuperaTexto.php"; |
6 | require_once __DIR__ . "/../lib/php/validaCue.php"; |
7 | require_once __DIR__ . "/../lib/php/ProblemDetails.php"; |
8 | require_once __DIR__ . "/../lib/php/selectFirst.php"; |
9 | require_once __DIR__ . "/../lib/php/fetchAll.php"; |
10 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
11 | require_once __DIR__ . "/CUE.php"; |
12 | require_once __DIR__ . "/ROL_IDS.php"; |
13 | require_once __DIR__ . "/Bd.php"; |
14 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
15 | require_once __DIR__ . "/protege.php"; |
16 | |
17 | ejecutaServicio(function () { |
18 | |
19 | $sesion = protege(); |
20 | |
21 | if ($sesion->cue !== "") |
22 | throw new ProblemDetails( |
23 | status: NO_AUTORIZADO, |
24 | type: "/error/sesioniniciada.html", |
25 | title: "Sesión iniciada.", |
26 | detail: "La sesión ya está iniciada.", |
27 | ); |
28 | |
29 | $cue = recuperaTexto("cue"); |
30 | $match = recuperaTexto("match"); |
31 | |
32 | $cue = validaCue($cue); |
33 | |
34 | if ($match === false) |
35 | throw new ProblemDetails( |
36 | status: BAD_REQUEST, |
37 | title: "Falta el match.", |
38 | type: "/error/faltamatch.html", |
39 | detail: "La solicitud no tiene el valor de match.", |
40 | ); |
41 | |
42 | if ($match === "") |
43 | throw new ProblemDetails( |
44 | status: BAD_REQUEST, |
45 | title: "Match en blanco.", |
46 | type: "/error/matchenblanco.html", |
47 | detail: "Pon texto en el campo match.", |
48 | ); |
49 | |
50 | $pdo = Bd::pdo(); |
51 | |
52 | $usuario = |
53 | selectFirst(pdo: $pdo, from: USUARIO, where: [USU_CUE => $cue]); |
54 | |
55 | if ($usuario === false || !password_verify($match, $usuario[USU_MATCH])) |
56 | throw new ProblemDetails( |
57 | status: BAD_REQUEST, |
58 | type: "/error/datosincorrectos.html", |
59 | title: "Datos incorrectos.", |
60 | detail: "El cue y/o el match proporcionados son incorrectos.", |
61 | ); |
62 | |
63 | $rolIds = fetchAll( |
64 | $pdo->query( |
65 | "SELECT ROL_ID |
66 | FROM USU_ROL |
67 | WHERE USU_ID = :USU_ID |
68 | ORDER BY USU_ID" |
69 | ), |
70 | [":USU_ID" => $usuario[USU_ID]], |
71 | PDO::FETCH_COLUMN |
72 | ); |
73 | |
74 | $_SESSION[CUE] = $cue; |
75 | $_SESSION[ROL_IDS] = $rolIds; |
76 | devuelveJson([ |
77 | CUE => $cue, |
78 | ROL_IDS => $rolIds |
79 | ]); |
80 | }); |
81 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
5 | require_once __DIR__ . "/CUE.php"; |
6 | require_once __DIR__ . "/ROL_IDS.php"; |
7 | |
8 | ejecutaServicio(function () { |
9 | |
10 | session_start(); |
11 | |
12 | if (isset($_SESSION[CUE])) { |
13 | unset($_SESSION[CUE]); |
14 | } |
15 | if (isset($_SESSION[ROL_IDS])) { |
16 | unset($_SESSION[ROL_IDS]); |
17 | } |
18 | |
19 | session_destroy(); |
20 | |
21 | devuelveJson([]); |
22 | }); |
23 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ProblemDetails.php"; |
4 | require_once __DIR__ . "/CUE.php"; |
5 | require_once __DIR__ . "/ROL_IDS.php"; |
6 | require_once __DIR__ . "/ROL_ID_CLIENTE.php"; |
7 | require_once __DIR__ . "/Sesion.php"; |
8 | |
9 | const NO_AUTORIZADO = 401; |
10 | |
11 | function protege(?array $rolIdsPermitidos = null) |
12 | { |
13 | |
14 | session_start(); |
15 | |
16 | $cue = isset($_SESSION[CUE]) ? $_SESSION[CUE] : ""; |
17 | $rolIds = isset($_SESSION[ROL_IDS]) ? $_SESSION[ROL_IDS] : []; |
18 | $sesion = new Sesion($cue, $rolIds); |
19 | |
20 | if ($rolIdsPermitidos === null) { |
21 | |
22 | return $sesion; |
23 | } else { |
24 | |
25 | foreach ($rolIdsPermitidos as $rolId) { |
26 | if (array_search($rolId, $rolIds, true) !== false) { |
27 | return $sesion; |
28 | } |
29 | } |
30 | |
31 | throw new ProblemDetails( |
32 | status: NO_AUTORIZADO, |
33 | type: "/error/noautorizado.html", |
34 | title: "No autorizado.", |
35 | detail: "No está autorizado para usar este recurso.", |
36 | ); |
37 | } |
38 | } |
39 |
1 | <?php |
2 | |
3 | const ROL_IDS = "rolIds"; |
1 | <?php |
2 | |
3 | const ROL_ID_ADMINISTRADOR = "Administrador"; |
4 |
1 | <?php |
2 | |
3 | const ROL_ID_CLIENTE = "Cliente"; |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
5 | require_once __DIR__ . "/ROL_ID_CLIENTE.php"; |
6 | require_once __DIR__ . "/protege.php"; |
7 | |
8 | ejecutaServicio(function () { |
9 | $sesion = protege([ROL_ID_CLIENTE]); |
10 | devuelveJson("Hola " . $sesion->cue); |
11 | }); |
12 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
5 | require_once __DIR__ . "/protege.php"; |
6 | |
7 | ejecutaServicio(function () { |
8 | devuelveJson(protege()); |
9 | }); |
10 |
1 | <?php |
2 | |
3 | class Sesion |
4 | { |
5 | |
6 | public string $cue; |
7 | public array $rolIds; |
8 | |
9 | public function __construct(string $cue, array $rolIds) |
10 | { |
11 | $this->cue = $cue; |
12 | $this->rolIds = $rolIds; |
13 | } |
14 | } |
15 |
1 | <?php |
2 | |
3 | const ROL = "ROL"; |
4 | const ROL_ID = "ROL_ID"; |
5 | const ROL_DESCRIPCION = "ROL_DESCRIPCION"; |
6 |
1 | <?php |
2 | |
3 | const USUARIO = "USUARIO"; |
4 | const USU_ID = "USU_ID"; |
5 | const USU_CUE = "USU_CUE"; |
6 | const USU_MATCH = "USU_MATCH"; |
7 |
1 | <?php |
2 | |
3 | const USU_ROL = "USU_ROL"; |
4 |