1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/selectFirst.php"; |
4 | require_once __DIR__ . "/../lib/php/insert.php"; |
5 | require_once __DIR__ . "/TABLA_ROL.php"; |
6 | |
7 | class Bd |
8 | { |
9 | |
10 | private static ?PDO $pdo = null; |
11 | |
12 | static function pdo(): PDO |
13 | { |
14 | if (self::$pdo === null) { |
15 | |
16 | self::$pdo = new PDO( |
17 | // cadena de conexión |
18 | "sqlite:srvamuchos.db", |
19 | // usuario |
20 | null, |
21 | // contraseña |
22 | null, |
23 | // Opciones: pdos no persistentes y lanza excepciones. |
24 | [PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
25 | ); |
26 | |
27 | self::$pdo->exec( |
28 | 'CREATE TABLE IF NOT EXISTS USUARIO ( |
29 | USU_ID INTEGER, |
30 | USU_CUE TEXT NOT NULL, |
31 | CONSTRAINT USU_PK |
32 | PRIMARY KEY(USU_ID), |
33 | CONSTRAINT USU_CUE_UNQ |
34 | UNIQUE(USU_CUE), |
35 | CONSTRAINT USU_CUE_NV |
36 | CHECK(LENGTH(USU_CUE) > 0) |
37 | )' |
38 | ); |
39 | self::$pdo->exec( |
40 | 'CREATE TABLE IF NOT EXISTS ROL ( |
41 | ROL_ID TEXT NOT NULL, |
42 | ROL_DESCRIPCION TEXT NOT NULL, |
43 | CONSTRAINT ROL_PK |
44 | PRIMARY KEY(ROL_ID), |
45 | CONSTRAINT ROL_ID_NV |
46 | CHECK(LENGTH(ROL_ID) > 0), |
47 | CONSTRAINT ROL_DESCR_UNQ |
48 | UNIQUE(ROL_DESCRIPCION), |
49 | CONSTRAINT ROL_DESCR_NV |
50 | CHECK(LENGTH(ROL_DESCRIPCION) > 0) |
51 | )' |
52 | ); |
53 | self::$pdo->exec( |
54 | 'CREATE TABLE IF NOT EXISTS USU_ROL ( |
55 | USU_ID INTEGER NOT NULL, |
56 | ROL_ID TEXT NOT NULL, |
57 | CONSTRAINT USU_ROL_PK |
58 | PRIMARY KEY(USU_ID, ROL_ID), |
59 | CONSTRAINT USU_ROL_USU_FK |
60 | FOREIGN KEY (USU_ID) REFERENCES USUARIO(USU_ID), |
61 | CONSTRAINT USU_ROL_ROL_FK |
62 | FOREIGN KEY (ROL_ID) REFERENCES ROL(ROL_ID) |
63 | )' |
64 | ); |
65 | |
66 | if (selectFirst(self::$pdo, ROL, [ROL_ID => "Administrador"]) === false) { |
67 | insert( |
68 | pdo: self::$pdo, |
69 | into: ROL, |
70 | values: [ |
71 | ROL_ID => "Administrador", |
72 | ROL_DESCRIPCION => "Administra el sistema." |
73 | ] |
74 | ); |
75 | } |
76 | |
77 | if (selectFirst(self::$pdo, ROL, [ROL_ID => "Cliente"]) === false) { |
78 | insert( |
79 | pdo: self::$pdo, |
80 | into: ROL, |
81 | values: [ |
82 | ROL_ID => "Cliente", |
83 | ROL_DESCRIPCION => "Realiza compras." |
84 | ] |
85 | ); |
86 | } |
87 | } |
88 | |
89 | return self::$pdo; |
90 | } |
91 | } |
92 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/select.php"; |
5 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
6 | require_once __DIR__ . "/Bd.php"; |
7 | require_once __DIR__ . "/TABLA_ROL.php"; |
8 | |
9 | ejecutaServicio(function () { |
10 | |
11 | $lista = select(pdo: Bd::pdo(), from: ROL, orderBy: ROL_ID); |
12 | |
13 | $render = ""; |
14 | foreach ($lista as $modelo) { |
15 | $id = htmlentities($modelo[ROL_ID]); |
16 | $descripcion = htmlentities($modelo[ROL_DESCRIPCION]); |
17 | $render .= |
18 | "<p> |
19 | <label style='display: flex'> |
20 | <input type='checkbox' name='rolIds[]' value='$id'> |
21 | <span> |
22 | <strong>$id</strong> |
23 | <br>$descripcion |
24 | </span> |
25 | </label> |
26 | </p>"; |
27 | } |
28 | |
29 | devuelveJson(["roles" => ["innerHTML" => $render]]); |
30 | }); |
31 |
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 |
1 | <?php |
2 | |
3 | const USU_ROL = "USU_ROL"; |
4 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/recuperaTexto.php"; |
5 | require_once __DIR__ . "/../lib/php/recuperaArray.php"; |
6 | require_once __DIR__ . "/../lib/php/validaCue.php"; |
7 | require_once __DIR__ . "/../lib/php/insert.php"; |
8 | require_once __DIR__ . "/../lib/php/insertBridges.php"; |
9 | require_once __DIR__ . "/../lib/php/devuelveCreated.php"; |
10 | require_once __DIR__ . "/Bd.php"; |
11 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
12 | require_once __DIR__ . "/TABLA_ROL.php"; |
13 | require_once __DIR__ . "/TABLA_USU_ROL.php"; |
14 | |
15 | ejecutaServicio(function () { |
16 | |
17 | $cue = recuperaTexto("cue"); |
18 | $rolIds = recuperaArray("rolIds"); |
19 | |
20 | $cue = validaCue($cue); |
21 | |
22 | $pdo = Bd::pdo(); |
23 | $pdo->beginTransaction(); |
24 | |
25 | insert(pdo: $pdo, into: USUARIO, values: [USU_CUE => $cue]); |
26 | $usuId = $pdo->lastInsertId(); |
27 | insertBridges( |
28 | pdo: $pdo, |
29 | into: USU_ROL, |
30 | valuesDePadre: [USU_ID => $usuId], |
31 | valueDeHijos: [ROL_ID => $rolIds] |
32 | ); |
33 | |
34 | $pdo->commit(); |
35 | |
36 | $encodeUsuId = urlencode($usuId); |
37 | devuelveCreated("/srv/usuario.php?id=$encodeUsuId", [ |
38 | "id" => ["value" => $usuId], |
39 | "cue" => ["value" => $cue], |
40 | "rolIds" => ["value" => $rolIds], |
41 | ]); |
42 | }); |
43 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/recuperaIdEntero.php"; |
5 | require_once __DIR__ . "/../lib/php/delete.php"; |
6 | require_once __DIR__ . "/../lib/php/devuelveNoContent.php"; |
7 | require_once __DIR__ . "/Bd.php"; |
8 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
9 | require_once __DIR__ . "/TABLA_USU_ROL.php"; |
10 | |
11 | ejecutaServicio(function () { |
12 | |
13 | $usuId = recuperaIdEntero("id"); |
14 | |
15 | $pdo = Bd::pdo(); |
16 | $pdo->beginTransaction(); |
17 | |
18 | delete(pdo: $pdo, from: USU_ROL, where: [USU_ID => $usuId]); |
19 | delete(pdo: $pdo, from: USUARIO, where: [USU_ID => $usuId]); |
20 | |
21 | $pdo->commit(); |
22 | |
23 | devuelveNoContent(); |
24 | }); |
25 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/recuperaIdEntero.php"; |
5 | require_once __DIR__ . "/../lib/php/recuperaTexto.php"; |
6 | require_once __DIR__ . "/../lib/php/recuperaArray.php"; |
7 | require_once __DIR__ . "/../lib/php/validaCue.php"; |
8 | require_once __DIR__ . "/../lib/php/update.php"; |
9 | require_once __DIR__ . "/../lib/php/delete.php"; |
10 | require_once __DIR__ . "/../lib/php/insertBridges.php"; |
11 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
12 | require_once __DIR__ . "/Bd.php"; |
13 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
14 | require_once __DIR__ . "/TABLA_ROL.php"; |
15 | require_once __DIR__ . "/TABLA_USU_ROL.php"; |
16 | |
17 | ejecutaServicio(function () { |
18 | |
19 | $usuId = recuperaIdEntero("id"); |
20 | $cue = recuperaTexto("cue"); |
21 | $rolIds = recuperaArray("rolIds"); |
22 | |
23 | $cue = validaCue($cue); |
24 | |
25 | $pdo = Bd::pdo(); |
26 | $pdo->beginTransaction(); |
27 | |
28 | update( |
29 | pdo: $pdo, |
30 | table: USUARIO, |
31 | set: [USU_CUE => $cue], |
32 | where: [USU_ID => $usuId] |
33 | ); |
34 | delete(pdo: $pdo, from: USU_ROL, where: [USU_ID => $usuId]); |
35 | insertBridges( |
36 | pdo: $pdo, |
37 | into: USU_ROL, |
38 | valuesDePadre: [USU_ID => $usuId], |
39 | valueDeHijos: [ROL_ID => $rolIds] |
40 | ); |
41 | |
42 | $pdo->commit(); |
43 | |
44 | devuelveJson([ |
45 | "id" => ["value" => $usuId], |
46 | "cue" => ["value" => $cue], |
47 | "rolIds" => ["value" => $rolIds], |
48 | ]); |
49 | }); |
50 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/NOT_FOUND.php"; |
4 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
5 | require_once __DIR__ . "/../lib/php/recuperaIdEntero.php"; |
6 | require_once __DIR__ . "/../lib/php/selectFirst.php"; |
7 | require_once __DIR__ . "/../lib/php/fetchAll.php"; |
8 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
9 | require_once __DIR__ . "/../lib/php/ProblemDetails.php"; |
10 | require_once __DIR__ . "/Bd.php"; |
11 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
12 | |
13 | ejecutaServicio(function () { |
14 | |
15 | $usuId = recuperaIdEntero("id"); |
16 | |
17 | $pdo = Bd::pdo(); |
18 | |
19 | $modelo = selectFirst(pdo: $pdo, from: USUARIO, where: [USU_ID => $usuId]); |
20 | |
21 | if ($modelo === false) { |
22 | $htmlId = htmlentities($usuId); |
23 | throw new ProblemDetails( |
24 | title: "Usuario no encontrado.", |
25 | status: NOT_FOUND, |
26 | type: "/error/usuarionoencontrado.html", |
27 | detail: "No se encontró ningún usuario con el id $htmlId.", |
28 | ); |
29 | } else { |
30 | |
31 | $rolIds = fetchAll( |
32 | $pdo->query( |
33 | "SELECT ROL_ID |
34 | FROM USU_ROL |
35 | WHERE USU_ID = :USU_ID |
36 | ORDER BY USU_ID" |
37 | ), |
38 | [":USU_ID" => $usuId], |
39 | PDO::FETCH_COLUMN |
40 | ); |
41 | |
42 | devuelveJson([ |
43 | "id" => ["value" => $usuId], |
44 | "cue" => ["value" => $modelo[USU_CUE]], |
45 | "rolIds[]" => $rolIds |
46 | ]); |
47 | } |
48 | }); |
49 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/ejecutaServicio.php"; |
4 | require_once __DIR__ . "/../lib/php/fetchAll.php"; |
5 | require_once __DIR__ . "/../lib/php/devuelveJson.php"; |
6 | require_once __DIR__ . "/Bd.php"; |
7 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
8 | |
9 | ejecutaServicio(function () { |
10 | |
11 | $lista = fetchAll(Bd::pdo()->query( |
12 | "SELECT |
13 | U.USU_ID, |
14 | U.USU_CUE, |
15 | GROUP_CONCAT(UR.ROL_ID, ', ') AS roles |
16 | FROM USUARIO U |
17 | LEFT JOIN USU_ROL UR |
18 | ON U.USU_ID = UR.USU_ID |
19 | GROUP BY U.USU_CUE |
20 | ORDER BY U.USU_CUE" |
21 | )); |
22 | |
23 | $render = ""; |
24 | foreach ($lista as $modelo) { |
25 | $encodeUsuId = urlencode($modelo[USU_ID]); |
26 | $usuId = htmlentities($encodeUsuId); |
27 | $usuCue = htmlentities($modelo[USU_CUE]); |
28 | $roles = $modelo["roles"] === null || $modelo["roles"] === "" |
29 | ? "<em>-- Sin roles --</em>" |
30 | : htmlentities($modelo["roles"]); |
31 | $render .= |
32 | "<dt><a href='modifica.html?id=$usuId'>$usuCue</a></dt> |
33 | <dd><a href='modifica.html?id=$usuId'>$roles</a></dd>"; |
34 | } |
35 | |
36 | devuelveJson(["lista" => ["innerHTML" => $render]]); |
37 | }); |
38 |