فهرست منبع

Added: possibility to delete ssh keys + simplifications

clement 1 سال پیش
والد
کامیت
12ba3ec401
2فایلهای تغییر یافته به همراه58 افزوده شده و 12 حذف شده
  1. 39 7
      src/handler.rs
  2. 19 5
      src/ldap/lib.rs

+ 39 - 7
src/handler.rs

@@ -20,10 +20,15 @@ pub struct FormChangePasswd {
 }
 
 #[derive(Deserialize)]
-pub struct FormSSHKey {
+pub struct FormAddSSHKey {
     new_ssh_key: String,
 }
 
+#[derive(Deserialize)]
+pub struct FormDelSSHKey {
+    key_to_delete: String,
+}
+
 async fn get_template(template_name: String, session: Session) -> String {
     let error_message: Option<String> = session.get("error_message")
     .unwrap_or(None);
@@ -206,7 +211,7 @@ pub async fn form_ssh(ldap_wrapper: web::Data<LdapWrapper>, session: Session) ->
     .body(body)
 }
 
-pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormSSHKey>,session: Session) -> impl Responder {
+pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormAddSSHKey>,session: Session) -> impl Responder {
     if !validate_session(&session) {
         return HttpResponse::Ok()
         .status(StatusCode::FOUND)
@@ -224,7 +229,7 @@ pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<F
 
     let uid = session.get("user_id").unwrap().unwrap();
 
-    match ldap_wrapper.add_ssh_key(uid, form.new_ssh_key.clone()).await {
+    match ldap_wrapper.manage_ssh_key(uid, form.new_ssh_key.clone(), ldap::ManageSSHOps::ADD).await {
         Ok(_) => return HttpResponse::Ok()
                 .status(StatusCode::FOUND)
                 .append_header((header::LOCATION, "/ssh"))
@@ -239,10 +244,37 @@ pub async fn add_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<F
     }
 }
 
-pub async fn del_ssh_key(session: Session) -> impl Responder {
-    let body = get_template("sshhomepage.html".to_string(), session).await;
-    HttpResponse::Ok().content_type("text/html")
-    .body(body)
+pub async fn del_ssh_key(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormDelSSHKey>,session: Session) -> impl Responder {
+    if !validate_session(&session) {
+        return HttpResponse::Ok()
+        .status(StatusCode::FOUND)
+        .append_header((header::LOCATION, "/"))
+        .finish();
+    }
+
+    let is_ssh: bool = session.get("user_is_ssh").unwrap_or(None).unwrap();
+    if !is_ssh {
+        return HttpResponse::Ok()
+        .status(StatusCode::FOUND)
+        .append_header((header::LOCATION, "/"))
+        .finish();
+    }
+
+    let uid = session.get("user_id").unwrap().unwrap();
+
+    match ldap_wrapper.manage_ssh_key(uid, form.key_to_delete.clone(), ldap::ManageSSHOps::DEL).await {
+        Ok(_) => return HttpResponse::Ok()
+                .status(StatusCode::FOUND)
+                .append_header((header::LOCATION, "/ssh"))
+                .finish(),
+        Err(e) => {
+            session.insert("error_message", e.to_string()).unwrap();
+            return HttpResponse::Ok()
+            .status(StatusCode::FOUND)
+            .append_header((header::LOCATION, "/ssh"))
+            .finish()
+        },
+    }
 }
 
 pub async fn signout(session: Session) -> impl Responder {

+ 19 - 5
src/ldap/lib.rs

@@ -27,6 +27,11 @@ impl fmt::Display for Error {
     }
 }
 
+pub enum ManageSSHOps {
+    ADD,
+    DEL
+}
+
 #[derive(Clone)]
 pub struct LdapWrapper {
     ldap_pool: Pool<LdapManager>,
@@ -194,16 +199,25 @@ impl LdapWrapper {
         Ok(ssh_keys)
     }
 
-    pub async fn add_ssh_key(&self, username: String, ssh_key: String) -> Result<(), Error> {
+    pub async fn manage_ssh_key(&self, username: String, ssh_key: String, op: ManageSSHOps) -> Result<(), Error> {
         let mut ldap = self.ldap_pool.get().await.unwrap();
-        let add = ldap
-        .modify(format!("uid={},{}", username, self.config.basedn).as_str(), vec![Mod::Add("sshPublicKey", HashSet::from([ssh_key.as_str()]))]).await;
+        let mods: Vec<Mod<&str>>;
+        match op {
+            ManageSSHOps::ADD => {
+                mods = vec![Mod::Add("sshPublicKey", HashSet::from([ssh_key.as_str()]))];
+            },
+            ManageSSHOps::DEL => {
+                mods = vec![Mod::Delete("sshPublicKey", HashSet::from([ssh_key.as_str()]))];
+            },
+        }
+        let res = ldap
+        .modify(format!("uid={},{}", username, self.config.basedn).as_str(), mods).await;
 
-        if let Err(e) = add {
+        if let Err(e) = res {
             return Err(Error::LdapServerError { message: format!("An error occured, contact admins: {}", e)});
         }
 
-        match add.unwrap().success() {
+        match res.unwrap().success() {
             Ok(_) => Ok(()),
             Err(e) => Err(Error::LdapServerError { message: format!("An error occured, contact admins: {}", e)}),
         }