Sfoglia il codice sorgente

Adding form and logic for adding a new ssh key

clement 2 anni fa
parent
commit
d2113773b0
3 ha cambiato i file con 74 aggiunte e 1 eliminazioni
  1. 29 1
      src/controllers/ssh.php
  2. 28 0
      src/models/ssh.php
  3. 17 0
      templates/sshhomepage.html.twig

+ 29 - 1
src/controllers/ssh.php

@@ -20,5 +20,33 @@ function form_ssh() {
     $template = get_view("sshhomepage");
 
     echo $template->render();
-    //unset($_SESSION['message']);
+    unset($_SESSION['message']);
+}
+
+function add_ssh_key() {
+    session_start();
+
+    if (!isset($_SESSION['user'])) {
+        header('Location: /');
+        exit();
+    }
+
+    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+        header('Location: /');
+        exit();
+    }
+
+    if (!isset($_POST['new_ssh_key'])) {
+        header('Location: ssh');
+        exit();
+    }
+
+    $res = SshModel\add_ssh_key($_SESSION['user'], $_POST['new_ssh_key']);
+
+    if (!$res) {
+        $_SESSION['message'] = "There was a problem adding the key, contact admins";
+    }
+
+    header('Location: ssh');
+    exit();
 }

+ 28 - 0
src/models/ssh.php

@@ -36,4 +36,32 @@ function get_ssh_keys($id) {
         }
     }
     return false;
+}
+
+function add_ssh_key($id, $new_key) {
+    $ini = parse_ini_file('../includes/config.ini');
+    try {
+        $ldapconn = ldap_connect($ini['hostname'], $ini['port']);
+        ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
+        ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
+    } catch (Exception $e) {
+        die ('ERROR: ' . $e->getMessage());
+    }
+
+    if (!$ldapconn) {
+        return false;
+    }
+
+    $ldap_bind = ldap_bind($ldapconn, $ini['binddn'], $ini["bindpw"]);
+
+    if (!$ldap_bind) {
+        return false;
+    }
+
+    $dn = "uid=" . $id . "," . $ini['basedn'];
+    $entry = array();
+    $entry['sshPublicKey'][0] = $new_key;
+    $res = ldap_mod_add($ldapconn, $dn, $entry);
+
+    return $res;
 }

+ 17 - 0
templates/sshhomepage.html.twig

@@ -21,5 +21,22 @@
                 </article>
             {% endfor %}
         </div>
+        <div class="section">
+            <form method="post" accept-charset="UTF-8" action="addsshkey" class="box">
+                <div class="field">
+                    <label for="new_ssh_key" class="label">New SSH key</label>
+                    <div class="control">
+                        <input id="new_ssh_key" type="text" name="new_ssh_key" class="input is-rounded is-primary">
+                    </div>
+                </div>
+
+                <button class="button is-primary">Add</button>
+            </form>
+        </div>
+        {% if session.message is defined %}
+            <div class="container">
+                <div class="box notification is-warning">{{ session.message }}</div>
+            </div>
+        {% endif %}
     </body>
 </html>