Merge branch '#8_return_user_info_on_successful_modification' into 'main'

feat: returns the user details on successful account modification

Closes #8

See merge request compsoc1/skynet/ldap/backend!6
This commit is contained in:
eoghanconlon73 2023-08-05 16:59:29 +00:00
commit ec3064faab
2 changed files with 61 additions and 3 deletions

View file

@ -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

View file

@ -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<String>,
#[serde(rename = "sshPublicKey")]
ssh_public_key: Option<String>,
cn: Option<String>,
#[serde(rename = "skDiscord")]
sk_discord: Option<String>,
}
/// Handles updating a single field with the users own password
pub async fn post_update_ldap(mut req: Request<State>) -> tide::Result {
let LdapUpdate {
@ -80,7 +90,41 @@ pub async fn post_update_ldap(mut req: Request<State>) -> 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<String, Vec<String>>, field: &str) -> Option<String> {
if let Some(field) = attrs.get(field) {
if !field.is_empty() {
return Some(field[0].clone());
}
}
None
}