Bladeren bron

Added the possibility to delete ssh keys

clement 2 jaren geleden
bovenliggende
commit
3ee6743341
3 gewijzigde bestanden met toevoegingen van 61 en 5 verwijderingen
  1. 28 0
      src/controllers/ssh.php
  2. 30 4
      src/models/ssh.php
  3. 3 1
      templates/sshhomepage.html.twig

+ 28 - 0
src/controllers/ssh.php

@@ -47,6 +47,34 @@ function add_ssh_key() {
         $_SESSION['message'] = "There was a problem adding the key, contact admins";
     }
 
+    header('Location: ssh');
+    exit();
+}
+
+function del_ssh_key() {
+    session_start();
+
+    if (!isset($_SESSION['user'])) {
+        header('Location: /');
+        exit();
+    }
+
+    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+        header('Location: /');
+        exit();
+    }
+
+    if (!isset($_POST['key_to_delete'])) {
+        header('Location: ssh');
+        exit();
+    }
+
+    $res = SshModel\del_ssh_key($_SESSION['user'], $_POST['key_to_delete']);
+
+    if (!$res) {
+        $_SESSION['message'] = "There was a problem deleting the key, contact admins";
+    }
+
     header('Location: ssh');
     exit();
 }

+ 30 - 4
src/models/ssh.php

@@ -52,16 +52,42 @@ function add_ssh_key($id, $new_key) {
         return false;
     }
 
-    $ldap_bind = ldap_bind($ldapconn, $ini['binddn'], $ini["bindpw"]);
+    $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);
+    $entry['sshPublicKey'] = $new_key;
+    $res = @ldap_mod_add($ldapconn, $dn, $entry);
+
+    return $res;
+}
+
+function del_ssh_key($id, $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["sshPublicKey"] = $key;
+    $res = @ldap_mod_del($ldapconn, $dn, $entry);
 
     return $res;
 }

+ 3 - 1
templates/sshhomepage.html.twig

@@ -17,7 +17,9 @@
                         </div>
                         <div class="message-body">
                             {{ key_value }} </br></br>
-                            <button class="button is-danger">Delete</button>
+                            <form method="post" accept-charset="UTF-8" action="delsshkey">
+                            <button type="submit" name="key_to_delete" value="{{ key_value }}" class="button is-danger">Delete</button>
+                            </form>
                         </div>
                     </article>
                 {% endfor %}