Ver código fonte

Add new files and update host index page

Alejandro Rosales 1 ano atrás
pai
commit
e8acdcea6c
4 arquivos alterados com 62 adições e 22 exclusões
  1. 0 0
      action/conectar_moodle.php
  2. 8 0
      action/login.php
  3. 38 0
      action/new_host.php
  4. 16 22
      pages/host/index.php

+ 0 - 0
action/conectar_moodle.php


+ 8 - 0
action/login.php

@@ -0,0 +1,8 @@
+<?php
+require_once "{$_SERVER['DOCUMENT_ROOT']}/dependencies.php";
+$token = $db->querySingle("SELECT sign() as token")['token'];
+?>
+
+<script>
+    sessionStorage.setItem('token', JSON.stringify('<?= $token ?>'))
+</script>

+ 38 - 0
action/new_host.php

@@ -0,0 +1,38 @@
+<?php
+require_once "{$_SERVER['DOCUMENT_ROOT']}/dependencies.php";
+
+use Respect\Validation\Validator as v;
+
+// method must be POST
+methods(['POST' => v::keySet(
+    v::key('etiqueta', v::stringType()->notEmpty()),
+    v::key('host', v::stringType()->notEmpty()),
+    v::key('puerto', v::intType()->notEmpty()),
+    v::key('usuario', v::stringType()->notEmpty()),
+    v::key('base_datos', v::stringType()->notEmpty()),
+    v::key('password', v::stringType()->notEmpty()),
+    v::key('periodos', v::arrayType()->notEmpty()->each(v::intType()))
+)]);
+
+try {
+    $db->query('BEGIN');
+    $params = array(
+        'etiqueta' => $_POST['etiqueta'],
+        'host' => $_POST['host'],
+        'puerto' => $_POST['puerto'],
+        'postgres_user' => $_POST['usuario'],
+        'postgres_dbname' => $_POST['base_datos'],
+        'postgres_password' => $_POST['password'],
+        'periodos_gema' => '{' . implode(',', $_POST['periodos']) . '}',
+    );
+    $db->query("INSERT INTO moodle_host (etiqueta, host, puerto, postgres_user, postgres_dbname, postgres_password, periodos_gema) VALUES (:etiqueta, :host, :puerto, :postgres_user, :postgres_dbname, PGP_SYM_ENCRYPT(:postgres_password, '{$_ENV['KEY_ENCRYPT']}'), :periodos_gema)", $params);
+
+    $db->query('COMMIT');
+} catch (\PDOException $th) {
+    $db->query('ROLLBACK');
+    http_response_code(500);
+    echo json_encode(['error' => $th->getMessage()]);
+    exit();
+}
+
+returnResponse(message: "Host {$_POST['etiqueta']} agregado correctamente");

+ 16 - 22
pages/host/index.php

@@ -2,15 +2,20 @@
     <h1>Conectar un HOST</h1>
     <form action="action/conectar_moodle.php" method="post" v-scope="{host: null}">
         <div class="grid">
-            <?php
-            if ($db->count('moodle_host') > 0) : ?>
+            <div v-if="hosts.length > 0">
                 <label for="moodle-host">
                     Moodle host
                     <input list="moodle-hosts" name="moodle-host" placeholder="Moodle host" required v-model="host">
                 </label>
-            <?php else : ?>
+                <datalist id="moodle-hosts" v-for="host in hosts">
+                    <option :value="host.host">
+                        {{ host.etiqueta }}
+                    </option>
+                </datalist>
+            </div>
+            <div v-else>
                 <p>No hay hosts registrados</p>
-            <?php endif ?>
+            </div>
             <label for="agregar-host">
                 Agregar host
                 <button id="agregar-host" type="button" @click="page = 'new_host'">
@@ -18,13 +23,6 @@
                 </button>
             </label>
         </div>
-        <?php if ($db->count('moodle_host') > 0) : ?>
-            <datalist id="moodle-hosts" v-for="host in hosts">
-                <option :value="host.host">
-                    {{ host.etiqueta }}
-                </option>
-            </datalist> 
-        <?php endif ?>
         <div class="grid">
             <button type="button" :disabled="!host" @click="editHost(host)">Editar <i class="fas fa-edit"></i></button>
             <button type="submit" :disabled="!host">Conectar <i class="fas fa-database"></i></button>
@@ -32,10 +30,6 @@
     </form>
 </main>
 
-<div v-else-if="page === 'new_host'">
-    <?php require 'new_host.php' ?>
-</div>
-
 <div v-else>
     <?php require 'edit_host.php' ?>
 </div>
@@ -44,23 +38,23 @@
 
 <script>
     PetiteVue.createApp({
+        store,
         // state
-        loading: false,
         page: 'host',
         // data
         hosts: [],
         current_host: null,
 
         async editHost(host) {
-            this.loading = true
-            const hosts = await fetch(`http://www.localhost:3000/moodle_host?etiqueta=eq.${host}`, {
+            store.loading = true
+            const hosts = await store.fetch(`http://www.localhost:3000/moodle_host?etiqueta=eq.${host}`, {
                 headers: {
                     'Prefer': 'plurality=singular'
                 }
             })
 
             if (!hosts.ok) {
-                this.loading = false
+                store.loading = false
                 alert('Error al obtener el host: ' + hosts.status)
                 return
             }
@@ -73,10 +67,10 @@
         },
 
         async mounted() {
-            this.loading = true
-            const hosts = await fetch('http://www.localhost:3000/moodle_host')
+            store.loading = true
+            const hosts = await store.fetch('http://www.localhost:3000/moodle_host')
             const data = await hosts.json()
-            this.loading = false
+            store.loading = false
 
             this.hosts = data
         }