From 61ccaf0bdb70448b4972339053a761cdc8c4b8d7 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 5 Aug 2023 17:36:48 +0100 Subject: [PATCH] feat: returns the user details on successful account modification Closes #8 --- README.md | 14 ++++++++++ src/methods/account_update.rs | 50 ++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4392de7..365ea29 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,20 @@ Fields: } ``` +Success: +Each value is either a string or ``null``. +```json +{ + "result": "success", + "success": { + "cn": "Firstname Surname", + "mail": "Email address", + "skDiscord": null, + "sshPublicKey": "ssh key" + } +} +``` + Changing ``userPassword`` requires the existing password in teh apssword field and the new one in teh value field. ### POST /ldap/new diff --git a/src/methods/account_update.rs b/src/methods/account_update.rs index 9dd0f27..4cbbea6 100644 --- a/src/methods/account_update.rs +++ b/src/methods/account_update.rs @@ -1,8 +1,8 @@ use crate::State; use ldap3::{exop::PasswordModify, LdapConn, Mod, Scope, SearchEntry}; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use tide::{ - prelude::{json, Deserialize}, + prelude::{json, Deserialize, Serialize}, Request, }; @@ -14,6 +14,16 @@ pub struct LdapUpdate { value: String, } +#[derive(Debug, Serialize)] +pub struct ModifyResult { + mail: Option, + #[serde(rename = "sshPublicKey")] + ssh_public_key: Option, + cn: Option, + #[serde(rename = "skDiscord")] + sk_discord: Option, +} + /// Handles updating a single field with the users own password pub async fn post_update_ldap(mut req: Request) -> tide::Result { let LdapUpdate { @@ -80,7 +90,41 @@ pub async fn post_update_ldap(mut req: Request) -> tide::Result { ldap.extended(tmp)?.success()?; }; + let result = get_result(&mut ldap, &dn); + ldap.unbind()?; - Ok(json!({"result": "success"}).into()) + Ok(json!({"result": "success", "success": result}).into()) +} + +fn get_result(ldap: &mut LdapConn, dn: &str) -> ModifyResult { + let mut result = ModifyResult { + mail: None, + ssh_public_key: None, + cn: None, + sk_discord: None, + }; + + if let Ok(temp) = ldap.search(dn, Scope::Base, "(objectClass=*)", vec!["mail", "sshPublicKey", "cn", "skDiscord"]) { + if let Ok((rs, _res)) = temp.success() { + if !rs.is_empty() { + let tmp = SearchEntry::construct(rs[0].clone()); + result.mail = get_result_values(&tmp.attrs, "mail"); + result.ssh_public_key = get_result_values(&tmp.attrs, "sshPublicKey"); + result.cn = get_result_values(&tmp.attrs, "cn"); + result.sk_discord = get_result_values(&tmp.attrs, "skDiscord"); + } + } + } + + result +} + +fn get_result_values(attrs: &HashMap>, field: &str) -> Option { + if let Some(field) = attrs.get(field) { + if !field.is_empty() { + return Some(field[0].clone()); + } + } + None }