From d1a42b178a651b00c198f9aece1fb0e32eee2b45 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 26 May 2021 08:42:05 +0200 Subject: [PATCH] Make display_name, first&last name optional --- model/src/lib.rs | 12 ++++++------ src/domain/sql_backend_handler.rs | 8 +------- src/infra/ldap_handler.rs | 25 ++++++++++++++----------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/model/src/lib.rs b/model/src/lib.rs index eed7bac..cafcb4e 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -26,9 +26,9 @@ pub struct ListUsersRequest { pub struct User { pub user_id: String, pub email: String, - pub display_name: String, - pub first_name: String, - pub last_name: String, + pub display_name: Option, + pub first_name: Option, + pub last_name: Option, // pub avatar: ?, pub creation_date: chrono::NaiveDateTime, } @@ -38,9 +38,9 @@ impl Default for User { User { user_id: String::new(), email: String::new(), - display_name: String::new(), - first_name: String::new(), - last_name: String::new(), + display_name: None, + first_name: None, + last_name: None, creation_date: chrono::NaiveDateTime::from_timestamp(0, 0), } } diff --git a/src/domain/sql_backend_handler.rs b/src/domain/sql_backend_handler.rs index c4beb5e..dc1d16a 100644 --- a/src/domain/sql_backend_handler.rs +++ b/src/domain/sql_backend_handler.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use futures_util::StreamExt; use futures_util::TryStreamExt; use log::*; -use sea_query::{Expr, Iden, Order, Query, SimpleExpr}; +use sea_query::{Expr, Iden, Order, Query, SimpleExpr, Value}; use sqlx::Row; use std::collections::HashSet; @@ -205,18 +205,12 @@ mod tests { .columns(vec![ Users::UserId, Users::Email, - Users::DisplayName, - Users::FirstName, - Users::LastName, Users::CreationDate, Users::Password, ]) .values_panic(vec![ name.into(), "bob@bob".into(), - "Bob Böbberson".into(), - "Bob".into(), - "Böbberson".into(), chrono::NaiveDateTime::from_timestamp(0, 0).into(), pass.into(), ]) diff --git a/src/infra/ldap_handler.rs b/src/infra/ldap_handler.rs index c45648a..2585178 100644 --- a/src/infra/ldap_handler.rs +++ b/src/infra/ldap_handler.rs @@ -67,11 +67,14 @@ fn get_attribute(user: &User, attribute: &str) -> Result> { "posixAccount".to_string(), "mailAccount".to_string(), ]), - "uid" => Ok(vec![user.user_id.to_string()]), - "mail" => Ok(vec![user.email.to_string()]), - "givenName" => Ok(vec![user.first_name.to_string()]), - "sn" => Ok(vec![user.last_name.to_string()]), - "cn" => Ok(vec![user.display_name.to_string()]), + "uid" => Ok(vec![user.user_id.clone()]), + "mail" => Ok(vec![user.email.clone()]), + "givenName" => Ok(vec![user.first_name.clone().unwrap_or("".to_string())]), + "sn" => Ok(vec![user.last_name.clone().unwrap_or("".to_string())]), + "cn" => Ok(vec![user + .display_name + .clone() + .unwrap_or_else(|| user.user_id.clone())]), _ => bail!("Unsupported attribute: {}", attribute), } } @@ -481,17 +484,17 @@ mod tests { User { user_id: "bob_1".to_string(), email: "bob@bobmail.bob".to_string(), - display_name: "Bôb Böbberson".to_string(), - first_name: "Bôb".to_string(), - last_name: "Böbberson".to_string(), + display_name: Some("Bôb Böbberson".to_string()), + first_name: Some("Bôb".to_string()), + last_name: Some("Böbberson".to_string()), creation_date: NaiveDateTime::from_timestamp(1_000_000, 0), }, User { user_id: "jim".to_string(), email: "jim@cricket.jim".to_string(), - display_name: "Jimminy Cricket".to_string(), - first_name: "Jim".to_string(), - last_name: "Cricket".to_string(), + display_name: Some("Jimminy Cricket".to_string()), + first_name: Some("Jim".to_string()), + last_name: Some("Cricket".to_string()), creation_date: NaiveDateTime::from_timestamp(1_500_000, 0), }, ])